diff --git a/src/main/java/com/example/demo/author/controller/AuthorController.java b/src/main/java/com/example/demo/author/controller/AuthorController.java index 043ddc0..dbca18c 100644 --- a/src/main/java/com/example/demo/author/controller/AuthorController.java +++ b/src/main/java/com/example/demo/author/controller/AuthorController.java @@ -1,12 +1,13 @@ package com.example.demo.author.controller; +import com.example.demo.WebConfiguration; import com.example.demo.author.service.AuthorService; +import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; - @RestController -@RequestMapping("/authors") +@RequestMapping(WebConfiguration.REST_API +"/authors") public class AuthorController { private final AuthorService authorService; public AuthorController(AuthorService authorService){ @@ -25,26 +26,24 @@ public class AuthorController { } @PostMapping - public AuthorDto createAuthor(@RequestParam("name") String name, - @RequestParam("surname") String surname, - @RequestParam("patronymic") String patronymic) { - return new AuthorDto(authorService.addAuthor(name, surname, patronymic)); + public AuthorDto createAuthor(@RequestBody @Valid AuthorDto authorDto) { + return new AuthorDto(authorService.addAuthor(authorDto.getName(), authorDto.getSurname(), authorDto.getPatronymic())); } @PutMapping("/{id}") public AuthorDto updateAuthor(@PathVariable Long id, - @RequestParam("name") String name, - @RequestParam("surname") String surname, - @RequestParam("patronymic") String patronymic) { - return new AuthorDto(authorService.updateAuthor(id, name, surname, patronymic)); - } - @GetMapping("/getC") - public Long getCountGenreOfFilmFromAuthor(@RequestParam("id") Long id){ - return authorService.getCountGenreOfFilmFromAuthor(id); + @RequestBody @Valid AuthorDto authorDto) { + return new AuthorDto(authorService.updateAuthor(id, authorDto.getName(), authorDto.getSurname(), authorDto.getPatronymic())); } @DeleteMapping("/{id}") public AuthorDto deleteAuthor(@PathVariable Long id) { return new AuthorDto(authorService.deleteAuthor(id)); } + @GetMapping("/getC") + public Long getCountGenreOfFilmFromAuthor(@RequestParam("id") Long id){ + return authorService.getCountGenreOfFilmFromAuthor(id); + } + + } diff --git a/src/main/java/com/example/demo/author/controller/AuthorDto.java b/src/main/java/com/example/demo/author/controller/AuthorDto.java index 826b6d6..97b32a4 100644 --- a/src/main/java/com/example/demo/author/controller/AuthorDto.java +++ b/src/main/java/com/example/demo/author/controller/AuthorDto.java @@ -1,18 +1,26 @@ package com.example.demo.author.controller; import com.example.demo.author.model.Author; +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotBlank; public class AuthorDto { - private final long id; - private final String name; - private final String surname; - private final String patronymic; + private long id; + @NotBlank(message = "Name can't be null or empty") + private String name; + @NotBlank(message = "Surname can't be null or empty") + private String surname; + private String patronymic; + public AuthorDto(){ + + } public AuthorDto(Author author){ id = author.getId(); name = author.getName(); surname = author.getSurname(); patronymic = author.getPatronymic(); } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public long getId() { return id; } @@ -28,4 +36,17 @@ public class AuthorDto { public String getPatronymic() { return patronymic; } + public void setId(long id) { + this.id = id; + } + public void setName(String name) { + this.name = name; + } + public void setSurname(String surname) { + this.surname = surname; + } + public void setPatronymic(String patronymic) { + this.patronymic = patronymic; + } + } diff --git a/src/main/java/com/example/demo/author/controller/AuthorDtoFull.java b/src/main/java/com/example/demo/author/controller/AuthorDtoFull.java index b3dcb5c..589f633 100644 --- a/src/main/java/com/example/demo/author/controller/AuthorDtoFull.java +++ b/src/main/java/com/example/demo/author/controller/AuthorDtoFull.java @@ -2,12 +2,16 @@ package com.example.demo.author.controller; import com.example.demo.author.model.Author; import com.example.demo.film.controller.FilmDtoWithoutAuthor; +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotBlank; import java.util.List; public class AuthorDtoFull { private final long id; + @NotBlank(message = "Name can't be null or empty") private final String name; + @NotBlank(message = "Surname can't be null or empty") private final String surname; private final String patronymic; private final List films; @@ -18,6 +22,7 @@ public class AuthorDtoFull { patronymic = author.getPatronymic(); films = author.getFilms().stream().map(FilmDtoWithoutAuthor::new).toList(); } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public long getId() { return id; } @@ -37,4 +42,4 @@ public class AuthorDtoFull { public List getFilms() { return films; } -} +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/genre/controller/GenreController.java b/src/main/java/com/example/demo/genre/controller/GenreController.java index 76efc68..e051e78 100644 --- a/src/main/java/com/example/demo/genre/controller/GenreController.java +++ b/src/main/java/com/example/demo/genre/controller/GenreController.java @@ -1,12 +1,14 @@ package com.example.demo.genre.controller; +import com.example.demo.WebConfiguration; import com.example.demo.genre.service.GenreService; +import jakarta.validation.Valid; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/genres") +@RequestMapping(WebConfiguration.REST_API + "/genres") public class GenreController { private final GenreService genreService; public GenreController(GenreService genreService){ @@ -25,14 +27,14 @@ public class GenreController { } @PostMapping - public GenreDto createGenre(@RequestParam("name") String name) { - return new GenreDto(genreService.addGenre(name)); + public GenreDto createGenre(@RequestBody @Valid GenreDto genreDto) { + return new GenreDto(genreService.addGenre(genreDto.getName())); } @PutMapping("/{id}") public GenreDto updateGenre(@PathVariable Long id, - @RequestParam("name") String name) { - return new GenreDto(genreService.updateGenre(id, name)); + @RequestBody @Valid GenreDto genreDto) { + return new GenreDto(genreService.updateGenre(id, genreDto.getName())); } @DeleteMapping("/{id}") diff --git a/src/main/java/com/example/demo/genre/controller/GenreDto.java b/src/main/java/com/example/demo/genre/controller/GenreDto.java index 20808bb..7fbe7c3 100644 --- a/src/main/java/com/example/demo/genre/controller/GenreDto.java +++ b/src/main/java/com/example/demo/genre/controller/GenreDto.java @@ -1,19 +1,30 @@ package com.example.demo.genre.controller; import com.example.demo.genre.model.Genre; +import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.validation.constraints.NotBlank; public class GenreDto { - private final long id; - private final String name; - + private long id; + @NotBlank(message = "Name can't be null or empty") + private String name; + public GenreDto() { + } public GenreDto(Genre genre) { this.id = genre.getId(); this.name = genre.getName(); } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public long getId() { return id; } public String getName() { return name; } + public void setId(long id) { + this.id = id; + } + public void setName(String name) { + this.name = name; + } } diff --git a/src/main/java/com/example/demo/genre/controller/GenreMvcController.java b/src/main/java/com/example/demo/genre/controller/GenreMvcController.java new file mode 100644 index 0000000..c6c21e3 --- /dev/null +++ b/src/main/java/com/example/demo/genre/controller/GenreMvcController.java @@ -0,0 +1,63 @@ +package com.example.demo.genre.controller; + +import com.example.demo.genre.service.GenreService; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.validation.Valid; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping("/genres") +public class GenreMvcController { + private final GenreService genreService; + public GenreMvcController(GenreService genreService){ + this.genreService = genreService; + } + @ModelAttribute("requestURI") + public String requestURI(final HttpServletRequest request) { + return request.getRequestURI(); + } + @GetMapping + public String getGenres(Model model) { + model.addAttribute("genres", + genreService.findAllGenres().stream() + .map(GenreDto::new) + .toList()); + return "genre"; + } + @GetMapping(value = {"/edit", "/edit/{id}"}) + public String editGenre(@PathVariable(required = false) Long id, + Model model) { + if (id == null || id <= 0) { + model.addAttribute("genreDto", new GenreDto()); + } else { + model.addAttribute("genreId", id); + model.addAttribute("genreDto", new GenreDto(genreService.findGenre(id))); + } + return "genre-edit"; + } + @PostMapping(value = {"", "/{id}"}) + public String saveGenre(@PathVariable(required = false) Long id, + @ModelAttribute("genreDto") @Valid GenreDto genreDto, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "genre-edit"; + } + if (id == null || id <= 0) { + genreService.addGenre(genreDto.getName()); + } else { + genreService.updateGenre(id, genreDto.getName()); + } + return "redirect:/genres"; + } + + @PostMapping("/delete/{id}") + public String deleteGenre(@PathVariable Long id) { + genreService.deleteGenre(id); + return "redirect:/genres"; + } +}