mvc fix author and type names problem
This commit is contained in:
parent
c5d0bd083f
commit
80a6dddd0d
@ -1,6 +1,7 @@
|
|||||||
package com.ip.library.controllers.books;
|
package com.ip.library.controllers.books;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.springframework.stereotype.Controller;
|
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.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
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.controllers.types.TypeService;
|
||||||
import com.ip.library.core.api.PageAttributesMapper;
|
import com.ip.library.core.api.PageAttributesMapper;
|
||||||
import com.ip.library.core.configuration.Constants;
|
import com.ip.library.core.configuration.Constants;
|
||||||
@ -43,7 +45,18 @@ public class BookController {
|
|||||||
|
|
||||||
private BookDto toBookDto (BookEntity entity) {
|
private BookDto toBookDto (BookEntity entity) {
|
||||||
BookDto bookDto = modelMapper.map(entity, BookDto.class);
|
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;
|
return bookDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,10 @@ import jakarta.validation.constraints.NotNull;
|
|||||||
public class BookDto {
|
public class BookDto {
|
||||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
private String authorName;
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
private String typeName;
|
||||||
@NotBlank
|
@NotBlank
|
||||||
private String name;
|
private String name;
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -27,6 +31,22 @@ public class BookDto {
|
|||||||
this.id = id;
|
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() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
@ -47,7 +67,7 @@ public class BookDto {
|
|||||||
return authorsId;
|
return authorsId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAuthorId(List<Long> authorsId) {
|
public void setAuthorsId(List<Long> authorsId) {
|
||||||
this.authorsId = authorsId;
|
this.authorsId = authorsId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
<th scope="col" class="w-10">ID</th>
|
<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-auto">Жанр</th>
|
||||||
|
<th scope="col" class="w-auto">Автор</th>
|
||||||
<th scope="col" class="w-10"></th>
|
<th scope="col" class="w-10"></th>
|
||||||
<th scope="col" class="w-10"></th>
|
<th scope="col" class="w-10"></th>
|
||||||
</tr>
|
</tr>
|
||||||
@ -29,7 +30,8 @@
|
|||||||
<tr th:each="book : ${items}">
|
<tr th:each="book : ${items}">
|
||||||
<th scope="row" th:text="${book.id}"></th>
|
<th scope="row" th:text="${book.id}"></th>
|
||||||
<td th:text="${book.name}"></td>
|
<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>
|
<td>
|
||||||
<form th:action="@{/api/1.0/book/edit/{id}(id=${book.id})}" method="get">
|
<form th:action="@{/api/1.0/book/edit/{id}(id=${book.id})}" method="get">
|
||||||
<input type="hidden" th:name="page" th:value="${page}">
|
<input type="hidden" th:name="page" th:value="${page}">
|
||||||
|
Loading…
Reference in New Issue
Block a user