ЛР4 чекбоксы работают с бд!

This commit is contained in:
ityurner02@mail.ru 2023-05-15 23:36:37 +04:00
parent 411eb34bab
commit 04fea2fcc8
5 changed files with 46 additions and 27 deletions

View File

@ -20,9 +20,10 @@
headersGenres: [ headersGenres: [
{ name: 'name', label: 'Жанр' } { name: 'name', label: 'Жанр' }
], ],
selectedItemsGenres: [], selectedGenres: [],
genreUrl: 'genre/', genreUrl: 'genre/',
genres: [] genres: [],
open: []
} }
}, },
created() { created() {
@ -32,15 +33,21 @@
}); });
}, },
methods: { methods: {
addGenre(filmId) { addGenre(filmId, genreId) {
let genreId = document.getElementById('genres').value;
let response = axios.post(`http://localhost:8080/film/add_genre/${filmId}?genre_id=${genreId}`); let response = axios.post(`http://localhost:8080/film/add_genre/${filmId}?genre_id=${genreId}`);
console.log(response); console.log(response);
return response;
}, },
delGenre(filmId) { delGenres(filmId) {
let genreId = document.getElementById('genres').value; let response = axios.delete(`http://localhost:8080/film/del_genres/${filmId}`);
let response = axios.delete(`http://localhost:8080/film/del_genre/${filmId}?genre_id=${genreId}`);
console.log(response); console.log(response);
return response;
},
async createGenres(filmId, genreIds){
await this.delGenres(filmId);
for(var j=0; j<genreIds.length; j++){
await this.addGenre(filmId, genreIds[j]);
}
}, },
itemsGenres(genreIds) { itemsGenres(genreIds) {
let result = []; let result = [];
@ -81,26 +88,18 @@
<label for="name" class="form-label">Название фильма</label> <label for="name" class="form-label">Название фильма</label>
<input type="text" class="form-control" id="name" required v-model="data.name"> <input type="text" class="form-control" id="name" required v-model="data.name">
</div> </div>
<DataTable <div class="form-genre">
:headers="this.headersGenres" <label for="name">Выберите жанры для добавления:</label>
:items="itemsGenres(data.genreIds)" <ul class="list-genre">
:selectedItems="this.selectedItemsGenres"> <li class="list-genre-item" v-for="genre in genres" :key="genre.id">
</DataTable> <div class="form-check">
<div class="mb-3"> <input class="form-check-input" type="checkbox" v-model="selectedGenres" :value="genre.id">
<label for="genres" class="form-label">Жанры</label> <label class="form-check-label" for="genreCheck{{ genre.id }}">{{ genre.name }}</label>
<select class="form-select" id="genres" required> </div>
<option disabled value="">Выберите жанр</option> </li>
<option v-for="genre in this.genres" </ul>
:value="genre.id"> <button class="btn btn-outline-secondary" type="button" id="GenreOnFilm"
{{ genre.name }} @click="createGenres(data.id, selectedGenres)">Добавить</button>
</option>
</select>
</div>
<div class="d-flex justify-content-between">
<button class="btn btn-outline-secondary" type="button" id="addGenreButton"
@click.prevent="addGenre(data.id)">Добавить</button>
<button class="btn btn-outline-secondary" type="button" id="delGenreButton"
@click.prevent="delGenre(data.id)">Удалить</button>
</div> </div>
</Modal> </Modal>
</template> </template>

Binary file not shown.

View File

@ -54,4 +54,9 @@ public class FilmController {
public FilmDTO delGenre(@PathVariable Long id, @RequestParam Long genre_id) { public FilmDTO delGenre(@PathVariable Long id, @RequestParam Long genre_id) {
return new FilmDTO(filmService.deleteGenre(id, genre_id)); return new FilmDTO(filmService.deleteGenre(id, genre_id));
} }
@DeleteMapping("/del_genres/{id}")
public FilmDTO delGenres(@PathVariable Long id) {
return new FilmDTO(filmService.deleteGenres(id));
}
} }

View File

@ -60,6 +60,10 @@ public class Film {
} }
} }
public void removeGenres(){
genres = new ArrayList<>();
}
public void addCollection(Collection collection) { public void addCollection(Collection collection) {
if(this.collections==null) if(this.collections==null)
collections=new ArrayList<>(); collections=new ArrayList<>();

View File

@ -97,4 +97,15 @@ public class FilmService {
film.removeGenre(genre); film.removeGenre(genre);
return filmRepository.save(film); return filmRepository.save(film);
} }
@Transactional
public Film deleteGenres(Long filmId) {
Film film = findFilm(filmId);
if (film == null) {
throw new EntityNotFoundException(String.format("Film with id [%s] is not found", filmId));
}
film.removeGenres();
return filmRepository.save(film);
}
} }