ЛР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() {
return {
catalogs: [
{ name: 'collections', label: 'Сборники' },
{ name: 'genres', 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 CatalogGenres from './components/CatalogGenres.vue'
import CatalogFilms from './components/CatalogFilms.vue'
import CatalogCollections from './components/CatalogCollections.vue'
const routes = [
{ path: '/', redirect: '/index' },
@ -22,7 +22,8 @@ const routes = [
{ path: '/table', component: Table},
{ path: '/catalogs', component: Catalogs},
{ path: '/catalogs/genres', component: CatalogGenres},
{ path: '/catalogs/films', component: CatalogFilms}
{ path: '/catalogs/films', component: CatalogFilms},
{ path: '/catalogs/collections', component: CatalogCollections}
]
const router = createRouter({

View File

@ -21,6 +21,6 @@ export default class Collection {
}
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;
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.Collection;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.lab1.DataBase.service.CollectionService;
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 CollectionController {
private final CollectionService collectionService;
@Autowired
public CollectionController(CollectionService collectionService) {
this.collectionService = collectionService;
}
@ -37,41 +26,13 @@ public class CollectionController {
}
@PostMapping
public CollectionDTO createCollection(@RequestParam("name") String name, @RequestParam("FilmsId") String FilmsId) throws IOException {
List<Long> ids = new ArrayList<>();
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));
public CollectionDTO createCollection(@RequestBody @Valid CollectionDTO collectionDTO) {
return new CollectionDTO(collectionService.addCollection(collectionDTO.getName()));
}
@PatchMapping("/{id}")
public CollectionDTO updateCollection(@PathVariable Long id, @RequestParam("name") String name, @RequestParam("FilmsId") String FilmsId) {
List<Long> ids = new ArrayList<>();
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));
@PutMapping("/{id}")
public CollectionDTO updateCollection(@PathVariable Long id, @RequestBody @Valid CollectionDTO collectionDTO) {
return new CollectionDTO(collectionService.updateCollection(id, collectionDTO.getName()));
}
@DeleteMapping("/{id}")
@ -82,4 +43,14 @@ public class CollectionController {
public void 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;
import ru.ulstu.is.lab1.DataBase.model.Collection;
import ru.ulstu.is.lab1.DataBase.model.Film;
import java.util.List;
public class CollectionDTO {
private Long id;
private String name;
private List<FilmDTO> filmDTOList;
private List<Long> filmIds;
public CollectionDTO() {
}
public CollectionDTO(Collection collection){
this.id = collection.getId();
this.name = collection.getName();
this.filmDTOList = collection.getFilms() == null ? null : collection.getFilms()
.stream()
.map(FilmDTO::new)
.toList();
if(collection.getFilms() != null) {
this.filmIds = collection.getFilms().stream().map(Film::getId).toList();
}
public CollectionDTO() {
}
public Long getId(){
return id;
@ -24,7 +24,7 @@ public class CollectionDTO {
public String getName(){
return name;
}
public List<FilmDTO> getFilmDTOList(){
return filmDTOList;
public List<Long> getFilmIds(){
return filmIds;
}
}

View File

@ -1,13 +1,15 @@
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.Collection;
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 org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Optional;
@ -23,17 +25,12 @@ public class CollectionService {
this.filmService = filmService;
}
@Transactional
public Collection addCollection(String name, List<Long> filmsId) throws IOException {
public Collection addCollection(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Collection name is null or empty");
}
final Collection collection = new Collection(name);
if(filmsId.size() > 0 )
{
for (Long id: filmsId) {
Film film = filmService.findFilm(id);
collection.addFilm(film);
}
}
validatorUtil.validate(collection);
return collectionRepository.save(collection);
}
@ -49,19 +46,12 @@ public class CollectionService {
}
@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);
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);
return collectionRepository.save(currentCollection);
}
@ -77,4 +67,34 @@ public class CollectionService {
public void deleteAllCollections() {
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 javax.persistence.EntityNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Optional;