diff --git a/Frontend/vue-project/src/mixins/CatalogMixins.js b/Frontend/vue-project/src/mixins/CatalogMixins.js index 4b493d2..3c700ba 100644 --- a/Frontend/vue-project/src/mixins/CatalogMixins.js +++ b/Frontend/vue-project/src/mixins/CatalogMixins.js @@ -82,7 +82,7 @@ const CatalogMixin = { const promises = []; const self = this; this.selectedItems.forEach(item => { - promises.push(DataService.delete(this.dataUrl + item)); + promises.push(DataService.delete(this.dataUrl + "/" + item)); }); Promise.all(promises).then((results) => { results.forEach(function (id) { diff --git a/data.mv.db b/data.mv.db index 39ecef2..8c0e32d 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java index d0db0aa..51c88f4 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreController.java @@ -1,16 +1,9 @@ package ru.ulstu.is.lab1.DataBase.controller; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PatchMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import ru.ulstu.is.lab1.DataBase.service.GenreService; +import javax.validation.Valid; import java.util.List; @RestController @@ -18,7 +11,6 @@ import java.util.List; public class GenreController { private final GenreService genreService; - @Autowired public GenreController(GenreService genreService) { this.genreService = genreService; } @@ -28,19 +20,19 @@ public class GenreController { return new GenreDTO(genreService.findGenre(id)); } - @GetMapping + @GetMapping("/") public List getGenre() { return genreService.findAllGenres().stream().map(GenreDTO::new).toList(); } - @PostMapping - public GenreDTO createGenre(@RequestParam("name") String name) { - return new GenreDTO(genreService.addGenre(name)); + @PostMapping("/") + public GenreDTO createGenre(@RequestBody @Valid GenreDTO genreDTO) { + return new GenreDTO(genreService.addGenre(genreDTO.getName())); } - @PatchMapping("/{id}") - public GenreDTO updateGenre(@PathVariable Long id, @RequestParam("name") String name) { - return new GenreDTO(genreService.updateGenre(id, name)); + @PutMapping("/{id}") + public GenreDTO updateGenre(@PathVariable Long id, @RequestBody @Valid GenreDTO genreDTO) { + return new GenreDTO(genreService.updateGenre(id, genreDTO.getName())); } @DeleteMapping("/{id}") @@ -48,7 +40,7 @@ public class GenreController { return new GenreDTO(genreService.deleteGenre(id)); } - @DeleteMapping + @DeleteMapping("/") public void deleteAllGenres(){ genreService.deleteAllGenres(); } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java index fa616f0..b49a765 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/controller/GenreDTO.java @@ -5,16 +5,17 @@ import ru.ulstu.is.lab1.DataBase.model.Genre; public class GenreDTO { private Long id; private String name; + public GenreDTO() { + } public GenreDTO(Genre genre){ this.id = genre.getId(); this.name = genre.getName(); } - public GenreDTO() { - } public Long getId(){ return id; } public String getName(){ return name; } + public void setName(String name) { this.name = name; } } diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java index cfb0d7a..cd9319b 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/model/Genre.java @@ -3,7 +3,6 @@ package ru.ulstu.is.lab1.DataBase.model; import javax.persistence.*; import javax.validation.constraints.NotBlank; -import java.util.List; import java.util.Objects; @Entity @@ -35,10 +34,12 @@ public class Genre { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) + return true; + if (!(o instanceof Genre)) + return false; Genre genre = (Genre) o; - return Objects.equals(id, genre.id); + return Objects.equals(id, genre.id) && Objects.equals(this.name, genre.name); } @Override public int hashCode() { diff --git a/src/main/java/ru/ulstu/is/lab1/DataBase/service/GenreService.java b/src/main/java/ru/ulstu/is/lab1/DataBase/service/GenreService.java index ab78ab7..dbfb3cf 100644 --- a/src/main/java/ru/ulstu/is/lab1/DataBase/service/GenreService.java +++ b/src/main/java/ru/ulstu/is/lab1/DataBase/service/GenreService.java @@ -6,6 +6,7 @@ import ru.ulstu.is.lab1.DataBase.Repository.IGenreRepository; import ru.ulstu.is.lab1.util.validation.ValidatorUtil; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; import java.util.List; import java.util.Optional; @@ -21,6 +22,9 @@ public class GenreService { @Transactional public Genre addGenre(String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Genre name is null or empty"); + } final Genre genre = new Genre(name); validatorUtil.validate(genre); return genreRepository.save(genre); @@ -39,6 +43,9 @@ public class GenreService { @Transactional public Genre updateGenre(Long id, String name) { + if (!StringUtils.hasText(name)) { + throw new IllegalArgumentException("Genre name is null or empty"); + } final Genre currentGenre = findGenre(id); currentGenre.setName(name); validatorUtil.validate(currentGenre); diff --git a/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java b/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java index 59abff5..f847c1d 100644 --- a/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java +++ b/src/main/java/ru/ulstu/is/lab1/WebConfiguration.java @@ -5,7 +5,7 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration -class webConfiguration implements WebMvcConfigurer { +class WebConfiguration implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry){ registry.addMapping("/**").allowedMethods("*");