From 509d31e1d874c1f74883eb72c5cc1f27b25219c7 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Thu, 11 Jan 2024 21:56:11 +0300 Subject: [PATCH] Add Author page --- .../redrise/marinesco/AuthorController.java | 43 +++++++++++++++++++ .../marinesco/data/BookRepository.java | 4 ++ src/main/resources/static/styles/styles.css | 4 ++ src/main/resources/templates/author.html | 37 ++++++++++++++++ src/main/resources/templates/root.html | 1 + 5 files changed, 89 insertions(+) create mode 100644 src/main/java/ru/redrise/marinesco/AuthorController.java create mode 100644 src/main/resources/templates/author.html diff --git a/src/main/java/ru/redrise/marinesco/AuthorController.java b/src/main/java/ru/redrise/marinesco/AuthorController.java new file mode 100644 index 0000000..b033fdd --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/AuthorController.java @@ -0,0 +1,43 @@ +package ru.redrise.marinesco; + +import java.util.List; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; + +import ru.redrise.marinesco.data.AuthorRepository; +import ru.redrise.marinesco.data.BookRepository; +import ru.redrise.marinesco.library.Author; +import ru.redrise.marinesco.library.Book; + +@Controller +@RequestMapping("/author") +public class AuthorController { + private AuthorRepository authorRepository; + private BookRepository bookRepository; + + public AuthorController(AuthorRepository authorRepository, BookRepository bookRepository){ + this.authorRepository = authorRepository; + this.bookRepository = bookRepository; + } + + @GetMapping("/{authorId}") + public String getPage(@PathVariable("authorId") Long authorId, Model model) { + final Author author = authorRepository.findById(authorId).orElse(null); + + if (author == null){ + model.addAttribute("Error", "Not found"); + return "author"; + } + + List books = bookRepository.findAllByAuthorsContains(author); + + model.addAttribute("author", author); + model.addAttribute("books", books); + + return "author"; + } +} diff --git a/src/main/java/ru/redrise/marinesco/data/BookRepository.java b/src/main/java/ru/redrise/marinesco/data/BookRepository.java index 137fabb..b7e6309 100644 --- a/src/main/java/ru/redrise/marinesco/data/BookRepository.java +++ b/src/main/java/ru/redrise/marinesco/data/BookRepository.java @@ -5,12 +5,16 @@ import java.util.List; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import ru.redrise.marinesco.library.Author; import ru.redrise.marinesco.library.Book; + @Repository public interface BookRepository extends CrudRepository{ List findBySeriesContainingIgnoreCase(String title); List findByTitleContainingIgnoreCase(String title); + + List findAllByAuthorsContains(Author author); } \ No newline at end of file diff --git a/src/main/resources/static/styles/styles.css b/src/main/resources/static/styles/styles.css index 823cccb..85b89cb 100644 --- a/src/main/resources/static/styles/styles.css +++ b/src/main/resources/static/styles/styles.css @@ -234,3 +234,7 @@ button:hover { padding: 3px; background-color: #D00000; } + +.book_title{ + font-weight: bold; +} \ No newline at end of file diff --git a/src/main/resources/templates/author.html b/src/main/resources/templates/author.html new file mode 100644 index 0000000..66f2b92 --- /dev/null +++ b/src/main/resources/templates/author.html @@ -0,0 +1,37 @@ + + + + + Marinesco + + + + + + +
+
+
+ +
+

+
+ + + +
+
+
+
+ + +

+
+ +
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/root.html b/src/main/resources/templates/root.html index 6db75fb..cf942db 100644 --- a/src/main/resources/templates/root.html +++ b/src/main/resources/templates/root.html @@ -17,6 +17,7 @@
/manage_users
/settings
/book/59992766 +
/author/1
H2