Skip to content

Commit

Permalink
added pagination and api annotations (#5)
Browse files Browse the repository at this point in the history
added pagination and api annotations
  • Loading branch information
BlueVioletTeti authored Sep 28, 2023
1 parent 7f77cef commit 8577c61
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
21 changes: 14 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<java.version>17</java.version>
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<springdoc-openapi.version>2.1.0</springdoc-openapi.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -76,7 +76,11 @@
<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>${springdoc-openapi.version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -124,14 +128,17 @@
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>${lombok.mapstruct.binding.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package mate.academy.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 mate.academy.bookstore.dto.BookDto;
import mate.academy.bookstore.dto.CreateBookRequestDto;
import mate.academy.bookstore.service.BookService;
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 @@ -15,34 +18,40 @@
import org.springframework.web.bind.annotation.RequestMapping;
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.findAll();
@Operation(summary = "Get all books", description = "Get a list of all available books")
public List<BookDto> getAll(Pageable pageable) {
return bookService.findAll(pageable);
}

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

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

@PutMapping("/{id}")
@Operation(summary = "Update a book", description = "Update a book by id")
public BookDto updateBook(@PathVariable Long id,
@RequestBody @Valid CreateBookRequestDto requestDto) {
return bookService.update(id, requestDto);
}

@DeleteMapping("/{id}")
@Operation(summary = "Delete a book", description = "Delete a book by id")
public void deleteBook(@PathVariable Long id) {
bookService.delete(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import java.util.List;
import mate.academy.bookstore.dto.BookDto;
import mate.academy.bookstore.dto.CreateBookRequestDto;
import org.springframework.data.domain.Pageable;

public interface BookService {
BookDto save(CreateBookRequestDto requestDto);

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

BookDto findById(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import mate.academy.bookstore.mapper.BookMapper;
import mate.academy.bookstore.model.Book;
import mate.academy.bookstore.repository.BookRepository;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
Expand All @@ -23,8 +24,8 @@ public BookDto save(CreateBookRequestDto requestDto) {
}

@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
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring.datasource.url=jdbc:mysql://localhost:3306/books_app?serverTimezone=UTC
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.username=root
spring.datasource.password=imdv306
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/liquibase.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
url=jdbc:mysql://localhost:3306/books_app?serverTimezone=UTC
username=root
password=imdv306
changeLogFile=/db/changelog/db.changelog-master.yaml

0 comments on commit 8577c61

Please sign in to comment.