DictionaryRestService.java 1.63 KB
Newer Older
Michele Fiori's avatar
Michele Fiori committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package services;

import beans.Dictionary;
import beans.Word;

import javax.ws.rs.*;
import javax.ws.rs.core.Response;


@Path("dictionary")
public class DictionaryRestService {

    @Path("add")
    @POST
    @Consumes({"application/json", "application/xml"})
    public Response addWord(Word w){
        Dictionary dict = Dictionary.getInstance();
        int ret = dict.addWord(w.getWord().toLowerCase(), w.getDefinition());
        if(ret == -1){
            return Response.status(Response.Status.NOT_ACCEPTABLE).build();
        }else{
            return Response.ok().build();
        }
    }

    @Path("modify")
    @PUT
    @Consumes({"application/xml", "application/json"})
    public Response changeDefinition(Word w){
        Dictionary dict = Dictionary.getInstance();
        int ret = dict.changeWordDefinition(w.getWord().toLowerCase(), w.getDefinition());
        if(ret == -1){
            return Response.status(Response.Status.NOT_FOUND).build();
        }else{
            return Response.ok().build();
        }
    }

    @Path("get/{word}")
    @GET
    @Produces({"text/plain"})
    public String getDefinition(@PathParam("word") String word){
        Dictionary dict = Dictionary.getInstance();
        String ret = dict.viewDefinition(word.toLowerCase());
        if(ret == null){
            return "Word not in dictionary";
        }
        return "Definition of " + word + ": " + ret;
    }

    @Path("delete/{word}")
    @DELETE
    public Response deleteWord(@PathParam("word") String word){
        Dictionary dict = Dictionary.getInstance();
        dict.deleteWord(word.toLowerCase());
        return Response.ok().build();
    }

}