-
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 Order entity with all related dto, service, mapper and controll…
…er (#14) * Add Order, OrderItem entities, and Status enum. * Added repository and dto layers, added mappers for related Order & OrderItem entities. * Added liquibase change files to create orders and order-items tables in DB. Fixed checkstyle issues. * Added service and controller layers. Code refactored. Fixed issue in ShoppingCartRepository with findByUser query. * Resolved issues: - Refactored order saving method in OrderServiceImpl. - Fixed issues with lazy initialization. - Added authorized user in the getOrderItem method in the controller so that any user cannot get another user's data. - Added equals & hashcode, and toString in OrderItem entity. - Added equals and hashcode to all entities. - Resolved issue when anyone can search for any order items. * Resolved issue with shopping cart: now when an order is placed, the shopping cart is removed. Removed soft delete in ShoppingCart * Fixed the issue where categories were not being added to saved books. * Added exclude constraint for EqualsAndHashcode and ToString annotations. * Added @CreationTimestamp for orderDate in Order entity.
- Loading branch information
1 parent
56913c9
commit 6340daa
Showing
35 changed files
with
641 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,7 @@ build/ | |
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
.vscode/ | ||
|
||
### Docker ### | ||
/.env |
81 changes: 81 additions & 0 deletions
81
src/main/java/mate/academy/bookstore/controller/OrderController.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,81 @@ | ||
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.order.OrderItemResponseDto; | ||
import mate.academy.bookstore.dto.order.OrderRequestDto; | ||
import mate.academy.bookstore.dto.order.OrderResponseDto; | ||
import mate.academy.bookstore.dto.order.OrderStatusDto; | ||
import mate.academy.bookstore.model.User; | ||
import mate.academy.bookstore.service.OrderService; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Order Management", description = "Endpoints for managing the order") | ||
@RestController | ||
@RequestMapping("/orders") | ||
@RequiredArgsConstructor | ||
public class OrderController { | ||
private final OrderService orderService; | ||
|
||
@PostMapping | ||
@PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')") | ||
@Operation(summary = "Create an order", | ||
description = "Creates a new order with the items in the current user cart") | ||
public void createOrder( | ||
@Valid @RequestBody OrderRequestDto orderDto, | ||
@AuthenticationPrincipal User user) { | ||
orderService.createOrder(orderDto, user); | ||
} | ||
|
||
@GetMapping | ||
@PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')") | ||
@Operation(summary = "Get orders history", | ||
description = "Retrieves the order history for the current user") | ||
public List<OrderResponseDto> getOrdersHistory( | ||
@AuthenticationPrincipal User currentUser) { | ||
return orderService.getAllOrders(currentUser); | ||
} | ||
|
||
@PatchMapping("/{orderId}") | ||
@PreAuthorize("hasAnyRole('ROLE_ADMIN')") | ||
@Operation(summary = "Update an order status", | ||
description = "Updates the status of an existing order identified by its id") | ||
public void updateOrderStatus( | ||
@Valid @RequestBody OrderStatusDto statusDto, | ||
@PathVariable Long orderId, | ||
@AuthenticationPrincipal User user) { | ||
orderService.updateStatus(statusDto, orderId, user.getId()); | ||
} | ||
|
||
@GetMapping("/{orderId}/items") | ||
@PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')") | ||
@Operation(summary = "Get all order items from order", | ||
description = "Retrieves all items associated for authenticated user") | ||
public List<OrderItemResponseDto> getAllOrderItems( | ||
@PathVariable Long orderId, | ||
@AuthenticationPrincipal User user) { | ||
return orderService.getAllOrderItems(orderId, user); | ||
} | ||
|
||
@GetMapping("/{orderId}/items/{itemId}") | ||
@PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')") | ||
@Operation(summary = "Get item from order", | ||
description = "Retrieves a specific item from an order identified by its id") | ||
public OrderItemResponseDto getOrderItem( | ||
@PathVariable Long orderId, | ||
@PathVariable Long itemId, | ||
@AuthenticationPrincipal User user) { | ||
return orderService.getOrderItem(orderId, itemId, user); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/bookstore/dto/order/OrderItemResponseDto.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,10 @@ | ||
package mate.academy.bookstore.dto.order; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class OrderItemResponseDto { | ||
private Long id; | ||
private Long bookId; | ||
private int quantity; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/bookstore/dto/order/OrderRequestDto.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,10 @@ | ||
package mate.academy.bookstore.dto.order; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class OrderRequestDto { | ||
@NotBlank(message = "Shipping address can not be empty") | ||
private String shippingAddress; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/mate/academy/bookstore/dto/order/OrderResponseDto.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,16 @@ | ||
package mate.academy.bookstore.dto.order; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDateTime; | ||
import java.util.Set; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class OrderResponseDto { | ||
private Long id; | ||
private Long userId; | ||
private Set<OrderItemResponseDto> orderItems; | ||
private LocalDateTime orderDate; | ||
private BigDecimal total; | ||
private String status; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/bookstore/dto/order/OrderStatusDto.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,11 @@ | ||
package mate.academy.bookstore.dto.order; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Data; | ||
import mate.academy.bookstore.model.order.Status; | ||
|
||
@Data | ||
public class OrderStatusDto { | ||
@NotNull | ||
private Status status; | ||
} |
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/mapper/OrderItemMapper.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.order.OrderItemResponseDto; | ||
import mate.academy.bookstore.model.order.OrderItem; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface OrderItemMapper { | ||
@Mapping(target = "bookId", source = "book.id") | ||
OrderItemResponseDto toDto(OrderItem orderItem); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/bookstore/mapper/OrderMapper.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.order.OrderResponseDto; | ||
import mate.academy.bookstore.model.order.Order; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(config = MapperConfig.class, uses = OrderItemMapper.class) | ||
public interface OrderMapper { | ||
@Mapping(source = "user.id", target = "userId") | ||
OrderResponseDto toDto(Order order); | ||
} |
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
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
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
Oops, something went wrong.