Версия после сдачи
This commit is contained in:
parent
093212138f
commit
9e0a6d0e18
@ -77,7 +77,7 @@ public class DemoApplication implements CommandLineRunner {
|
||||
"Мягкий переплёт", 100000, 488, ""));
|
||||
|
||||
log.info("Create default users values");
|
||||
final var superAdmin = new UserEntity("admin", "forum98761@gmail.com", "bth4323", "Александр", "Мельников");
|
||||
final var superAdmin = new UserEntity("chief", "forum98761@gmail.com", "bth4323", "Александр", "Мельников");
|
||||
superAdmin.setRole(UserRole.SUPERADMIN);
|
||||
userService.create(superAdmin);
|
||||
|
||||
|
@ -1,5 +1,8 @@
|
||||
package com.example.backend.books.api;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
@ -15,6 +18,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import com.example.backend.core.api.PageAttributesMapper;
|
||||
import com.example.backend.core.configuration.Constants;
|
||||
import com.example.backend.core.utils.Formatter;
|
||||
import com.example.backend.core.utils.ToBase64;
|
||||
import com.example.backend.books.model.BookEntity;
|
||||
import com.example.backend.books.service.BookService;
|
||||
@ -50,12 +54,14 @@ public class BookController {
|
||||
private BookDto toDto(BookEntity entity) {
|
||||
final BookDto dto = modelMapper.map(entity, BookDto.class);
|
||||
dto.setCategoryName(entity.getCategory().getName());
|
||||
dto.setDate(Formatter.format(entity.getDate()));
|
||||
return dto;
|
||||
}
|
||||
|
||||
private BookEntity toEntity(BookDto dto) {
|
||||
private BookEntity toEntity(BookDto dto) throws ParseException {
|
||||
final BookEntity entity = modelMapper.map(dto, BookEntity.class);
|
||||
entity.setCategory(categoryService.get(dto.getCategoryId()));
|
||||
entity.setDate(Formatter.parse(dto.getDate()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@ -104,7 +110,7 @@ public class BookController {
|
||||
@RequestParam("cover") MultipartFile cover,
|
||||
BindingResult bindingResult,
|
||||
Model model,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
RedirectAttributes redirectAttributes) throws ParseException {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||
model.addAttribute(
|
||||
@ -120,6 +126,7 @@ public class BookController {
|
||||
book.setImage(base64Image);
|
||||
|
||||
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||
book.setDate(Formatter.format(new Date()));
|
||||
bookService.create(toEntity(book));
|
||||
return Constants.REDIRECT_VIEW + URL;
|
||||
}
|
||||
@ -152,7 +159,7 @@ public class BookController {
|
||||
@RequestParam("cover") MultipartFile cover,
|
||||
BindingResult bindingResult,
|
||||
Model model,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
RedirectAttributes redirectAttributes) throws ParseException {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||
model.addAttribute(
|
||||
|
@ -32,6 +32,7 @@ public class BookDto {
|
||||
private Integer circulation;
|
||||
private Integer weight;
|
||||
private String image;
|
||||
private String date;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@ -176,4 +177,12 @@ public class BookDto {
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import com.example.backend.books.model.BookEntity;
|
||||
import com.example.backend.books.service.BookService;
|
||||
import com.example.backend.core.utils.Formatter;
|
||||
|
||||
@Controller
|
||||
public class MainPageController {
|
||||
@ -23,24 +24,24 @@ public class MainPageController {
|
||||
}
|
||||
|
||||
private BookDto toDto(BookEntity entity) {
|
||||
return modelMapper.map(entity, BookDto.class);
|
||||
final BookDto dto = modelMapper.map(entity, BookDto.class);
|
||||
dto.setDate(Formatter.format(entity.getDate()));
|
||||
return dto;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getMainPage(Model model) {
|
||||
model.addAttribute(
|
||||
"novelties",
|
||||
bookService.getByIds(List.of(4L, 5L, 6L, 7L)).stream()
|
||||
.map(this::toDto)
|
||||
.toList());
|
||||
bookService.getNewest(6));
|
||||
model.addAttribute(
|
||||
"promotions",
|
||||
bookService.getByIds(List.of(5L, 6L, 7L, 8L, 4L)).stream()
|
||||
bookService.getByIds(List.of(4L, 44L, 45L, 65L, 66L, 67L)).stream()
|
||||
.map(this::toDto)
|
||||
.toList());
|
||||
model.addAttribute(
|
||||
"recommendations",
|
||||
bookService.getByIds(List.of(6L, 4L, 8L, 5L, 7L)).stream()
|
||||
bookService.getByIds(List.of(68L, 69L, 70L, 71L, 72L, 75L)).stream()
|
||||
.map(this::toDto)
|
||||
.toList());
|
||||
return MAIN_VIEW;
|
||||
|
@ -3,6 +3,7 @@ package com.example.backend.books.model;
|
||||
import java.util.HashSet;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.Date;
|
||||
|
||||
import com.example.backend.categories.model.CategoryEntity;
|
||||
import com.example.backend.ordersbooks.model.OrderBookEntity;
|
||||
@ -47,6 +48,8 @@ public class BookEntity extends BaseEntity {
|
||||
private Integer weight;
|
||||
@Lob
|
||||
private String image;
|
||||
private Date date = new Date();
|
||||
|
||||
@OneToMany(mappedBy = "book", cascade = CascadeType.REMOVE)
|
||||
@OrderBy("id ASC")
|
||||
private Set<OrderBookEntity> orderBooks = new HashSet<>();
|
||||
@ -74,6 +77,7 @@ public class BookEntity extends BaseEntity {
|
||||
this.circulation = circulation;
|
||||
this.weight = weight;
|
||||
this.image = image;
|
||||
date = new Date();
|
||||
}
|
||||
|
||||
public CategoryEntity getCategory() {
|
||||
@ -204,6 +208,14 @@ public class BookEntity extends BaseEntity {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Set<OrderBookEntity> getOrderBooks() {
|
||||
return orderBooks;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.example.backend.books.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
@ -24,4 +25,7 @@ public interface BookRepository extends CrudRepository<BookEntity, Long>, Paging
|
||||
Page<BookEntity> findAllByFilters(@Param("categoryId") Long categoryId,
|
||||
@Param("searchInfo") String searchInfo,
|
||||
Pageable pageable);
|
||||
|
||||
@Query("SELECT b FROM BookEntity b ORDER BY b.date desc")
|
||||
List<BookEntity> findSomeNewest(Limit limit);
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
package com.example.backend.books.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@ -77,6 +79,14 @@ public class BookService {
|
||||
return sortedEntities;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<BookEntity> getNewest(int limit) {
|
||||
if (limit > 0) {
|
||||
return repository.findSomeNewest(Limit.of(limit));
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public BookEntity get(long id) {
|
||||
return repository.findById(id)
|
||||
@ -129,7 +139,7 @@ public class BookService {
|
||||
case "new":
|
||||
return new Order(Sort.Direction.DESC, "publicationYear");
|
||||
default:
|
||||
throw new IllegalArgumentException("Unknown sort type: " + type);
|
||||
return new Order(Sort.Direction.DESC, "publicationYear");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,6 +39,7 @@
|
||||
<th scope="col">Автор</th>
|
||||
<th scope="col">Цена</th>
|
||||
<th scope="col">Категория каталога</th>
|
||||
<th scope="col">Дата</th>
|
||||
<th scope="col"></th>
|
||||
<th scope="col"></th>
|
||||
</tr>
|
||||
@ -50,6 +51,7 @@
|
||||
<td th:text="${book.author}"></td>
|
||||
<td th:text="${#numbers.formatDecimal(book.price, 1, 2)}"></td>
|
||||
<td th:text="${book.categoryName}"></td>
|
||||
<td th:text="${book.date}"></td>
|
||||
<td>
|
||||
<form th:action="@{/admin/book/edit/{id}(id=${book.id})}" method="get">
|
||||
<input type="hidden" th:name="page" th:value="${page}">
|
||||
|
@ -43,7 +43,7 @@
|
||||
<div th:if="${#fields.hasErrors('email')}" th:errors="*{email}" class="invalid-feedback">
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center d-flex flex-column align-items-center">
|
||||
<div class="text-center d-flex flex-column align-items-center mb-3">
|
||||
<button class="btn btn-primary w-50 rounded-5 reg-btn mb-3" type="submit">Сохранить</button>
|
||||
<a class="btn btn-secondary w-25 rounded-5 reg-btn" th:href="@{/profile(page=${page})}">Отмена</a>
|
||||
</div>
|
||||
|
@ -29,7 +29,7 @@
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-sm-6 col-md-4 col-lg-3 mt-4" th:each="book : ${items}">
|
||||
<div class="col-sm-6 col-md-4 col-lg-3 mt-4 mb-3" th:each="book : ${items}">
|
||||
<div class="block d-flex flex-column h-100">
|
||||
<div class="d-flex justify-content-center mb-4">
|
||||
<a th:href="@{/product-card/{id}(id=${book.id})}">
|
||||
|
Loading…
Reference in New Issue
Block a user