-
Notifications
You must be signed in to change notification settings - Fork 0
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 Category entity with all related dto, service, mapper and controller. #12
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9eaa94b
Changed @Component annotation to @Service in UserServiceImpl
nklimovych 510f3c0
Added Category entity with all related dto, service and controller. A…
nklimovych 00635f0
Merge branch 'refs/heads/main' into category-impl
nklimovych 0d14de8
Added @PreAuthorize to all relevant endpoints
nklimovych 996f91a
Added @RequiredArgsConstructor instead of constructor autowiring. Add…
nklimovych 4c17749
Removed empty line
nklimovych File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.