-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Category entity with all related dto, service, mapper and contr…
…oller. (#12) * Added Category entity with all related dto, service, and controller. Added mapper for Category. Created DB tables categories and books_categories. * Added @PreAuthorize to all relevant endpoints * Added @requiredargsconstructor instead of constructor autowiring. Added findCategoryByIdOrElseThrow() method at CategoryServiceImpl.
- Loading branch information
1 parent
7e352d8
commit a94f80f
Showing
22 changed files
with
388 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/mate/academy/bookstore/controller/CategoryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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.book.BookDtoWithoutCategoryIds; | ||
import mate.academy.bookstore.dto.category.CategoryDto; | ||
import mate.academy.bookstore.service.BookService; | ||
import mate.academy.bookstore.service.CategoryService; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@Tag(name = "Category management", description = "Endpoints for category management") | ||
@RequestMapping(value = "/categories") | ||
public class CategoryController { | ||
private final CategoryService categoryService; | ||
private final BookService bookService; | ||
|
||
@PreAuthorize("hasRole('ADMIN')") | ||
@PostMapping | ||
@Operation(summary = "Create new category", description = "Create category with generated ID") | ||
public CategoryDto createCategory(@RequestBody @Valid CategoryDto categoryDto) { | ||
return categoryService.save(categoryDto); | ||
} | ||
|
||
@PreAuthorize("hasAnyRole('USER', 'ADMIN')") | ||
@GetMapping | ||
@Operation(summary = "Get all categories", description = "Get a list of existing categories") | ||
public List<CategoryDto> getAll(Pageable pageable) { | ||
return categoryService.findAll(pageable); | ||
} | ||
|
||
@PreAuthorize("hasAnyRole('USER', 'ADMIN')") | ||
@GetMapping("/{id}") | ||
@Operation(summary = "Get category by ID", description = "Get category by ID, if there is one") | ||
public CategoryDto getCategoryById(@PathVariable Long id) { | ||
return categoryService.getById(id); | ||
} | ||
|
||
@PreAuthorize("hasRole('ADMIN')") | ||
@PutMapping("/{id}") | ||
@Operation(summary = "Update category by ID", description = "Update category by ID, if exists") | ||
public CategoryDto updateCategory(@PathVariable Long id, | ||
@RequestBody @Valid CategoryDto categoryDto) { | ||
return categoryService.update(id, categoryDto); | ||
} | ||
|
||
@PreAuthorize("hasRole('ADMIN')") | ||
@DeleteMapping("/{id}") | ||
@Operation(summary = "Delete category by ID", description = "Delete category by ID, if exists") | ||
public void deleteCategory(@PathVariable Long id) { | ||
categoryService.delete(id); | ||
} | ||
|
||
@PreAuthorize("hasAnyRole('USER', 'ADMIN')") | ||
@GetMapping("/{id}/books") | ||
@Operation(summary = "Get books by category ID", | ||
description = "Get a list of books by category ID") | ||
public List<BookDtoWithoutCategoryIds> getBooksByCategoryId( | ||
@PathVariable Long id, | ||
Pageable pageable | ||
) { | ||
return bookService.getAllBookByCategoryId(id, pageable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/mate/academy/bookstore/dto/book/BookDtoWithoutCategoryIds.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package mate.academy.bookstore.dto.book; | ||
|
||
import java.math.BigDecimal; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class BookDtoWithoutCategoryIds { | ||
private Long id; | ||
private String title; | ||
private String author; | ||
private String isbn; | ||
private BigDecimal price; | ||
private String description; | ||
private String coverImage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/bookstore/dto/category/CategoryDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.bookstore.dto.category; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Size; | ||
|
||
public record CategoryDto( | ||
Long id, | ||
@NotBlank | ||
@Size(min = 4, max = 24, message = "Category name must be 4 to 24 characters long") | ||
String name, | ||
String description | ||
) { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/bookstore/exception/DuplicateEntityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.bookstore.exception; | ||
|
||
public class DuplicateEntityException extends RuntimeException { | ||
public DuplicateEntityException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 21 additions & 2 deletions
23
src/main/java/mate/academy/bookstore/mapper/BookMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,36 @@ | ||
package mate.academy.bookstore.mapper; | ||
|
||
import java.util.Collections; | ||
import java.util.stream.Collectors; | ||
import mate.academy.bookstore.config.MapperConfig; | ||
import mate.academy.bookstore.dto.book.BookDto; | ||
import mate.academy.bookstore.dto.book.BookRequestDto; | ||
import mate.academy.bookstore.dto.book.BookDtoWithoutCategoryIds; | ||
import mate.academy.bookstore.dto.book.CreateBookRequestDto; | ||
import mate.academy.bookstore.model.Book; | ||
import mate.academy.bookstore.model.Category; | ||
import org.mapstruct.AfterMapping; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.MappingTarget; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface BookMapper { | ||
@Mapping(target = "categories", ignore = true) | ||
BookDto toDto(Book book); | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "deleted", ignore = true) | ||
Book toModel(BookRequestDto requestDto); | ||
Book toEntity(CreateBookRequestDto requestDto); | ||
|
||
BookDtoWithoutCategoryIds toDtoWithoutCategories(Book book); | ||
|
||
@AfterMapping | ||
default void setCategoryIds(@MappingTarget BookDto bookDto, Book book) { | ||
if (book.getCategories() == null) { | ||
bookDto.setCategories(Collections.emptySet()); | ||
} | ||
bookDto.setCategories(book.getCategories().stream() | ||
.map(Category::getId) | ||
.collect(Collectors.toSet())); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/bookstore/mapper/CategoryMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.bookstore.mapper; | ||
|
||
import mate.academy.bookstore.config.MapperConfig; | ||
import mate.academy.bookstore.dto.category.CategoryDto; | ||
import mate.academy.bookstore.model.Category; | ||
import org.mapstruct.Mapper; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface CategoryMapper { | ||
CategoryDto toDto(Category category); | ||
|
||
Category toEntity(CategoryDto categoryDto); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mate.academy.bookstore.model; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.SoftDelete; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@SoftDelete | ||
@Table(name = "categories") | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(nullable = false, unique = true) | ||
private String name; | ||
private String description; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/bookstore/repository/book/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
package mate.academy.bookstore.repository.book; | ||
|
||
import java.util.List; | ||
import mate.academy.bookstore.model.Book; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.EntityGraph; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> { | ||
@EntityGraph(attributePaths = {"categories"}) | ||
Book findBookByIsbn(String isbn); | ||
|
||
@Query("SELECT b FROM Book b LEFT JOIN FETCH b.categories c WHERE c.id = :categoryId") | ||
List<Book> findAllBooksByCategoryId(@Param("categoryId") Long categoryId, Pageable pageable); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/bookstore/repository/category/CategoryRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.bookstore.repository.category; | ||
|
||
import mate.academy.bookstore.model.Category; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
|
||
Category findByName(String categoryName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/mate/academy/bookstore/service/CategoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mate.academy.bookstore.service; | ||
|
||
import java.util.List; | ||
import mate.academy.bookstore.dto.category.CategoryDto; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CategoryService { | ||
List<CategoryDto> findAll(Pageable pageable); | ||
|
||
CategoryDto getById(Long id); | ||
|
||
CategoryDto save(CategoryDto categoryDto); | ||
|
||
CategoryDto update(Long id, CategoryDto categoryDto); | ||
|
||
void delete(Long id); | ||
} |
Oops, something went wrong.