diff --git a/src/main/java/project/bookstore/controller/CategoryController.java b/src/main/java/project/bookstore/controller/CategoryController.java index 71bd597..e24d46e 100644 --- a/src/main/java/project/bookstore/controller/CategoryController.java +++ b/src/main/java/project/bookstore/controller/CategoryController.java @@ -1,8 +1,13 @@ 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.springdoc.core.annotations.ParameterObject; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -28,36 +33,54 @@ public class CategoryController { @PreAuthorize("hasRole('ROLE_ADMIN')") @PostMapping - public CategoryDto createCategory(@RequestBody CategoryRequestDto requestDto) { + @Operation( + summary = "Create new category", + description = "Create new category and add to db") + public CategoryDto createCategory(@RequestBody @Valid CategoryRequestDto requestDto) { return categoryService.save(requestDto); } @GetMapping - public List getAll() { + @Operation( + summary = "Get all categories", + description = "Get all categories from db") + public List getAll(@ParameterObject @PageableDefault Pageable pageable) { return categoryService.findAll(); } @GetMapping("/{id}") + @Operation( + summary = "Get category by id", + description = "Get category by id from db") public CategoryDto getCategoryById(@PathVariable Long id) { return categoryService.getById(id); } @PreAuthorize("hasRole('ROLE_ADMIN')") @PutMapping("/{id}") + @Operation( + summary = "Update category by id", + description = "Update category by id in db") public CategoryDto updateCategory( @PathVariable Long id, - @RequestBody CategoryRequestDto categoryRequestDto + @RequestBody @Valid CategoryRequestDto categoryRequestDto ) { return categoryService.update(id, categoryRequestDto); } @PreAuthorize("hasRole('ROLE_ADMIN')") @DeleteMapping("/{id}") + @Operation( + summary = "Delete category by id", + description = "Delete category by id from db") public void deleteCategory(@PathVariable Long id) { categoryService.deleteById(id); } @GetMapping("/{id}/books") + @Operation( + summary = "Get all books of category", + description = "Get all books of category by id from db") public List getBooksByCategoryId(@PathVariable Long id) { return bookService.getAllByCategoriesId(id); } diff --git a/src/main/java/project/bookstore/model/Category.java b/src/main/java/project/bookstore/model/Category.java index 1e42a0e..5a68477 100644 --- a/src/main/java/project/bookstore/model/Category.java +++ b/src/main/java/project/bookstore/model/Category.java @@ -17,7 +17,7 @@ public class Category { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Column(unique = true) + @Column(nullable = false, unique = true) private String name; private String description; } diff --git a/src/main/java/project/bookstore/repository/category/CategoryRepository.java b/src/main/java/project/bookstore/repository/category/CategoryRepository.java index ca65a17..a7a0133 100644 --- a/src/main/java/project/bookstore/repository/category/CategoryRepository.java +++ b/src/main/java/project/bookstore/repository/category/CategoryRepository.java @@ -4,5 +4,4 @@ import project.bookstore.model.Category; public interface CategoryRepository extends JpaRepository { - //Category updateCategoryById(Long id, Category category); } diff --git a/src/main/java/project/bookstore/service/CategoryService.java b/src/main/java/project/bookstore/service/CategoryService.java index 5bf4d00..5a62256 100644 --- a/src/main/java/project/bookstore/service/CategoryService.java +++ b/src/main/java/project/bookstore/service/CategoryService.java @@ -5,7 +5,7 @@ import project.bookstore.dto.category.CategoryRequestDto; public interface CategoryService { - List findAll(); + List findAll(); CategoryDto getById(Long id); diff --git a/src/main/java/project/bookstore/service/impl/CategoryServiceImpl.java b/src/main/java/project/bookstore/service/impl/CategoryServiceImpl.java index 9e72967..e9a3249 100644 --- a/src/main/java/project/bookstore/service/impl/CategoryServiceImpl.java +++ b/src/main/java/project/bookstore/service/impl/CategoryServiceImpl.java @@ -18,8 +18,11 @@ public class CategoryServiceImpl implements CategoryService { private final CategoryMapper categoryMapper; @Override - public List findAll() { - return categoryRepository.findAll(); + public List findAll() { + return categoryRepository.findAll() + .stream() + .map(categoryMapper::toDto) + .toList(); } @Override