|
| 1 | +package com.baeldung.server; |
| 2 | + |
| 3 | +import com.baeldung.model.Movie; |
| 4 | +import javax.ws.rs.*; |
| 5 | +import javax.ws.rs.core.MediaType; |
| 6 | +import javax.ws.rs.core.Response; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +@Path("/movies") |
| 14 | +public class MovieCrudService { |
| 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 | + } |
| 82 | + |
| 83 | +} |
0 commit comments