-
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
1 parent
216972d
commit 2efb85c
Showing
5 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/spring/boot/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 spring.boot.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 java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.Authentication; | ||
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; | ||
import spring.boot.bookstore.dto.order.OrderRequestDto; | ||
import spring.boot.bookstore.dto.order.OrderResponseDto; | ||
import spring.boot.bookstore.dto.order.OrderUpdateDto; | ||
import spring.boot.bookstore.dto.orderitem.OrderItemResponseDto; | ||
import spring.boot.bookstore.model.Order; | ||
import spring.boot.bookstore.model.User; | ||
import spring.boot.bookstore.service.order.OrderService; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@RequestMapping("/orders") | ||
@Tag(name = "Order Controller management", description = "Endpoints for managing users orders") | ||
public class OrderController { | ||
private static final Logger logger = LogManager.getLogger(OrderController.class); | ||
private final OrderService orderService; | ||
|
||
@PostMapping | ||
@Operation(summary = "Place order", description = "place order") | ||
public OrderResponseDto create(Authentication authentication, | ||
@RequestBody | ||
@Valid OrderRequestDto orderRequestDto) { | ||
User user = (User) authentication.getPrincipal(); | ||
logger.info("Placing a new Order."); | ||
return orderService.create(user.getId(), orderRequestDto); | ||
} | ||
|
||
@Operation(summary = "Get all users order history", description = "Get all users order history") | ||
@GetMapping | ||
public List<OrderResponseDto> findAll(Authentication authentication, Pageable pageable) { | ||
User user = (User) authentication.getPrincipal(); | ||
logger.info("Find all Orders."); | ||
return orderService.findAllOrders(user.getId(), pageable); | ||
} | ||
|
||
@Operation(summary = "update order", description = "update order status") | ||
@PatchMapping("/{id}") | ||
@PreAuthorize("hasRole('ROLE_ADMIN')") | ||
public OrderResponseDto updateOrderStatus(@PathVariable Long id, | ||
@RequestBody OrderUpdateDto orderUpdateDto) { | ||
logger.info("updating Order Status by id." + id); | ||
return orderService.updateOrderStatus(id, Order.Status | ||
.valueOf(String.valueOf(orderUpdateDto.getStatus()))); | ||
} | ||
|
||
@Operation(summary = "", description = "") | ||
@GetMapping("/{orderId}/items") | ||
@PreAuthorize("hasRole('USER')") | ||
public Set<OrderItemResponseDto> findAllOrderItems(@PathVariable Long orderId) { | ||
logger.info("find All Order Items using id" + orderId); | ||
return orderService.findAllOrderItems(orderId); | ||
} | ||
|
||
@Operation(summary = "", description = "") | ||
@GetMapping("/{orderId}/items/{itemId}") | ||
@PreAuthorize("hasRole('USER')") | ||
public OrderItemResponseDto findOrderItemById(@PathVariable Long orderId, | ||
@PathVariable Long itemId) { | ||
logger.info("find Order Item By Id" + orderId + "and here Item id : " + itemId); | ||
return orderService.findOrderItemById(orderId, itemId); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/spring/boot/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,8 @@ | ||
package spring.boot.bookstore.dto.order; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class OrderRequestDto { | ||
private String shippingAddress; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/spring/boot/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,17 @@ | ||
package spring.boot.bookstore.dto.order; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDateTime; | ||
import java.util.Set; | ||
import lombok.Data; | ||
import spring.boot.bookstore.dto.orderitem.OrderItemResponseDto; | ||
|
||
@Data | ||
public class OrderResponseDto { | ||
private Long id; | ||
private Long userId; | ||
private Set<OrderItemResponseDto> orderItems; | ||
private LocalDateTime orderTime; | ||
private BigDecimal totalPrice; | ||
private String status; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/spring/boot/bookstore/dto/order/OrderUpdateDto.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 spring.boot.bookstore.dto.order; | ||
|
||
import lombok.Data; | ||
import spring.boot.bookstore.model.Order; | ||
|
||
@Data | ||
public class OrderUpdateDto { | ||
private Order.Status status; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/spring/boot/bookstore/dto/orderitem/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 spring.boot.bookstore.dto.orderitem; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class OrderItemResponseDto { | ||
private Long id; | ||
private Long bookId; | ||
private int quantity; | ||
} |