diff --git a/backend/src/main/java/com/example/backend/controller/MovieController.java b/backend/src/main/java/com/example/backend/controller/MovieController.java index ff680f3..5146178 100644 --- a/backend/src/main/java/com/example/backend/controller/MovieController.java +++ b/backend/src/main/java/com/example/backend/controller/MovieController.java @@ -29,12 +29,19 @@ public List getAll(){ return movieService.getAllMovies().stream().map(MovieResponse::from).collect(Collectors.toList()); } - @GetMapping("/{movieId}") - public MovieResponse getByMovieId(@PathVariable @NonNull Long movieId) { - Movie searchedMovie = movieService.getMovieById(movieId); + @GetMapping("/{id}") + public MovieResponse getByMovieId(@PathVariable @NonNull Long id) { + Movie searchedMovie = movieService.getMovieById(id); return MovieResponse.from(searchedMovie); } + @PutMapping("/{id}") + public MovieResponse update(@RequestBody @NotNull CreateMovieRequest movieRequest, @PathVariable Long id) { + Movie movie = movieRequest.toMovie(); + movie.setId(id); + return MovieResponse.from(movieService.updateMovie(movie)); + } + @DeleteMapping("/{id}") public void delete(@PathVariable Long id){ movieService.deleteMovie(id); diff --git a/backend/src/main/java/com/example/backend/service/MovieService.java b/backend/src/main/java/com/example/backend/service/MovieService.java index 3f6e3ef..cb88273 100644 --- a/backend/src/main/java/com/example/backend/service/MovieService.java +++ b/backend/src/main/java/com/example/backend/service/MovieService.java @@ -36,4 +36,10 @@ public void deleteMovie(Long movieId) { throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with id " + movieId + " does not exist"); } } + public Movie updateMovie(Movie movie) { + if (movieRepository.existsById(movie.getId())) { + return movieRepository.save(movie); + } + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Movie with id " + movie.getId() + " does not exist"); + } } diff --git a/backend/src/test/java/com/example/backend/controller/MovieControllerTest.java b/backend/src/test/java/com/example/backend/controller/MovieControllerTest.java index dc98993..d05ea97 100644 --- a/backend/src/test/java/com/example/backend/controller/MovieControllerTest.java +++ b/backend/src/test/java/com/example/backend/controller/MovieControllerTest.java @@ -99,6 +99,7 @@ void getAll() throws Exception { .andExpect(jsonPath("$[1].name").value(NAME_DEADPOOL)); } + @Test @DirtiesContext void getMovie_By_ID_Test() throws Exception { @@ -115,6 +116,7 @@ void getMovie_By_ID_Test() throws Exception { .andExpect(jsonPath("$.id").value(ID_FIRST)); } + @Test @DirtiesContext void getMovie_By_NonExistingID() throws Exception { @@ -129,6 +131,63 @@ void getMovie_By_NonExistingID() throws Exception { } + @Test + @DirtiesContext + void updateMovie_Sucessfull() throws Exception { + movieRepository.saveAll( + List.of( + Movie.builder().name(NAME_MEMENTO).build(), + Movie.builder().name(NAME_DEADPOOL).build() + ) + ); + mockMvc.perform(MockMvcRequestBuilders.put(URL_BASE + "/" + ID_FIRST) + .contentType(MediaType.APPLICATION_JSON) + .content( + """ + { + "name": "Memento Edited", + "isWatched": true, + "rating": 10 + } + """ + )) + .andExpect(MockMvcResultMatchers.status().isOk()); + mockMvc.perform(MockMvcRequestBuilders.get(URL_BASE)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$", hasSize(2))) + .andExpect(jsonPath("$[0].name").value("Memento Edited")) + .andExpect(jsonPath("$[1].name").value(NAME_DEADPOOL)); + } + @Test + @DirtiesContext + void updateMovie_NonExistand_ID() throws Exception { + movieRepository.saveAll( + List.of( + Movie.builder().name(NAME_MEMENTO).build(), + Movie.builder().name(NAME_DEADPOOL).build() + ) + ); + mockMvc.perform(MockMvcRequestBuilders.put(URL_BASE + "/" + 3) + .contentType(MediaType.APPLICATION_JSON) + .content( + """ + { + "name": "Memento Edited", + "isWatched": true, + "rating": 10 + } + """ + )) + .andExpect(MockMvcResultMatchers.status().is4xxClientError()); + mockMvc.perform(MockMvcRequestBuilders.get(URL_BASE)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$", hasSize(2))) + .andExpect(jsonPath("$[0].name").value(NAME_MEMENTO)) + .andExpect(jsonPath("$[1].name").value(NAME_DEADPOOL)); + } + @Test @DirtiesContext void deleteTest_Successful() throws Exception { @@ -146,6 +205,7 @@ void deleteTest_Successful() throws Exception { .andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$[0].name").value(NAME_DEADPOOL)); } + @Test @DirtiesContext void deleteTest_NonExisting_ID() throws Exception {