ЛР4 считываются жанры для каждого фильма, но не редактируются
This commit is contained in:
parent
1d1b1404b0
commit
b07faa67c5
102
Frontend/vue-project/src/components/CatalogFilms.vue
Normal file
102
Frontend/vue-project/src/components/CatalogFilms.vue
Normal 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>
|
@ -65,6 +65,6 @@
|
||||
user-select: none;
|
||||
}
|
||||
*{
|
||||
color: white;
|
||||
color: red;
|
||||
}
|
||||
</style>
|
@ -60,4 +60,7 @@
|
||||
.modal-show {
|
||||
display: block;
|
||||
}
|
||||
.modal-content {
|
||||
width: max-content;
|
||||
}
|
||||
</style>
|
@ -9,6 +9,7 @@ import Form from './components/Form.vue'
|
||||
import Table from './components/Table.vue'
|
||||
import Catalogs from './components/Catalogs.vue'
|
||||
import CatalogGenres from './components/CatalogGenres.vue'
|
||||
import CatalogFilms from './components/CatalogFilms.vue'
|
||||
|
||||
|
||||
const routes = [
|
||||
@ -20,7 +21,8 @@ const routes = [
|
||||
{ path: '/form', component: Form},
|
||||
{ path: '/table', component: Table},
|
||||
{ path: '/catalogs', component: Catalogs},
|
||||
{ path: '/catalogs/genres', component: CatalogGenres}
|
||||
{ path: '/catalogs/genres', component: CatalogGenres},
|
||||
{ path: '/catalogs/films', component: CatalogFilms}
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
|
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -1,19 +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 ru.ulstu.is.lab1.DataBase.model.Film;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.lab1.DataBase.service.FilmService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@ -21,7 +11,6 @@ import java.util.List;
|
||||
public class FilmController {
|
||||
private final FilmService filmService;
|
||||
|
||||
@Autowired
|
||||
public FilmController(FilmService filmService) {
|
||||
this.filmService = filmService;
|
||||
}
|
||||
@ -37,41 +26,13 @@ public class FilmController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public FilmDTO createFilm(@RequestParam("name") String name, @RequestParam("GenresId") String GenresId) throws IOException {
|
||||
List<Long> ids = new ArrayList<>();
|
||||
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));
|
||||
public FilmDTO createFilm(@RequestBody @Valid FilmDTO filmDTO) {
|
||||
return new FilmDTO(filmService.addFilm(filmDTO.getName()));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public FilmDTO updateFilm(@PathVariable Long id, @RequestParam("name") String name, @RequestParam("GenresId") String GenresId) {
|
||||
List<Long> ids = new ArrayList<>();
|
||||
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));
|
||||
@PutMapping("/{id}")
|
||||
public FilmDTO updateFilm(@PathVariable Long id, @RequestBody @Valid FilmDTO filmDTO) {
|
||||
return new FilmDTO(filmService.updateFilm(id, filmDTO.getName()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@ -83,4 +44,14 @@ public class FilmController {
|
||||
public void 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));
|
||||
}
|
||||
}
|
||||
|
@ -1,23 +1,22 @@
|
||||
package ru.ulstu.is.lab1.DataBase.controller;
|
||||
|
||||
import ru.ulstu.is.lab1.DataBase.model.Film;
|
||||
import ru.ulstu.is.lab1.DataBase.model.Genre;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FilmDTO {
|
||||
private Long id;
|
||||
private String name;
|
||||
private List<GenreDTO> genreDTOList;
|
||||
private List<CollectionDTO> collectionDTOList;
|
||||
private List<Long> genreIds;
|
||||
public FilmDTO() {
|
||||
}
|
||||
public FilmDTO(Film film){
|
||||
this.id = film.getId();
|
||||
this.name = film.getName();
|
||||
this.genreDTOList = film.getGenres() == null ? null : film.getGenres()
|
||||
.stream()
|
||||
.map(GenreDTO::new)
|
||||
.toList();
|
||||
if(film.getGenres() != null) {
|
||||
this.genreIds = film.getGenres().stream().map(Genre::getId).toList();
|
||||
}
|
||||
public FilmDTO() {
|
||||
}
|
||||
public Long getId(){
|
||||
return id;
|
||||
@ -25,7 +24,7 @@ public class FilmDTO {
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public List<GenreDTO> getGenreDTOList(){
|
||||
return genreDTOList;
|
||||
public List<Long> getGenreIds(){
|
||||
return genreIds;
|
||||
}
|
||||
}
|
||||
|
@ -17,5 +17,4 @@ public class GenreDTO {
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) { this.name = name; }
|
||||
}
|
||||
|
@ -34,12 +34,10 @@ public class Genre {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Genre))
|
||||
return false;
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Genre genre = (Genre) o;
|
||||
return Objects.equals(id, genre.id) && Objects.equals(this.name, genre.name);
|
||||
return Objects.equals(id, genre.id);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
@ -6,7 +6,9 @@ import ru.ulstu.is.lab1.DataBase.Repository.IFilmRepository;
|
||||
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 javax.persistence.EntityNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@ -24,17 +26,12 @@ public class FilmService {
|
||||
}
|
||||
|
||||
@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);
|
||||
if(genresId.size() > 0 )
|
||||
{
|
||||
for (Long id: genresId) {
|
||||
Genre genre = genreService.findGenre(id);
|
||||
film.addGenre(genre);
|
||||
}
|
||||
}
|
||||
validatorUtil.validate(film);
|
||||
|
||||
return filmRepository.save(film);
|
||||
}
|
||||
|
||||
@ -50,19 +47,12 @@ public class FilmService {
|
||||
}
|
||||
|
||||
@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);
|
||||
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);
|
||||
return filmRepository.save(currentFilm);
|
||||
}
|
||||
@ -78,4 +68,40 @@ public class FilmService {
|
||||
public void deleteAllFilms() {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user