mvc fix author and type names problem

This commit is contained in:
Zakharov_Rostislav 2024-06-05 22:10:12 +04:00
parent c5d0bd083f
commit 80a6dddd0d
3 changed files with 38 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package com.ip.library.controllers.books;
import java.util.Map;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Controller;
@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.ip.library.controllers.authors.AuthorEntity;
import com.ip.library.controllers.types.TypeService;
import com.ip.library.core.api.PageAttributesMapper;
import com.ip.library.core.configuration.Constants;
@ -43,7 +45,18 @@ public class BookController {
private BookDto toBookDto (BookEntity entity) {
BookDto bookDto = modelMapper.map(entity, BookDto.class);
bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
List<AuthorEntity> authors = entity.getAuthorsBooks().stream().map(x -> x.getAuthor()).toList();
bookDto.setAuthorsId(authors.stream().map(x -> x.getId()).toList());
bookDto.setTypeName(entity.getType().getName());
StringBuilder authorName = new StringBuilder();
for (AuthorEntity authorEntity : authors) {
authorName.append(", ").append(authorEntity.getName());
}
if (authorName.length() > 0) {
bookDto.setAuthorName(authorName.toString().substring(2));
} else {
bookDto.setAuthorName("Неизвестен");
}
return bookDto;
}

View File

@ -11,6 +11,10 @@ import jakarta.validation.constraints.NotNull;
public class BookDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String authorName;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String typeName;
@NotBlank
private String name;
@NotNull
@ -27,6 +31,22 @@ public class BookDto {
this.id = id;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getName() {
return name;
}
@ -47,7 +67,7 @@ public class BookDto {
return authorsId;
}
public void setAuthorId(List<Long> authorsId) {
public void setAuthorsId(List<Long> authorsId) {
this.authorsId = authorsId;
}
}

View File

@ -21,6 +21,7 @@
<th scope="col" class="w-10">ID</th>
<th scope="col" class="w-auto">Название</th>
<th scope="col" class="w-auto">Жанр</th>
<th scope="col" class="w-auto">Автор</th>
<th scope="col" class="w-10"></th>
<th scope="col" class="w-10"></th>
</tr>
@ -29,7 +30,8 @@
<tr th:each="book : ${items}">
<th scope="row" th:text="${book.id}"></th>
<td th:text="${book.name}"></td>
<td th:text="${book.type.name}"></td>
<td th:text="${book.typeName}"></td>
<td th:text="${book.authorName}"></td>
<td>
<form th:action="@{/api/1.0/book/edit/{id}(id=${book.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}">