задаток на 5 лабу
This commit is contained in:
parent
dcff548ad8
commit
e4af02f2fe
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<FilmDtoWithoutAuthor> 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<FilmDtoWithoutAuthor> getFilms() {
|
||||
return films;
|
||||
}
|
||||
}
|
||||
}
|
@ -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}")
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user