From 25d1af73995d63b014a733de2acf6afa017caa5b Mon Sep 17 00:00:00 2001 From: ekallin Date: Sun, 17 Mar 2024 20:42:09 +0400 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=20=D0=BA?= =?UTF-8?q?=D0=BB=D0=B0=D1=81=D1=81=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D1=80=20=D0=B4=D0=BB=D1=8F=20=D1=84=D0=B8?= =?UTF-8?q?=D0=BB=D1=8C=D0=BC=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/movies/api/MovieController.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 backend/src/main/java/com/example/backend/movies/api/MovieController.java diff --git a/backend/src/main/java/com/example/backend/movies/api/MovieController.java b/backend/src/main/java/com/example/backend/movies/api/MovieController.java new file mode 100644 index 0000000..804b281 --- /dev/null +++ b/backend/src/main/java/com/example/backend/movies/api/MovieController.java @@ -0,0 +1,73 @@ +package com.example.backend.movies.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.example.backend.categories.service.CategorieService; +import com.example.backend.core.configurations.Constants; +import com.example.backend.movies.model.MovieEntity; +import com.example.backend.movies.service.MovieService; + +import jakarta.validation.Valid; + +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.PutMapping; + +@RestController +@RequestMapping(Constants.API_URL + "/movie") +public class MovieController { + + private final MovieService movieService; + private final CategorieService categorieService; + private final ModelMapper modelMapper; + + public MovieController(MovieService movieService, CategorieService categorieService, ModelMapper modelMapper) { + this.modelMapper = modelMapper; + this.categorieService = categorieService; + this.movieService = movieService; + } + + private MovieDTO toDto(MovieEntity entity) { + return modelMapper.map(entity, MovieDTO.class); + } + + private MovieEntity toEntity(MovieDTO dto) { + final MovieEntity entity = modelMapper.map(dto, MovieEntity.class); + entity.setCategorie(categorieService.get(dto.getId())); + return entity; + } + + @GetMapping + public List getAll(@RequestParam(name = "categorieId", defaultValue = "0") Integer categorieId) { + return movieService.getAll(categorieId).stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public MovieDTO get(@PathVariable(name = "id") Integer id) { + return toDto(movieService.get(id)); + } + + @PostMapping + public MovieDTO create(@RequestBody @Valid MovieDTO dto) { + return toDto(movieService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public MovieDTO update(@PathVariable Integer id, @RequestBody MovieDTO dto) { + return toDto(movieService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public MovieDTO delete(@PathVariable Integer id) { + return toDto(movieService.delete(id)); + } + +}