Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swagger, pagination, sorting added #8

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@
<artifactId>hibernate-validator</artifactId>
<version>7.0.5.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>

<build>
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/project/bookstore/controller/BookController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package project.bookstore.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -17,6 +20,7 @@
import project.bookstore.mapper.BookMapper;
import project.bookstore.service.BookService;

@Tag(name = "Book controller", description = "endpoints for managing library")
@RequiredArgsConstructor
@RestController
@RequestMapping("/books")
Expand All @@ -25,21 +29,31 @@ public class BookController {
private final BookMapper bookMapper;

@GetMapping
public List<BookDto> getAll() {
return bookService.findAll();
@Operation(
summary = "Get all books",
description = "Get all books from db")
public List<BookDto> getAll(Pageable pageable) {
return bookService.findAll(pageable);
}

@GetMapping("/{id}")
@Operation(
summary = "Get book by id",
description = "Get book by id from db")
public BookDto getBookById(@PathVariable Long id) {
return bookService.getBookById(id);
}

@PostMapping
@Operation(
summary = "Create new book",
description = "Create new book and add to db")
public BookDto createBook(@RequestBody @Valid CreateBookRequestDto requestDto) {
return bookMapper.toDto(bookService.save(requestDto));
}

@PutMapping("/{id}")
@Operation(summary = "Update book's info", description = "Update book's info")
public BookDto updateBook(
@RequestBody @Valid CreateBookRequestDto requestDto,
@PathVariable Long id
Expand All @@ -48,11 +62,15 @@ public BookDto updateBook(
}

@DeleteMapping("/{id}")
@Operation(summary = "Delete book", description = "Delete book from db")
public void deleteBook(@PathVariable Long id) {
bookService.deleteBookById(id);
}

@GetMapping("/search")
@Operation(
summary = "Search books by parameters",
description = "Search books by parameters: title, author and price")
public List<BookDto> searchBook(BookSearchParameters searchParameters) {
return bookService.search(searchParameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import project.bookstore.model.Book;

public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> {
public interface BookRepository extends
JpaRepository<Book, Long>,
JpaSpecificationExecutor<Book>,
PagingAndSortingRepository<Book, Long> {
}
3 changes: 2 additions & 1 deletion src/main/java/project/bookstore/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package project.bookstore.service;

import java.util.List;
import org.springframework.data.domain.Pageable;
import project.bookstore.dto.BookDto;
import project.bookstore.dto.BookSearchParameters;
import project.bookstore.dto.CreateBookRequestDto;
Expand All @@ -9,7 +10,7 @@
public interface BookService {
Book save(CreateBookRequestDto requestBookDto);

List<BookDto> findAll();
List<BookDto> findAll(Pageable pageable);

BookDto getBookById(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import project.bookstore.dto.BookDto;
Expand All @@ -28,8 +29,8 @@ public Book save(CreateBookRequestDto requestBookDto) {
}

@Override
public List<BookDto> findAll() {
return bookRepository.findAll().stream()
public List<BookDto> findAll(Pageable pageable) {
return bookRepository.findAll(pageable).stream()
.map(bookMapper::toDto)
.toList();
}
Expand Down
Loading