ЛР4 жанры работают
This commit is contained in:
parent
49b8125146
commit
1d1b1404b0
@ -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) {
|
||||
|
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -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<GenreDTO> 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();
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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);
|
||||
|
@ -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("*");
|
||||
|
Loading…
Reference in New Issue
Block a user