diff --git a/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java b/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java index f29a4d6..64a3930 100644 --- a/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java +++ b/src/main/java/ru/redrise/marinesco/data/AuthorRepository.java @@ -2,6 +2,7 @@ package ru.redrise.marinesco.data; import java.util.Optional; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -12,4 +13,5 @@ import java.util.List; public interface AuthorRepository extends JpaRepository{ Optional findByAuthorName(String authorName); List findByAuthorNameContainingIgnoreCase(String authorName); + List findByAuthorNameContainingIgnoreCase(String authorName, PageRequest pageRequest); } diff --git a/src/main/java/ru/redrise/marinesco/data/BookRepository.java b/src/main/java/ru/redrise/marinesco/data/BookRepository.java index f656335..c60596f 100644 --- a/src/main/java/ru/redrise/marinesco/data/BookRepository.java +++ b/src/main/java/ru/redrise/marinesco/data/BookRepository.java @@ -2,6 +2,7 @@ package ru.redrise.marinesco.data; import java.util.List; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -10,5 +11,7 @@ import ru.redrise.marinesco.library.Book; @Repository public interface BookRepository extends JpaRepository{ List findBySeriesContainingIgnoreCase(String title); + List findBySeriesContainingIgnoreCase(String title, PageRequest page); List findByTitleContainingIgnoreCase(String title); + List findByTitleContainingIgnoreCase(String title, PageRequest page); } \ No newline at end of file diff --git a/src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java b/src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java index 76752c5..bdb83cc 100644 --- a/src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java +++ b/src/main/java/ru/redrise/marinesco/library/api/AuthorsApiController.java @@ -3,6 +3,7 @@ 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.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -29,4 +30,20 @@ public class AuthorsApiController { return authorRepository.findAll(pageRequest).getContent(); } + + @GetMapping("/by/name/{name}") + public Iterable getAuthorId( + @PathVariable("name") String authorName, + @RequestParam(value = "page", required = false, defaultValue = "0") Integer page){ + + PageRequest pageRequest = PageRequest.of(page, 10); + + return authorRepository.findByAuthorNameContainingIgnoreCase(authorName, pageRequest); + } + + @GetMapping("/by/id/{id}") + public Author getAuthorId(@PathVariable("id") Long authorId){ + + return authorRepository.findById(authorId).get(); + } } diff --git a/src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java b/src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java index 98f5833..fb6b8a1 100644 --- a/src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java +++ b/src/main/java/ru/redrise/marinesco/library/api/BooksApiController.java @@ -1,8 +1,11 @@ package ru.redrise.marinesco.library.api; +import java.util.Optional; + 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.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -27,12 +30,38 @@ public class BooksApiController { } @GetMapping(params = "page") - public Iterable getBooks(@RequestParam(value = "page", required = true) Integer 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(); } + @GetMapping("/by/title/{title}") + public Iterable getBooksByName( + @PathVariable("title") String title, + @RequestParam(value = "page", required = false, defaultValue = "0") Integer page){ + + PageRequest pageRequest = PageRequest.of(page, 10); + + return bookRepository.findByTitleContainingIgnoreCase(title, pageRequest); + } + + @GetMapping("/by/series/{series}") + public Iterable getBooksBySeries( + @PathVariable("series") String series, + @RequestParam(value = "page", required = false, defaultValue = "0") Integer page){ + + PageRequest pageRequest = PageRequest.of(page, 10); + + return bookRepository.findBySeriesContainingIgnoreCase(series, pageRequest); + } + + @GetMapping("/by/id/{id}") + public Book getBooksById(@PathVariable("id") Integer id){ + return bookRepository.findById(id).get(); + } } diff --git a/src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java b/src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java index 6a7a8c9..2e4a61b 100644 --- a/src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java +++ b/src/main/java/ru/redrise/marinesco/library/api/GenresApiController.java @@ -1,19 +1,26 @@ package ru.redrise.marinesco.library.api; +import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; +import lombok.extern.slf4j.Slf4j; import ru.redrise.marinesco.data.GenreRepository; +import ru.redrise.marinesco.library.Author; import ru.redrise.marinesco.library.Genre; +@Slf4j @RestController @RequestMapping(path = "/api/genres", produces = "application/json") public class GenresApiController { @@ -27,16 +34,36 @@ public class GenresApiController { 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()); + PageRequest pageRequest = PageRequest.of(page, 10, Sort.by(sortBy).descending()); return genreRepository.findAll(pageRequest).getContent(); } + + @GetMapping("/by/id/{id}") + public Genre getAuthorId(@PathVariable("id") String id){ + return genreRepository.findById(id).get(); + } @PostMapping(consumes = "application/json") @ResponseStatus(HttpStatus.CREATED) public Genre postGenre(@RequestBody Genre genre) { + log.info("{}", genre); return genreRepository.save(genre); } + @PutMapping(path = "/{genreId}", consumes = "application/json") + public Genre putGenre(@PathVariable("genreId") String genreId, + @RequestBody Genre genre){ + genre.setGenreId(genreId); + return genreRepository.save(genre); + } + + @DeleteMapping("/{genreId}") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void deleteGenre(@PathVariable("genreId") String genreId){ + try{ + genreRepository.deleteById(genreId); + } + catch(EmptyResultDataAccessException ignore){} + } } diff --git a/src/main/resources/templates/fragments/header.html b/src/main/resources/templates/fragments/header.html index 2786a70..5538708 100644 --- a/src/main/resources/templates/fragments/header.html +++ b/src/main/resources/templates/fragments/header.html @@ -51,8 +51,7 @@ |
  • - Logout - + Logout
  • diff --git a/src/main/resources/templates/registration.html b/src/main/resources/templates/registration.html index 5978356..d5a58b4 100644 --- a/src/main/resources/templates/registration.html +++ b/src/main/resources/templates/registration.html @@ -15,38 +15,38 @@

    Register

    - pew - Error


    - Error


    - false


    - Error


    - +

    diff --git a/src/main/resources/templates/settings.html b/src/main/resources/templates/settings.html index eeb9105..df8d7a7 100644 --- a/src/main/resources/templates/settings.html +++ b/src/main/resources/templates/settings.html @@ -14,27 +14,27 @@