Skip to content

Commit

Permalink
category_hw, swagger, @Valid added to CategoryController
Browse files Browse the repository at this point in the history
  • Loading branch information
ChabVlad committed Sep 24, 2024
1 parent 51266e9 commit 61a6182
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
29 changes: 26 additions & 3 deletions src/main/java/project/bookstore/controller/CategoryController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<CategoryDto> 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<BookDtoWithoutCategoryIds> getBooksByCategoryId(@PathVariable Long id) {
return bookService.getAllByCategoriesId(id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/project/bookstore/model/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
import project.bookstore.model.Category;

public interface CategoryRepository extends JpaRepository<Category, Long> {
//Category updateCategoryById(Long id, Category category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import project.bookstore.dto.category.CategoryRequestDto;

public interface CategoryService {
List findAll();
List<CategoryDto> findAll();

CategoryDto getById(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public class CategoryServiceImpl implements CategoryService {
private final CategoryMapper categoryMapper;

@Override
public List findAll() {
return categoryRepository.findAll();
public List<CategoryDto> findAll() {
return categoryRepository.findAll()
.stream()
.map(categoryMapper::toDto)
.toList();
}

@Override
Expand Down

0 comments on commit 61a6182

Please sign in to comment.