ЛР4 считываются жанры для каждого фильма, но не редактируются

This commit is contained in:
ityurner02@mail.ru 2023-05-03 22:18:30 +04:00
parent 1d1b1404b0
commit b07faa67c5
10 changed files with 187 additions and 87 deletions

View File

@ -0,0 +1,102 @@
<script>
import axios from 'axios';
import CatalogMixins from '../mixins/CatalogMixins.js';
import Film from "../models/Film";
import Genre from "../models/Genre";
import DataService from '../services/DataService';
export default {
mixins: [
CatalogMixins
],
data() {
return {
getAllUrl: 'film/',
dataUrl: 'film',
transformer: (data) => new Film(data),
headers: [
{ name: 'name', label: 'Название' }
],
headersGenres: [
{ name: 'name', label: 'Жанр' }
],
selectedItemsGenres: [],
genreUrl: 'genre/',
genres: []
}
},
created() {
DataService.readAll(this.genreUrl, (data) => new Genre(data))
.then(data => {
this.genres = data;
});
},
methods: {
addGenre(filmId) {
let genreId = document.getElementById('genre').value;
let response = axios.post(`http://localhost:8080/film/add_genre/${filmId}`);
console.log(response);
},
delGenre(filmId) {
let genreId = document.getElementById('genre').value;
let response = axios.delete(`http://localhost:8080/film/del_genre/${filmId}`);
console.log(response);
},
itemsGenres(genreIds) {
let result = [];
if (typeof genreIds === 'undefined') {
return;
}
this.genres.forEach(genre => {
for (let i = 0; i < genreIds.length; i++) {
if (genre.id === genreIds[i]) {
result.push(genre);
}
}
});
return result;
}
}
}
</script>
<template>
<ToolBar
@add="showAddModal"
@edit="showEditModal"
@remove="removeSelectedItems">
</ToolBar>
<DataTable
:headers="this.headers"
:items="this.items"
:selectedItems="this.selectedItems"
@dblclick="showEditModalDblClick">
</DataTable>
<Modal
:header="this.modal.header"
:confirm="this.modal.confirm"
v-model:visible="this.modalShow"
@done="saveItem">
<div class="mb-3">
<label for="name" class="form-label">Название фильма</label>
<input type="text" class="form-control" id="name" required v-model="data.name">
</div>
<DataTable
:headers="this.headersGenres"
:items="itemsGenres(data.genreIds)"
:selectedItems="this.selectedItemsGenres">
</DataTable>
<div class="mb-3">
<label for="genres" class="form-label">Добавить жанр</label>
<select class="form-select" id="genres" required>
<option disabled value="">Выберите жанр</option>
<option v-for="genre in this.genres"
:value="genre.id">
{{ genre.name }}
</option>
</select>
</div>
<button class="btn btn-outline-secondary" type="button" id="addGenreButton"
@click.prevent="addGenre(data.id)">Добавить</button>
</Modal>
</template>

View File

@ -65,6 +65,6 @@
user-select: none; user-select: none;
} }
*{ *{
color: white; color: red;
} }
</style> </style>

View File

@ -60,4 +60,7 @@
.modal-show { .modal-show {
display: block; display: block;
} }
.modal-content {
width: max-content;
}
</style> </style>

View File

