personalweb

archived
git clone git://git.wimdupont.com/personalweb.git
Log | Files | Refs | LICENSE

commit 51b22052b9185b50d4d81d8ff9ec7fcf5ada0d84
parent 150edb76ada6b01960874ba2636ce68843c913c2
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat, 29 Jan 2022 14:58:15 +0100

Book sorting update and affirmation as record


Former-commit-id: f80b5e8748d171ff9dfe45f09524a50ff0b9fa2e
Diffstat:
Msrc/main/java/com/wimdupont/personalweb/api/dto/affirmation/Affirmation.java | 12+-----------
Msrc/main/java/com/wimdupont/personalweb/controller/BookController.java | 11+++--------
Msrc/main/java/com/wimdupont/personalweb/controller/IndexController.java | 2+-
Msrc/main/java/com/wimdupont/personalweb/service/BookService.java | 5+++--
4 files changed, 8 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/wimdupont/personalweb/api/dto/affirmation/Affirmation.java b/src/main/java/com/wimdupont/personalweb/api/dto/affirmation/Affirmation.java @@ -1,15 +1,5 @@ package com.wimdupont.personalweb.api.dto.affirmation; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +public record Affirmation(String affirmation) { -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class Affirmation { - - private String affirmation; } diff --git a/src/main/java/com/wimdupont/personalweb/controller/BookController.java b/src/main/java/com/wimdupont/personalweb/controller/BookController.java @@ -9,7 +9,6 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; -import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -24,13 +23,9 @@ public class BookController { @GetMapping @SuppressWarnings("unused") public String getBooks(Model model) { - List<BookDto> bookDtoList = bookService.findAll().stream().map(bookToBookDtoConverter::convert) - .sorted(Comparator.comparing(BookDto::getCategory) - .thenComparing(BookDto::getAuthor) - .thenComparing(BookDto::getSeries, Comparator.nullsFirst(Comparator.naturalOrder())) - .thenComparing(BookDto::getSeriesNumber, Comparator.nullsFirst(Comparator.naturalOrder())) - .thenComparing(BookDto::getTitle) - ).collect(Collectors.toList()); + List<BookDto> bookDtoList = bookService.findAllSorted().stream() + .map(bookToBookDtoConverter::convert) + .collect(Collectors.toList()); model.addAttribute("books", bookDtoList); return "books"; diff --git a/src/main/java/com/wimdupont/personalweb/controller/IndexController.java b/src/main/java/com/wimdupont/personalweb/controller/IndexController.java @@ -17,7 +17,7 @@ public class IndexController { @SuppressWarnings("unused") public String home(Model model) { model.addAttribute("affirmation", affirmationApi.getAffirmation() - .orElseGet(() -> Affirmation.builder().affirmation("You're the best!").build())); + .orElseGet(() -> new Affirmation("You're the best!"))); return "home"; } diff --git a/src/main/java/com/wimdupont/personalweb/service/BookService.java b/src/main/java/com/wimdupont/personalweb/service/BookService.java @@ -3,6 +3,7 @@ package com.wimdupont.personalweb.service; import com.wimdupont.personalweb.model.dao.Book; import com.wimdupont.personalweb.repository.BookRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import javax.transaction.Transactional; @@ -15,7 +16,7 @@ public class BookService { private final BookRepository bookRepository; - public List<Book> findAll() { - return bookRepository.findAll(); + public List<Book> findAllSorted() { + return bookRepository.findAll(Sort.by("category", "author", "series", "seriesNumber", "title").ascending()); } }