|
13 | 13 | @Path("/movies") |
14 | 14 | public class MovieCrudService { |
15 | 15 |
|
16 | | - private Map<String, Movie> inventory = new HashMap<String, Movie>(); |
17 | | - |
18 | | - @GET |
19 | | - @Path("/getinfo") |
20 | | - @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) |
21 | | - public Movie movieByImdbId(@QueryParam("imdbId") String imdbId) { |
22 | | - |
23 | | - System.out.println("*** Calling getinfo for a given ImdbID***"); |
24 | | - |
25 | | - if (inventory.containsKey(imdbId)) { |
26 | | - return inventory.get(imdbId); |
27 | | - } else |
28 | | - return null; |
29 | | - } |
30 | | - |
31 | | - @POST |
32 | | - @Path("/addmovie") |
33 | | - @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) |
34 | | - public Response addMovie(Movie movie) { |
35 | | - |
36 | | - System.out.println("*** Calling addMovie ***"); |
37 | | - |
38 | | - if (null != inventory.get(movie.getImdbId())) { |
39 | | - return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build(); |
40 | | - } |
41 | | - |
42 | | - inventory.put(movie.getImdbId(), movie); |
43 | | - return Response.status(Response.Status.CREATED).build(); |
44 | | - } |
45 | | - |
46 | | - @PUT |
47 | | - @Path("/updatemovie") |
48 | | - @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) |
49 | | - public Response updateMovie(Movie movie) { |
50 | | - |
51 | | - System.out.println("*** Calling updateMovie ***"); |
52 | | - |
53 | | - if (null == inventory.get(movie.getImdbId())) { |
54 | | - return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is not in the database.\nUnable to Update").build(); |
55 | | - } |
56 | | - |
57 | | - inventory.put(movie.getImdbId(), movie); |
58 | | - return Response.status(Response.Status.OK).build(); |
59 | | - } |
60 | | - |
61 | | - @DELETE |
62 | | - @Path("/deletemovie") |
63 | | - public Response deleteMovie(@QueryParam("imdbId") String imdbId) { |
64 | | - |
65 | | - System.out.println("*** Calling deleteMovie ***"); |
66 | | - |
67 | | - if (null == inventory.get(imdbId)) { |
68 | | - return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete").build(); |
69 | | - } |
70 | | - |
71 | | - inventory.remove(imdbId); |
72 | | - return Response.status(Response.Status.OK).build(); |
73 | | - } |
74 | | - |
75 | | - @GET |
76 | | - @Path("/listmovies") |
77 | | - @Produces({ "application/json" }) |
78 | | - public List<Movie> listMovies() { |
79 | | - |
80 | | - return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); |
81 | | - } |
| 16 | + private Map<String, Movie> inventory = new HashMap<String, Movie>(); |
| 17 | + |
| 18 | + @GET |
| 19 | + @Path("/") |
| 20 | + @Produces({ MediaType.TEXT_PLAIN }) |
| 21 | + public Response index() { |
| 22 | + return Response.status(200).header("Access-Control-Allow-Origin", "*") |
| 23 | + .header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization") |
| 24 | + .header("Access-Control-Allow-Credentials", "true") |
| 25 | + .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD").entity("").build(); |
| 26 | + } |
| 27 | + |
| 28 | + @GET |
| 29 | + @Path("/getinfo") |
| 30 | + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) |
| 31 | + public Movie movieByImdbId(@QueryParam("imdbId") String imdbId) { |
| 32 | + |
| 33 | + System.out.println("*** Calling getinfo for a given ImdbID***"); |
| 34 | + |
| 35 | + if (inventory.containsKey(imdbId)) { |
| 36 | + return inventory.get(imdbId); |
| 37 | + } else |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + @POST |
| 42 | + @Path("/addmovie") |
| 43 | + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) |
| 44 | + public Response addMovie(Movie movie) { |
| 45 | + |
| 46 | + System.out.println("*** Calling addMovie ***"); |
| 47 | + |
| 48 | + if (null != inventory.get(movie.getImdbId())) { |
| 49 | + return Response.status(Response.Status.NOT_MODIFIED).entity("Movie is Already in the database.").build(); |
| 50 | + } |
| 51 | + |
| 52 | + inventory.put(movie.getImdbId(), movie); |
| 53 | + return Response.status(Response.Status.CREATED).build(); |
| 54 | + } |
| 55 | + |
| 56 | + @PUT |
| 57 | + @Path("/updatemovie") |
| 58 | + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) |
| 59 | + public Response updateMovie(Movie movie) { |
| 60 | + |
| 61 | + System.out.println("*** Calling updateMovie ***"); |
| 62 | + |
| 63 | + if (null == inventory.get(movie.getImdbId())) { |
| 64 | + return Response.status(Response.Status.NOT_MODIFIED) |
| 65 | + .entity("Movie is not in the database.\nUnable to Update").build(); |
| 66 | + } |
| 67 | + |
| 68 | + inventory.put(movie.getImdbId(), movie); |
| 69 | + return Response.status(Response.Status.OK).build(); |
| 70 | + } |
| 71 | + |
| 72 | + @DELETE |
| 73 | + @Path("/deletemovie") |
| 74 | + public Response deleteMovie(@QueryParam("imdbId") String imdbId) { |
| 75 | + |
| 76 | + System.out.println("*** Calling deleteMovie ***"); |
| 77 | + |
| 78 | + if (null == inventory.get(imdbId)) { |
| 79 | + return Response.status(Response.Status.NOT_FOUND).entity("Movie is not in the database.\nUnable to Delete") |
| 80 | + .build(); |
| 81 | + } |
| 82 | + |
| 83 | + inventory.remove(imdbId); |
| 84 | + return Response.status(Response.Status.OK).build(); |
| 85 | + } |
| 86 | + |
| 87 | + @GET |
| 88 | + @Path("/listmovies") |
| 89 | + @Produces({ "application/json" }) |
| 90 | + public List<Movie> listMovies() { |
| 91 | + |
| 92 | + return inventory.values().stream().collect(Collectors.toCollection(ArrayList::new)); |
| 93 | + } |
82 | 94 |
|
83 | 95 | } |
0 commit comments