From c9d0e58557ac38a745b0e404a2ccc8e71c594f22 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Wed, 24 Jan 2024 20:12:48 +0300 Subject: [PATCH] Mock basic REST api --- .../ru/redrise/marinesco/library/Author.java | 5 ++- .../library/api/AuthorsApiController.java | 32 ++++++++++++++++ .../library/api/BooksApiController.java | 38 +++++++++++++++++++ .../library/api/GenresApiController.java | 33 ++++++++++++++++ .../marinesco/security/SecurityConfig.java | 3 ++ src/main/resources/application.yml | 4 +- 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java create mode 100644 src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java create mode 100644 src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java diff --git a/src/main/java/ru/redrise/marinesco/library/Author.java b/src/main/java/ru/redrise/marinesco/library/Author.java index c109bee..8c260c7 100644 --- a/src/main/java/ru/redrise/marinesco/library/Author.java +++ b/src/main/java/ru/redrise/marinesco/library/Author.java @@ -2,6 +2,8 @@ package ru.redrise.marinesco.library; import java.util.List; +import com.fasterxml.jackson.annotation.JsonIgnore; + import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.ManyToMany; @@ -15,8 +17,9 @@ import lombok.NoArgsConstructor; public class Author { @Id private Long id; - private String authorName; + private String import lombok.extern.slf4j.Slf4j;import lombok.extern.slf4j.Slf4j;; + @JsonIgnore @ManyToMany(mappedBy = "authors") private List books; diff --git a/src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java b/src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java new file mode 100644 index 0000000..76752c5 --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java @@ -0,0 +1,32 @@ +package ru.redrise.marinesco.library.api; + +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import ru.redrise.marinesco.data.AuthorRepository; +import ru.redrise.marinesco.library.Author; + +@RestController +@RequestMapping(path = "/api/author", + produces = "application/json") +public class AuthorsApiController { + private AuthorRepository authorRepository; + + public AuthorsApiController(AuthorRepository authorRepository){ + this.authorRepository = authorRepository; + } + + @GetMapping + public Iterable getAuthors( + @RequestParam(value = "page", required = false, defaultValue = "0") Integer page, + @RequestParam(value = "sort", required = false, defaultValue = "authorName") String sortBy){ + PageRequest pageRequest = PageRequest.of( + page, 10, Sort.by(sortBy).descending()); + + return authorRepository.findAll(pageRequest).getContent(); + } +} diff --git a/src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java b/src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java new file mode 100644 index 0000000..98f5833 --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java @@ -0,0 +1,38 @@ +package ru.redrise.marinesco.library.api; + +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import ru.redrise.marinesco.data.BookRepository; +import ru.redrise.marinesco.library.Book; + +@RestController +@RequestMapping(path = "/api/book", + produces = "application/json") +public class BooksApiController { + private BookRepository bookRepository ; + + public BooksApiController(BookRepository bookRepository){ + this.bookRepository = bookRepository; + } + + @GetMapping(params = "recent") + //@PreAuthorize("hasRole('ADMIN')") // move to security later + public Iterable recentBooks(){ + return getBooks(0, "addedDate"); + } + + @GetMapping(params = "page") + public Iterable getBooks(@RequestParam(value = "page", required = true) Integer page, + @RequestParam(value = "sort", required = false, defaultValue = "title") String sortBy){ + PageRequest pageRequest = PageRequest.of( + page, 10, Sort.by(sortBy).descending()); + + return bookRepository.findAll(pageRequest).getContent(); + } + +} diff --git a/src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java b/src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java new file mode 100644 index 0000000..87e368a --- /dev/null +++ b/src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java @@ -0,0 +1,33 @@ +package ru.redrise.marinesco.library.api; + +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import ru.redrise.marinesco.data.GenreRepository; +import ru.redrise.marinesco.library.Genre; + +@RestController +@RequestMapping(path = "/api/genres", produces = "application/json") +public class GenresApiController { + private GenreRepository genreRepository; + + public GenresApiController(GenreRepository genreRepository) { + this.genreRepository = genreRepository; + } + + @GetMapping + public Iterable getGenres( + @RequestParam(value = "page", required = false, defaultValue = "0") Integer page, + @RequestParam(value = "sort", required = false, defaultValue = "genreId") String sortBy) { + PageRequest pageRequest = PageRequest.of( + page, 10, Sort.by(sortBy).descending()); + + return genreRepository.findAll(pageRequest).getContent(); + } + + +} diff --git a/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java b/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java index 71e8146..1ef8de0 100644 --- a/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java +++ b/src/main/java/ru/redrise/marinesco/security/SecurityConfig.java @@ -53,6 +53,9 @@ public class SecurityConfig { .requestMatchers(mvc.pattern("/register")).anonymous() .requestMatchers(mvc.pattern("/login")).anonymous() .requestMatchers(mvc.pattern("/error")).permitAll() + + .requestMatchers(mvc.pattern("/api/**")).permitAll() // TODO: FIX + .requestMatchers(PathRequest.toH2Console()).permitAll() .requestMatchers(mvc.pattern("/")).authenticated() .requestMatchers(mvc.pattern("/profile/**")).authenticated()//.hasAnyRole("ADMIN", "USER") diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 90eb1c7..a5935d2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,7 +6,7 @@ spring: generate-unique-name: false name: marinesco # url: jdbc:h2:mem:marinesco -# url: jdbc:h2:file:/tmp/h22 + url: jdbc:h2:file:/tmp/h22 username: sa password: jpa: @@ -28,6 +28,8 @@ logging: level: org: springframework: INFO +# file: +# path: "/tmp/log" marinesco: library: filesLocation: "./lib" \ No newline at end of file