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

added Pagination, Sorting, Swagger to the existing controllers #5

Merged
merged 1 commit into from
Aug 29, 2023
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
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>

<build>
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/bookstore/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.bookstore.dto.BookSearchParametersDto;
import com.bookstore.dto.CreateBookRequestDto;
import com.bookstore.service.BookService;
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.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -18,41 +21,48 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Book management", description = "Endpoints for managing books")
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/api/books")
public class BookController {
private final BookService bookService;

@GetMapping
public List<BookDto> getAll() {
return bookService.getAll();
@Operation(summary = "Get all books", description = "Get a list of available books")
public List<BookDto> getAll(Pageable pageable) {
return bookService.getAll(pageable);
}

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

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
@Operation(summary = "Create a new book", description = "Create a new book")
public BookDto createBook(@RequestBody @Valid CreateBookRequestDto bookRequestDto) {
return bookService.createBook(bookRequestDto);
}

@PutMapping("/{id}")
@ResponseStatus(HttpStatus.ACCEPTED)
@Operation(summary = "Update the existing book", description = "Update the existing book")
public void updateBook(@PathVariable Long id,
@RequestBody @Valid CreateBookRequestDto bookRequestDto) {
bookService.updateBook(id, bookRequestDto);
}

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

@GetMapping("/search")
@Operation(summary = "Search books", description = "Search book by specific search parameters")
public List<BookDto> searchBooks(BookSearchParametersDto searchParameters) {
return bookService.searchBooks(searchParameters);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/bookstore/service/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.bookstore.dto.BookSearchParametersDto;
import com.bookstore.dto.CreateBookRequestDto;
import java.util.List;
import org.springframework.data.domain.Pageable;

public interface BookService {
BookDto createBook(CreateBookRequestDto bookRequestDto);

BookDto getBookById(Long id);

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

void updateBook(Long id, CreateBookRequestDto bookRequestDto);

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/bookstore/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.bookstore.service.BookService;
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;

Expand All @@ -36,8 +37,8 @@ public BookDto getBookById(Long id) {
}

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