-
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.
- Loading branch information
Showing
19 changed files
with
381 additions
and
10 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
79 changes: 79 additions & 0 deletions
79
src/main/java/com/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,79 @@ | ||
package com.bookstore.controller; | ||
|
||
import com.bookstore.dto.book.BookDtoWithoutCategoryIds; | ||
import com.bookstore.dto.category.CategoryDto; | ||
import com.bookstore.service.BookService; | ||
import com.bookstore.service.CategoryService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
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.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Category management", description = "Endpoints for managing categories") | ||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping(value = "/api/categories") | ||
public class CategoryController { | ||
private final CategoryService categoryService; | ||
private final BookService bookService; | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.CREATED) | ||
@PreAuthorize("hasRole('ADMIN')") | ||
@Operation(summary = "Create a new category(only for admins)", | ||
description = "Create a new category") | ||
public CategoryDto createCategory(@RequestBody CategoryDto categoryDto) { | ||
return categoryService.save(categoryDto); | ||
} | ||
|
||
@GetMapping | ||
@PreAuthorize("hasRole('USER')") | ||
@Operation(summary = "Get all categories", description = "Get all categories") | ||
public List<CategoryDto> getAll(Pageable pageable) { | ||
return categoryService.findAll(pageable); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
@PreAuthorize("hasRole('USER')") | ||
@Operation(summary = "Get category by specific id", description = "Get category by specific id") | ||
public CategoryDto getCategoryById(@PathVariable Long id) { | ||
return categoryService.getById(id); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
@ResponseStatus(HttpStatus.ACCEPTED) | ||
@PreAuthorize("hasRole('ADMIN')") | ||
@Operation(summary = "Update category(only for admins)", description = "Update category") | ||
public CategoryDto updateCategory(@PathVariable Long id, | ||
@RequestBody CategoryDto categoryDto) { | ||
return categoryService.update(id, categoryDto); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@PreAuthorize("hasRole('ADMIN')") | ||
@Operation(summary = "Delete book(only for admins)", description = "Delete book") | ||
public void deleteCategory(@PathVariable Long id) { | ||
categoryService.deleteById(id); | ||
} | ||
|
||
@GetMapping("/{id}/books") | ||
@PreAuthorize("hasRole('USER')") | ||
@Operation(summary = "Get books by specific category_id", | ||
description = "Get books by specific category_id") | ||
public List<BookDtoWithoutCategoryIds> getBooksByCategoryId(@PathVariable Long id, | ||
Pageable pageable) { | ||
return bookService.findAllByCategoryId(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/com/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 com.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
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 com.bookstore.dto.category; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class CategoryDto { | ||
private String name; | ||
private String description; | ||
} |
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,13 @@ | ||
package com.bookstore.mapper; | ||
|
||
import com.bookstore.config.MapperConfig; | ||
import com.bookstore.dto.category.CategoryDto; | ||
import com.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,31 @@ | ||
package com.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.EqualsAndHashCode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hibernate.annotations.SQLDelete; | ||
import org.hibernate.annotations.Where; | ||
|
||
@Getter | ||
@Setter | ||
@EqualsAndHashCode | ||
@Entity | ||
@SQLDelete(sql = "UPDATE categories SET is_deleted=true WHERE id=?") | ||
@Where(clause = "is_deleted=false") | ||
@Table(name = "categories") | ||
public class Category { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(nullable = false) | ||
private String name; | ||
private String description; | ||
@Column(name = "is_deleted", nullable = false) | ||
private boolean isDeleted = false; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/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,27 @@ | ||
package com.bookstore.repository.book; | ||
|
||
import com.bookstore.model.Book; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.domain.Specification; | ||
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; | ||
|
||
public interface BookRepository extends JpaRepository<Book, Long>, | ||
JpaSpecificationExecutor<Book> { | ||
@Query("FROM Book b JOIN FETCH b.categories c " | ||
+ "WHERE c.id = :categoryId") | ||
List<Book> findAllByCategoryId(Long categoryId, Pageable pageable); | ||
|
||
@EntityGraph(attributePaths = "categories") | ||
Optional<Book> findById(Long id); | ||
|
||
@Query("FROM Book b LEFT JOIN FETCH b.categories c") | ||
List<Book> findAllPageable(Pageable pageable); | ||
|
||
@EntityGraph(attributePaths = "categories") | ||
List<Book> findAll(Specification<Book> bookSpecification); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/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 com.bookstore.repository.category; | ||
|
||
import com.bookstore.model.Category; | ||
import java.util.Set; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CategoryRepository extends JpaRepository<Category, Long> { | ||
Set<Category> findByIdIn(Set<Long> categoryIds); | ||
} |
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,17 @@ | ||
package com.bookstore.service; | ||
|
||
import com.bookstore.dto.category.CategoryDto; | ||
import java.util.List; | ||
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 deleteById(Long id); | ||
} |
Oops, something went wrong.