@ -9,6 +9,7 @@ import Form from './components/Form.vue'
import Table from './components/Table.vue' import Table from './components/Table.vue'
import Catalogs from './components/Catalogs.vue' import Catalogs from './components/Catalogs.vue'
import CatalogGenres from './components/CatalogGenres.vue' import CatalogGenres from './components/CatalogGenres.vue'
import CatalogFilms from './components/CatalogFilms.vue'
const routes = [ const routes = [
@ -20,7 +21,8 @@ const routes = [
{ path: '/form', component: Form}, { path: '/form', component: Form},
{ path: '/table', component: Table}, { path: '/table', component: Table},
{ path: '/catalogs', component: Catalogs}, { path: '/catalogs', component: Catalogs},
{ path: '/catalogs/genres', component: CatalogGenres} { path: '/catalogs/genres', component: CatalogGenres},
{ path: '/catalogs/films', component: CatalogFilms}
] ]
const router = createRouter({ const router = createRouter({

Binary file not shown.

View File

@ -1,19 +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.model.Film;
import ru.ulstu.is.lab1.DataBase.service.FilmService; import ru.ulstu.is.lab1.DataBase.service.FilmService;
import java.io.IOException; import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@ -21,7 +11,6 @@ import java.util.List;
public class FilmController { public class FilmController {
private final FilmService filmService; private final FilmService filmService;
@Autowired
public FilmController(FilmService filmService) { public FilmController(FilmService filmService) {
this.filmService = filmService; this.filmService = filmService;
} }
@ -37,41 +26,13 @@ public class FilmController {
} }
@PostMapping @PostMapping
public FilmDTO createFilm(@RequestParam("name") String name, @RequestParam("GenresId") String GenresId) throws IOException { public FilmDTO createFilm(@RequestBody @Valid FilmDTO filmDTO) {
List<Long> ids = new ArrayList<>(); return new FilmDTO(filmService.addFilm(filmDTO.getName()));
String num = "";
if(GenresId.length() == 0)
GenresId = "";
for (Character sm:
GenresId.toCharArray()) {
if(sm.equals('-'))
{
ids.add(Long.parseLong(num));
num="";
}
else
num+=sm;
}
return new FilmDTO(filmService.addFilm(name, ids));
} }
@PatchMapping("/{id}") @PutMapping("/{id}")
public FilmDTO updateFilm(@PathVariable Long id, @RequestParam("name") String name, @RequestParam("GenresId") String GenresId) { public FilmDTO updateFilm(@PathVariable Long id, @RequestBody @Valid FilmDTO filmDTO) {
List<Long> ids = new ArrayList<>(); return new FilmDTO(filmService.updateFilm(id, filmDTO.getName()));
String num = "";
if(GenresId.length() == 0)
GenresId = "";
for (Character sm:
GenresId.toCharArray()) {
if(sm.equals('-'))
{
ids.add(Long.parseLong(num));
num="";
}
else
num+=sm;
}
return new FilmDTO(filmService.updateFilm(id, name, ids));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
@ -83,4 +44,14 @@ public class FilmController {
public void deleteAllFilms(){ public void deleteAllFilms(){
filmService.deleteAllFilms(); filmService.deleteAllFilms();
} }
@PostMapping("/add_genre/{id}")
public FilmDTO addGenre(@PathVariable Long id, @RequestParam Long genre_id) {
return new FilmDTO(filmService.addGenre(id, genre_id));
}
@DeleteMapping("/del_genre/{id}")
public FilmDTO delGenre(@PathVariable Long id, @RequestParam Long genre_id) {
return new FilmDTO(filmService.deleteGenre(id, genre_id));
}
} }

View File

@ -1,23 +1,22 @@
package ru.ulstu.is.lab1.DataBase.controller; package ru.ulstu.is.lab1.DataBase.controller;
import ru.ulstu.is.lab1.DataBase.model.Film; import ru.ulstu.is.lab1.DataBase.model.Film;
import ru.ulstu.is.lab1.DataBase.model.Genre;
import java.util.List; import java.util.List;
public class FilmDTO { public class FilmDTO {
private Long id; private Long id;
private String name; private String name;
private List<GenreDTO> genreDTOList; private List<Long> genreIds;
private List<CollectionDTO> collectionDTOList; public FilmDTO() {
}
public FilmDTO(Film film){ public FilmDTO(Film film){
this.id = film.getId(); this.id = film.getId();
this.name = film.getName(); this.name = film.getName();
this.genreDTOList = film.getGenres() == null ? null : film.getGenres() if(film.getGenres() != null) {
.stream() this.genreIds = film.getGenres().stream().map(Genre::getId).toList();
.map(GenreDTO::new)
.toList();
} }
public FilmDTO() {
} }
public Long getId(){ public Long getId(){
return id; return id;
@ -25,7 +24,7 @@ public class FilmDTO {
public String getName(){ public String getName(){
return name; return name;
} }
public List<GenreDTO> getGenreDTOList(){ public List<Long> getGenreIds(){
return genreDTOList; return genreIds;
} }
} }

View File

@ -17,5 +17,4 @@ public class GenreDTO {
public String getName(){ public String getName(){
return name; return name;
} }
public void setName(String name) { this.name = name; }
} }

View File

@ -34,12 +34,10 @@ public class Genre {
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) if (this == o) return true;
return true; if (o == null || getClass() != o.getClass()) return false;
if (!(o instanceof Genre))
return false;
Genre genre = (Genre) o; Genre genre = (Genre) o;
return Objects.equals(id, genre.id) && Objects.equals(this.name, genre.name); return Objects.equals(id, genre.id);
} }
@Override @Override
public int hashCode() { public int hashCode() {

View File

@ -6,7 +6,9 @@ import ru.ulstu.is.lab1.DataBase.Repository.IFilmRepository;
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 javax.persistence.EntityNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -24,17 +26,12 @@ public class FilmService {
} }
@Transactional @Transactional
public Film addFilm(String name, List<Long> genresId) throws IOException { public Film addFilm(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Film name is null or empty");
}
final Film film = new Film(name); final Film film = new Film(name);
if(genresId.size() > 0 )
{
for (Long id: genresId) {
Genre genre = genreService.findGenre(id);
film.addGenre(genre);
}
}
validatorUtil.validate(film); validatorUtil.validate(film);
return filmRepository.save(film); return filmRepository.save(film);
} }
@ -50,19 +47,12 @@ public class FilmService {
} }
@Transactional @Transactional
public Film updateFilm(Long id, String name, List<Long> genresId) { public Film updateFilm(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Film name is null or empty");
}
final Film currentFilm = findFilm(id); final Film currentFilm = findFilm(id);
currentFilm.setName(name); currentFilm.setName(name);
if(genresId.size()>0)
{
currentFilm.getGenres().clear();
for (Long genreId: genresId) {
Genre genre = genreService.findGenre(genreId);
currentFilm.addGenre(genre);
}
}
else
currentFilm.getGenres().clear();
validatorUtil.validate(currentFilm); validatorUtil.validate(currentFilm);
return filmRepository.save(currentFilm); return filmRepository.save(currentFilm);
} }
@ -78,4 +68,40 @@ public class FilmService {
public void deleteAllFilms() { public void deleteAllFilms() {
filmRepository.deleteAll(); filmRepository.deleteAll();
} }
@Transactional
public Film addGenre(Long filmId, Long genreId) {
final Optional<Film> filmOpt = filmRepository.findById(filmId);
if (filmOpt.isEmpty()) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
Film film = filmOpt.get();
final Genre genre = genreService.findGenre(genreId);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId));
}
film.addGenre(genre);
return filmRepository.save(film);
}
@Transactional
public Film deleteGenre(Long filmId, Long genreId) {
final Optional<Film> filmOpt = filmRepository.findById(filmId);
if (filmOpt.isEmpty()) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
Film film = filmOpt.get();
final Genre genre = genreService.findGenre(genreId);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId));
}
film.removeGenre(genre);
return filmRepository.save(film);
}
} }