ЛР4 жанры работают

This commit is contained in:
ityurner02@mail.ru 2023-05-01 17:36:10 +04:00
parent 49b8125146
commit 1d1b1404b0
7 changed files with 27 additions and 26 deletions

View File

@ -82,7 +82,7 @@ const CatalogMixin = {
const promises = []; const promises = [];
const self = this; const self = this;
this.selectedItems.forEach(item => { this.selectedItems.forEach(item => {
promises.push(DataService.delete(this.dataUrl + item)); promises.push(DataService.delete(this.dataUrl + "/" + item));
}); });
Promise.all(promises).then((results) => { Promise.all(promises).then((results) => {
results.forEach(function (id) { results.forEach(function (id) {

Binary file not shown.

View File

@ -1,16 +1,9 @@
package ru.ulstu.is.lab1.DataBase.controller; package ru.ulstu.is.lab1.DataBase.controller;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
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 ru.ulstu.is.lab1.DataBase.service.GenreService; import ru.ulstu.is.lab1.DataBase.service.GenreService;
import javax.validation.Valid;
import java.util.List; import java.util.List;
@RestController @RestController
@ -18,7 +11,6 @@ import java.util.List;
public class GenreController { public class GenreController {
private final GenreService genreService; private final GenreService genreService;
@Autowired
public GenreController(GenreService genreService) { public GenreController(GenreService genreService) {
this.genreService = genreService; this.genreService = genreService;
} }
@ -28,19 +20,19 @@ public class GenreController {
return new GenreDTO(genreService.findGenre(id)); return new GenreDTO(genreService.findGenre(id));
} }
@GetMapping @GetMapping("/")
public List<GenreDTO> getGenre() { public List<GenreDTO> getGenre() {
return genreService.findAllGenres().stream().map(GenreDTO::new).toList(); return genreService.findAllGenres().stream().map(GenreDTO::new).toList();
} }
@PostMapping @PostMapping("/")
public GenreDTO createGenre(@RequestParam("name") String name) { public GenreDTO createGenre(@RequestBody @Valid GenreDTO genreDTO) {
return new GenreDTO(genreService.addGenre(name)); return new GenreDTO(genreService.addGenre(genreDTO.getName()));
} }
@PatchMapping("/{id}") @PutMapping("/{id}")
public GenreDTO updateGenre(@PathVariable Long id, @RequestParam("name") String name) { public GenreDTO updateGenre(@PathVariable Long id, @RequestBody @Valid GenreDTO genreDTO) {
return new GenreDTO(genreService.updateGenre(id, name)); return new GenreDTO(genreService.updateGenre(id, genreDTO.getName()));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
@ -48,7 +40,7 @@ public class GenreController {
return new GenreDTO(genreService.deleteGenre(id)); return new GenreDTO(genreService.deleteGenre(id));
} }
@DeleteMapping @DeleteMapping("/")
public void deleteAllGenres(){ public void deleteAllGenres(){
genreService.deleteAllGenres(); genreService.deleteAllGenres();
} }

View File

@ -5,16 +5,17 @@ import ru.ulstu.is.lab1.DataBase.model.Genre;
public class GenreDTO { public class GenreDTO {
private Long id; private Long id;
private String name; private String name;
public GenreDTO() {
}
public GenreDTO(Genre genre){ public GenreDTO(Genre genre){
this.id = genre.getId(); this.id = genre.getId();
this.name = genre.getName(); this.name = genre.getName();
} }
public GenreDTO() {
}
public Long getId(){ public Long getId(){
return id; return id;
} }
public String getName(){ public String getName(){
return name; return name;
} }
public void setName(String name) { this.name = name; }
} }

View File

@ -3,7 +3,6 @@ package ru.ulstu.is.lab1.DataBase.model;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects; import java.util.Objects;
@Entity @Entity
@ -35,10 +34,12 @@ public class Genre {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o)
if (o == null || getClass() != o.getClass()) return false; return true;
if (!(o instanceof Genre))
return false;
Genre genre = (Genre) o; Genre genre = (Genre) o;
return Objects.equals(id, genre.id); return Objects.equals(id, genre.id) && Objects.equals(this.name, genre.name);
} }
@Override @Override
public int hashCode() { public int hashCode() {

View File

@ -6,6 +6,7 @@ import ru.ulstu.is.lab1.DataBase.Repository.IGenreRepository;
import ru.ulstu.is.lab1.util.validation.ValidatorUtil; import ru.ulstu.is.lab1.util.validation.ValidatorUtil;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -21,6 +22,9 @@ public class GenreService {
@Transactional @Transactional
public Genre addGenre(String name) { public Genre addGenre(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Genre name is null or empty");
}
final Genre genre = new Genre(name); final Genre genre = new Genre(name);
validatorUtil.validate(genre); validatorUtil.validate(genre);
return genreRepository.save(genre); return genreRepository.save(genre);
@ -39,6 +43,9 @@ public class GenreService {
@Transactional @Transactional
public Genre updateGenre(Long id, String name) { 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); final Genre currentGenre = findGenre(id);
currentGenre.setName(name); currentGenre.setName(name);
validatorUtil.validate(currentGenre); validatorUtil.validate(currentGenre);

View File

@ -5,7 +5,7 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration @Configuration
class webConfiguration implements WebMvcConfigurer { class WebConfiguration implements WebMvcConfigurer {
@Override @Override
public void addCorsMappings(CorsRegistry registry){ public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**").allowedMethods("*"); registry.addMapping("/**").allowedMethods("*");