ЛР4 все работает

This commit is contained in:
ityurner02@mail.ru 2023-05-04 21:57:42 +04:00
parent 361d0a65ff
commit 3d47fb12ef
9 changed files with 179 additions and 82 deletions

View File

@ -0,0 +1,106 @@
<script>
import axios from 'axios';
import CatalogMixins from '../mixins/CatalogMixins.js';
import Collection from "../models/Collection";
import Film from "../models/Film";
import DataService from '../services/DataService';
export default {
mixins: [
CatalogMixins
],
data() {
return {
getAllUrl: 'collection/',
dataUrl: 'collection',
transformer: (data) => new Collection(data),
headers: [
{ name: 'name', label: 'Название' }
],
headersFilms: [
{ name: 'name', label: 'Фильм' }
],
selectedItemsFilms: [],
genreUrl: 'film/',
films: []
}
},
created() {
DataService.readAll(this.genreUrl, (data) => new Film(data))
.then(data => {
this.films = data;
});
},
methods: {
addFilm(collectionId) {
let filmId = document.getElementById('films').value;
let response = axios.post(`http://localhost:8080/collection/add_film/${collectionId}?film_id=${filmId}`);
console.log(response);
},
delFilm(collectionId) {
let filmId = document.getElementById('films').value;
let response = axios.delete(`http://localhost:8080/collection/del_film/${collectionId}?film_id=${filmId}`);
console.log(response);
},
itemsFilms(filmIds) {
let result = [];
if (typeof filmIds === 'undefined') {
return;
}
this.films.forEach(film => {
for (let i = 0; i < filmIds.length; i++) {
if (film.id === filmIds[i]) {
result.push(film);
}
}
});
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.headersFilms"
:items="itemsFilms(data.filmIds)"
:selectedItems="this.selectedItemsFilms">
</DataTable>
<div class="mb-3">
<label for="films" class="form-label">Фильмы</label>
<select class="form-select" id="films" required>
<option disabled value="">Выберите фильм</option>
<option v-for="film in this.films"
:value="film.id">
{{ film.name }}
</option>
</select>
</div>
<div class="d-flex justify-content-between">
<button class="btn btn-outline-secondary" type="button" id="addFilmButton"
@click.prevent="addFilm(data.id)">Добавить</button>
<button class="btn btn-outline-secondary" type="button" id="delFilmButton"
@click.prevent="delFilm(data.id)">Удалить</button>
</div>
</Modal>
</template>

View File

@ -3,9 +3,9 @@
data() { data() {
return { return {
catalogs: [ catalogs: [
{ name: 'collections', label: 'Сборники' }, { name: 'genres', label: 'Жанры' },
{ name: 'films', label: 'Фильмы' }, { name: 'films', label: 'Фильмы' },
{ name: 'genres', label: 'Жанры' } { name: 'collections', label: 'Коллекции' }
] ]
} }
} }

View File

@ -10,7 +10,7 @@ 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' import CatalogFilms from './components/CatalogFilms.vue'
import CatalogCollections from './components/CatalogCollections.vue'
const routes = [ const routes = [
{ path: '/', redirect: '/index' }, { path: '/', redirect: '/index' },
@ -22,7 +22,8 @@ const routes = [
{ 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} { path: '/catalogs/films', component: CatalogFilms},
{ path: '/catalogs/collections', component: CatalogCollections}
] ]
const router = createRouter({ const router = createRouter({

View File

@ -21,6 +21,6 @@ export default class Collection {
} }
get filmIds() { get filmIds() {
return this.filmIds; return this._filmIds;
} }
} }

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.Collection;
import ru.ulstu.is.lab1.DataBase.service.CollectionService; import ru.ulstu.is.lab1.DataBase.service.CollectionService;
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 CollectionController { public class CollectionController {
private final CollectionService collectionService; private final CollectionService collectionService;
@Autowired
public CollectionController(CollectionService collectionService) { public CollectionController(CollectionService collectionService) {
this.collectionService = collectionService; this.collectionService = collectionService;
} }
@ -37,41 +26,13 @@ public class CollectionController {
} }
@PostMapping @PostMapping
public CollectionDTO createCollection(@RequestParam("name") String name, @RequestParam("FilmsId") String FilmsId) throws IOException { public CollectionDTO createCollection(@RequestBody @Valid CollectionDTO collectionDTO) {
List<Long> ids = new ArrayList<>(); return new CollectionDTO(collectionService.addCollection(collectionDTO.getName()));
String num = "";
if(FilmsId.length() == 0)
FilmsId = "";
for (Character sm:
FilmsId.toCharArray()) {
if(sm.equals('-'))
{
ids.add(Long.parseLong(num));
num="";
}
else
num+=sm;
}
return new CollectionDTO(collectionService.addCollection(name, ids));
} }
@PatchMapping("/{id}") @PutMapping("/{id}")
public CollectionDTO updateCollection(@PathVariable Long id, @RequestParam("name") String name, @RequestParam("FilmsId") String FilmsId) { public CollectionDTO updateCollection(@PathVariable Long id, @RequestBody @Valid CollectionDTO collectionDTO) {
List<Long> ids = new ArrayList<>(); return new CollectionDTO(collectionService.updateCollection(id, collectionDTO.getName()));
String num = "";
if(FilmsId.length() == 0)
FilmsId = "";
for (Character sm:
FilmsId.toCharArray()) {
if(sm.equals('-'))
{
ids.add(Long.parseLong(num));
num="";
}
else
num+=sm;
}
return new CollectionDTO(collectionService.updateCollection(id, name, ids));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
@ -82,4 +43,14 @@ public class CollectionController {
public void deleteAllCollections(){ public void deleteAllCollections(){
collectionService.deleteAllCollections(); collectionService.deleteAllCollections();
} }
@PostMapping("/add_film/{id}")
public CollectionDTO addFilm(@PathVariable Long id, @RequestParam Long film_id) {
return new CollectionDTO(collectionService.addFilm(id, film_id));
}
@DeleteMapping("/del_film/{id}")
public CollectionDTO delFilm(@PathVariable Long id, @RequestParam Long film_id) {
return new CollectionDTO(collectionService.deleteFilm(id, film_id));
}
} }

View File

@ -1,22 +1,22 @@
package ru.ulstu.is.lab1.DataBase.controller; package ru.ulstu.is.lab1.DataBase.controller;
import ru.ulstu.is.lab1.DataBase.model.Collection; import ru.ulstu.is.lab1.DataBase.model.Collection;
import ru.ulstu.is.lab1.DataBase.model.Film;
import java.util.List; import java.util.List;
public class CollectionDTO { public class CollectionDTO {
private Long id; private Long id;
private String name; private String name;
private List<FilmDTO> filmDTOList; private List<Long> filmIds;
public CollectionDTO() {
}
public CollectionDTO(Collection collection){ public CollectionDTO(Collection collection){
this.id = collection.getId(); this.id = collection.getId();
this.name = collection.getName(); this.name = collection.getName();
this.filmDTOList = collection.getFilms() == null ? null : collection.getFilms() if(collection.getFilms() != null) {
.stream() this.filmIds = collection.getFilms().stream().map(Film::getId).toList();
.map(FilmDTO::new) }
.toList();
}
public CollectionDTO() {
} }
public Long getId(){ public Long getId(){
return id; return id;
@ -24,7 +24,7 @@ public class CollectionDTO {
public String getName(){ public String getName(){
return name; return name;
} }
public List<FilmDTO> getFilmDTOList(){ public List<Long> getFilmIds(){
return filmDTOList; return filmIds;
} }
} }

View File

@ -1,13 +1,15 @@
package ru.ulstu.is.lab1.DataBase.service; package ru.ulstu.is.lab1.DataBase.service;
import org.springframework.util.StringUtils;
import ru.ulstu.is.lab1.DataBase.model.Film; import ru.ulstu.is.lab1.DataBase.model.Film;
import ru.ulstu.is.lab1.DataBase.model.Collection; import ru.ulstu.is.lab1.DataBase.model.Collection;
import ru.ulstu.is.lab1.DataBase.Repository.ICollectionRepository; import ru.ulstu.is.lab1.DataBase.Repository.ICollectionRepository;
import ru.ulstu.is.lab1.DataBase.model.Genre;
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 java.io.IOException; import javax.persistence.EntityNotFoundException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -23,17 +25,12 @@ public class CollectionService {
this.filmService = filmService; this.filmService = filmService;
} }
@Transactional @Transactional
public Collection addCollection(String name, List<Long> filmsId) throws IOException { public Collection addCollection(String name) {
final Collection collection = new Collection(name); if (!StringUtils.hasText(name)) {
if(filmsId.size() > 0 ) throw new IllegalArgumentException("Collection name is null or empty");
{
for (Long id: filmsId) {
Film film = filmService.findFilm(id);
collection.addFilm(film);
}
} }
final Collection collection = new Collection(name);
validatorUtil.validate(collection); validatorUtil.validate(collection);
return collectionRepository.save(collection); return collectionRepository.save(collection);
} }
@ -49,19 +46,12 @@ public class CollectionService {
} }
@Transactional @Transactional
public Collection updateCollection(Long id, String name, List<Long> filmsId) { public Collection updateCollection(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Collection name is null or empty");
}
final Collection currentCollection = findCollection(id); final Collection currentCollection = findCollection(id);
currentCollection.setName(name); currentCollection.setName(name);
if(filmsId.size()>0)
{
currentCollection.getFilms().clear();
for (Long filmId: filmsId) {
Film film = filmService.findFilm(filmId);
currentCollection.addFilm(film);
}
}
else
currentCollection.getFilms().clear();
validatorUtil.validate(currentCollection); validatorUtil.validate(currentCollection);
return collectionRepository.save(currentCollection); return collectionRepository.save(currentCollection);
} }
@ -77,4 +67,34 @@ public class CollectionService {
public void deleteAllCollections() { public void deleteAllCollections() {
collectionRepository.deleteAll(); collectionRepository.deleteAll();
} }
@Transactional
public Collection addFilm(Long collectionId, Long filmId) {
Collection collection = findCollection(collectionId);
if (collection == null) {
throw new EntityNotFoundException(String.format("Collection with id [%s] is not found", collectionId));
}
final Film film = filmService.findFilm(filmId);
if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
collection.addFilm(film);
return collectionRepository.save(collection);
}
@Transactional
public Collection deleteFilm(Long collectionId, Long filmId) {
Collection collection = findCollection(collectionId);
if (collection == null) {
throw new EntityNotFoundException(String.format("Collection with id [%s] is not found", collectionId));
}
final Film film = filmService.findFilm(filmId);
if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
collection.removeFilm(film);
return collectionRepository.save(collection);
}
} }

View File

@ -9,7 +9,6 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;