lab5 full

This commit is contained in:
parap 2023-05-02 09:10:59 +04:00
parent e4c17f1642
commit c793d802ef
7 changed files with 133 additions and 18 deletions

Binary file not shown.

View File

@ -32,7 +32,7 @@ public class ActorController {
return new ActorDTO(actorService.addActor(actor.getName(), actor.getSurname()));
}
@PatchMapping("")
@PatchMapping("/{id}")
public ActorDTO updateActor(@PathVariable Long id, @RequestBody @Valid ActorDTO actor) {
return new ActorDTO(actorService.updateActor(id, actor.getName(), actor.getSurname()));
}

View File

@ -49,7 +49,7 @@ public class FilmMvcController {
model.addAttribute("errors", bindingResult.getAllErrors());
return "films-catalog";
}
Long filmId = filmDTO.getId();
Long filmId = id;
if (id == null || id <= 0) {
Film result = filmService.addFilm(filmDTO.getName());

View File

@ -16,7 +16,6 @@ public class ActorDTO {
private String name;
private String surname;
private String fullName;
private byte[] photo;
private List<Film> films;
@ -26,7 +25,6 @@ public class ActorDTO {
this.id = actor.getId();
this.name = actor.getName();
this.surname = actor.getSurname();
this.photo = actor.getPhoto();
this.fullName = this.name + this.surname;
}
public ActorDTO(Long id, String name, String surname, byte[] photo) {
@ -34,16 +32,6 @@ public class ActorDTO {
this.name = name;
this.surname = surname;
this.fullName = this.name + this.surname;
this.photo = photo;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(String path) throws IOException {
File f = new File(path);
photo = Files.readAllBytes(f.toPath());
}
public Long getId() {

View File

@ -78,6 +78,9 @@
editForm.action = "/actor";
editForm.previousElementSibling.children[0].textContent = "Добавление";
editForm.children[1].children[1].textContent = "Добавить";
editForm.name.value = "";
editForm.surname.value = "";
}
editBtn.onclick = () => {
@ -92,6 +95,7 @@
editForm.previousElementSibling.children[0].textContent = "Изменение";
submitBtn.textContent = "Изменить";
editForm.name.value = elem.children[1].textContent;
editForm.surname.value = elem.children[2].textContent;
}

View File

@ -44,12 +44,34 @@
<label htmlFor="nameInput" class="form-label">Название</label>
<input id="nameInput" type="text" th:field="${filmDTO.name}" name="name" class="form-control" required autoComplete="off">
</div>
<select name="genre" class="form-select" th:field="${filmDTO.genre}" multiple="multiple">
<select name="selectedGenre" class="form-select">
<option disabled value="">Укажите жанр</option>
<option th:each="g : ${allGenres}"
th:value="${g}"
th:text="${g}"></option>
</select>
<select name="fullNames" class="form-select" th:field="${filmDTO.fullNames}" multiple="multiple">
<div id="selectedGenres">
</div>
<br>
<button type="button" class="btn btn-success" id="addGenre">Добавить жанр</button>
<select name="selectedActor" class="form-select">
<option disabled value="">Укажите актера</option>
<option th:each="a : ${allActors}"
th:value="${a}"
th:text="${a}"></option>
</select>
<div id="selectedActors">
</div>
<br>
<button type="button" class="btn btn-success" id="addActor">Добавить актера</button>
<select name="genre" class="form-select" th:field="${filmDTO.genre}" multiple="multiple" hidden>
<option th:each="g : ${allGenres}"
th:value="${g}"
th:text="${g}"></option>
</select>
<select name="fullNames" class="form-select" th:field="${filmDTO.fullNames}" multiple="multiple" hidden>
<option th:each="a : ${allActors}"
th:value="${a}"
th:text="${a}"></option>
@ -74,6 +96,8 @@
let editBtn = document.getElementById("editBtn");
let submitBtn = document.getElementById("submitBtn");
let closeBtn = document.getElementById("closeBtn");
let addGenreBtn = document.getElementById("addGenre");
let addActorBtn = document.getElementById("addActor");
let editForm = document.getElementById("edit-form");
let deleteForm = document.getElementById("delete-form");
@ -84,7 +108,19 @@
addBtn.onclick = (e) => {
editForm.action = "/film";
editForm.previousElementSibling.children[0].textContent = "Добавление";
editForm.children[1].children[1].textContent = "Добавить";
submitBtn.textContent = "Добавить";
editForm.name.value = "";
document.getElementById("selectedGenres").innerHTML = "";
for(let opt of editForm.genre.options) {
opt.selected = false;
}
document.getElementById("selectedActors").innerHTML = "";
for(let opt of editForm.fullNames.options) {
opt.selected = false;
}
}
editBtn.onclick = (e) => {
@ -101,8 +137,44 @@
editForm.name.value = elem.children[1].textContent;
let genresArr = elem.children[2].textContent.split(", ");
document.getElementById("selectedGenres").innerHTML = "";
for(let opt of editForm.genre.options) {
if(genresArr.indexOf(opt.value) != -1) opt.selected = true;
if(genresArr.indexOf(opt.value) == -1) continue;
let budge = document.createElement("div");
budge.className = "badge bg-secondary m-1";
budge.innerHTML = `
<span>${opt.value}</span>
<button type="button" class="btn-close bg-danger m-1"></button>
`;
budge.children[1].onclick = () => {
budge.remove();
opt.selected = false;
}
document.getElementById("selectedGenres").appendChild(budge);
opt.selected = true;
}
let actorsArr = elem.children[3].textContent.split(", ");
document.getElementById("selectedActors").innerHTML = "";
for(let opt of editForm.fullNames.options) {
if(actorsArr.indexOf(opt.value) == -1) continue;
let budge = document.createElement("div");
budge.className = "badge bg-secondary m-1";
budge.innerHTML = `
<span>${opt.value}</span>
<button type="button" class="btn-close bg-danger m-1"></button>
`;
budge.children[1].onclick = () => {
budge.remove();
opt.selected = false;
}
document.getElementById("selectedActors").appendChild(budge);
opt.selected = true;
}
}
deleteBtn.onclick = () => {
@ -114,6 +186,56 @@
deleteForm.action = "/film/delete/" + elem.id;
deleteForm.submit();
}
addGenreBtn.onclick = () => {
let selectedIndex = editForm.selectedGenre.selectedIndex;
let g = editForm.selectedGenre.options[selectedIndex].value;
for(let elem of editForm.genre.options) {
if(g != elem.value) continue;
if(elem.selected) return;
elem.selected = true;
let budge = document.createElement("div");
budge.className = "badge bg-secondary m-1";
budge.innerHTML = `
<span>${g}</span>
<button type="button" class="btn-close bg-danger m-1"></button>
`;
budge.children[1].onclick = () => {
budge.remove();
elem.selected = false;
}
document.getElementById("selectedGenres").appendChild(budge);
}
}
addActorBtn.onclick = () => {
let selectedIndex = editForm.selectedActor.selectedIndex;
let a = editForm.selectedActor.options[selectedIndex].value;
for(let elem of editForm.fullNames.options) {
if(a != elem.value) continue;
if(elem.selected) return;
elem.selected = true;
let budge = document.createElement("div");
budge.className = "badge bg-secondary m-1";
budge.innerHTML = `
<span>${a}</span>
<button type="button" class="btn-close bg-danger m-1"></button>
`;
budge.children[1].onclick = () => {
budge.remove();
elem.selected = false;
}
document.getElementById("selectedActors").appendChild(budge);
}
}
</script>
</th:block>
</body>

View File

@ -71,6 +71,7 @@
editForm.action = "/genre";
editForm.previousElementSibling.children[0].textContent = "Добавление";
editForm.children[1].children[1].textContent = "Добавить";
editForm.name.value = "";
}
editBtn.onclick = (e) => {