diff --git a/src/main/java/com/example/backend/DemoApplication.java b/src/main/java/com/example/backend/DemoApplication.java index 33b7e94..6366ef9 100644 --- a/src/main/java/com/example/backend/DemoApplication.java +++ b/src/main/java/com/example/backend/DemoApplication.java @@ -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); diff --git a/src/main/java/com/example/backend/books/api/BookController.java b/src/main/java/com/example/backend/books/api/BookController.java index c66dd7b..588841d 100644 --- a/src/main/java/com/example/backend/books/api/BookController.java +++ b/src/main/java/com/example/backend/books/api/BookController.java @@ -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( diff --git a/src/main/java/com/example/backend/books/api/BookDto.java b/src/main/java/com/example/backend/books/api/BookDto.java index 71a5502..c5a5f6f 100644 --- a/src/main/java/com/example/backend/books/api/BookDto.java +++ b/src/main/java/com/example/backend/books/api/BookDto.java @@ -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; + } } diff --git a/src/main/java/com/example/backend/books/api/MainPageController.java b/src/main/java/com/example/backend/books/api/MainPageController.java index dd6e2ac..b0d20b0 100644 --- a/src/main/java/com/example/backend/books/api/MainPageController.java +++ b/src/main/java/com/example/backend/books/api/MainPageController.java @@ -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; diff --git a/src/main/java/com/example/backend/books/model/BookEntity.java b/src/main/java/com/example/backend/books/model/BookEntity.java index 01b2aa8..16cf79a 100644 --- a/src/main/java/com/example/backend/books/model/BookEntity.java +++ b/src/main/java/com/example/backend/books/model/BookEntity.java @@ -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 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 getOrderBooks() { return orderBooks; } diff --git a/src/main/java/com/example/backend/books/repository/BookRepository.java b/src/main/java/com/example/backend/books/repository/BookRepository.java index eba1c2a..742183c 100644 --- a/src/main/java/com/example/backend/books/repository/BookRepository.java +++ b/src/main/java/com/example/backend/books/repository/BookRepository.java @@ -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, Paging Page findAllByFilters(@Param("categoryId") Long categoryId, @Param("searchInfo") String searchInfo, Pageable pageable); + + @Query("SELECT b FROM BookEntity b ORDER BY b.date desc") + List findSomeNewest(Limit limit); } diff --git a/src/main/java/com/example/backend/books/service/BookService.java b/src/main/java/com/example/backend/books/service/BookService.java index 62a8639..7f1b2fc 100644 --- a/src/main/java/com/example/backend/books/service/BookService.java +++ b/src/main/java/com/example/backend/books/service/BookService.java @@ -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 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"); } } diff --git a/src/main/resources/templates/book.html b/src/main/resources/templates/book.html index 7fa741a..bb7d902 100644 --- a/src/main/resources/templates/book.html +++ b/src/main/resources/templates/book.html @@ -39,6 +39,7 @@ Автор Цена Категория каталога + Дата @@ -50,6 +51,7 @@ +
diff --git a/src/main/resources/templates/profile-edit.html b/src/main/resources/templates/profile-edit.html index 4ae164a..27e13b8 100644 --- a/src/main/resources/templates/profile-edit.html +++ b/src/main/resources/templates/profile-edit.html @@ -43,7 +43,7 @@
-
+
Отмена
diff --git a/src/main/resources/templates/search.html b/src/main/resources/templates/search.html index 919af76..c924af0 100644 --- a/src/main/resources/templates/search.html +++ b/src/main/resources/templates/search.html @@ -29,7 +29,7 @@
-
+