-
Notifications
You must be signed in to change notification settings - Fork 0
add Order support #11
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
Open
chupa-ilona
wants to merge
4
commits into
main
Choose a base branch
from
OrderModelSupport
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
86 changes: 86 additions & 0 deletions
86
src/main/java/spring/springbootintro/controller/OrderController.java
This file contains hidden or 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,86 @@ | ||
| package spring.springbootintro.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 org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.http.HttpStatus; | ||
| 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.ResponseStatus; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import spring.springbootintro.dto.CreateOrderRequestDto; | ||
| import spring.springbootintro.dto.OrderDto; | ||
| import spring.springbootintro.dto.OrderItemDto; | ||
| import spring.springbootintro.dto.UpdateOrderStatusRequestDto; | ||
| import spring.springbootintro.model.User; | ||
| import spring.springbootintro.service.OrderService; | ||
|
|
||
| @Tag(name = "Order management", description = "Endpoints for managing user orders") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/orders") | ||
| public class OrderController { | ||
|
|
||
| private final OrderService orderService; | ||
|
|
||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| @PreAuthorize("hasRole('USER')") | ||
| @Operation(summary = "Create an order", | ||
| description = "Creates a new order from the user's current" | ||
| + " shopping cart and clears the cart.") | ||
| public OrderDto createOrder(Authentication authentication, | ||
| @RequestBody @Valid CreateOrderRequestDto requestDto) { | ||
| User user = (User) authentication.getPrincipal(); | ||
| return orderService.createOrder(user.getId(), requestDto); | ||
| } | ||
|
|
||
| @GetMapping | ||
| @PreAuthorize("hasRole('USER')") | ||
| @Operation(summary = "Get order history", | ||
| description = "Retrieves the order history for the currently authenticated user.") | ||
| public Page<OrderDto> getOrderHistory(Authentication authentication, Pageable pageable) { | ||
| User user = (User) authentication.getPrincipal(); | ||
| return orderService.getOrderHistory(user.getId(), pageable); | ||
| } | ||
|
|
||
| @PatchMapping("/{id}") | ||
| @PreAuthorize("hasRole('ADMIN')") | ||
| @Operation(summary = "Update order status", | ||
| description = "Updates the status of an existing order. Available only for admins.") | ||
| public OrderDto updateOrderStatus(@PathVariable Long id, | ||
| @RequestBody @Valid UpdateOrderStatusRequestDto requestDto) { | ||
| return orderService.updateOrderStatus(id, requestDto); | ||
| } | ||
|
|
||
| @GetMapping("/{orderId}/items") | ||
| @PreAuthorize("hasRole('USER')") | ||
| @Operation(summary = "Get all items from an order", | ||
| description = "Retrieves all OrderItems for a specific order belonging to the user.") | ||
| public List<OrderItemDto> getOrderItems(Authentication authentication, | ||
| @PathVariable Long orderId) { | ||
| User user = (User) authentication.getPrincipal(); | ||
| return orderService.getOrderItems(user.getId(), orderId); | ||
| } | ||
|
|
||
| @GetMapping("/{orderId}/items/{itemId}") | ||
| @PreAuthorize("hasRole('USER')") | ||
| @Operation(summary = "Get a specific item from an order", | ||
| description = "Retrieves a specific OrderItem by its ID within a specific order.") | ||
| public OrderItemDto getOrderItem(Authentication authentication, | ||
| @PathVariable Long orderId, | ||
| @PathVariable Long itemId) { | ||
| User user = (User) authentication.getPrincipal(); | ||
| return orderService.getOrderItem(user.getId(), orderId, itemId); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/spring/springbootintro/dto/CreateOrderRequestDto.java
This file contains hidden or 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,12 @@ | ||
| package spring.springbootintro.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class CreateOrderRequestDto { | ||
| @NotBlank | ||
| private String shippingAddress; | ||
| } |
This file contains hidden or 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,19 @@ | ||
| package spring.springbootintro.dto; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Set; | ||
| import lombok.Data; | ||
| import spring.springbootintro.model.OrderItem; | ||
| import spring.springbootintro.model.Status; | ||
|
|
||
| @Data | ||
| public class OrderDto { | ||
| private Long id; | ||
| private Long userId; | ||
| private Set<OrderItem> orderItems; | ||
| private LocalDateTime orderDate; | ||
| private BigDecimal total; | ||
| private Status status; | ||
|
|
||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/spring/springbootintro/dto/OrderItemDto.java
This file contains hidden or 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.springbootintro.dto; | ||
|
|
||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class OrderItemDto { | ||
| private Long id; | ||
| private Long bookId; | ||
| private int quantity; | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/spring/springbootintro/dto/UpdateOrderStatusRequestDto.java
This file contains hidden or 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 spring.springbootintro.dto; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import spring.springbootintro.model.Status; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class UpdateOrderStatusRequestDto { | ||
| @NotBlank | ||
| private Status status; | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/spring/springbootintro/exception/OrderProcessingException.java
This file contains hidden or 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 spring.springbootintro.exception; | ||
|
|
||
| public class OrderProcessingException extends RuntimeException { | ||
| public OrderProcessingException(String message) { | ||
| super(message); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/spring/springbootintro/mapper/OrderItemMapper.java
This file contains hidden or 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,18 @@ | ||
| package spring.springbootintro.mapper; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import spring.springbootintro.dto.OrderItemDto; | ||
| import spring.springbootintro.model.OrderItem; | ||
|
|
||
| @Mapper(config = spring.springbootintro.config.MapperConfig.class) | ||
| public interface OrderItemMapper { | ||
|
|
||
| @Mapping(source = "book.id", target = "bookId") | ||
| OrderItemDto toDto(OrderItem orderItem); | ||
|
|
||
| @Mapping(target = "id", ignore = true) | ||
| @Mapping(target = "deleted", ignore = true) | ||
| OrderItem toModel(OrderItemDto orderItemDto); | ||
|
|
||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/spring/springbootintro/mapper/OrderMapper.java
This file contains hidden or 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,14 @@ | ||
| package spring.springbootintro.mapper; | ||
|
|
||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
| import spring.springbootintro.dto.OrderDto; | ||
| import spring.springbootintro.model.Order; | ||
|
|
||
| @Mapper(config = spring.springbootintro.config.MapperConfig.class, uses = OrderItemMapper.class) | ||
| public interface OrderMapper { | ||
|
|
||
| @Mapping(source = "user.id", target = "userId") | ||
| OrderDto toDto(Order order); | ||
|
|
||
| } |
This file contains hidden or 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,55 @@ | ||
| package spring.springbootintro.model; | ||
|
|
||
| import jakarta.persistence.CascadeType; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.OneToMany; | ||
| import jakarta.persistence.Table; | ||
| import java.math.BigDecimal; | ||
| import java.time.LocalDateTime; | ||
| import java.util.Set; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.hibernate.annotations.SQLDelete; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| @SQLDelete(sql = "UPDATE orders SET is_deleted = true WHERE id = ?") | ||
| @SQLRestriction("is_deleted = false") | ||
| @Entity | ||
| @Table(name = "orders") | ||
| @Getter | ||
| @Setter | ||
| public class Order { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
| @JoinColumn(name = "user_id", nullable = false) | ||
| private User user; | ||
|
|
||
| @Column(nullable = false) | ||
| private Status status; | ||
|
|
||
| @Column(nullable = false) | ||
| private BigDecimal total; | ||
|
|
||
| @Column(nullable = false) | ||
| private LocalDateTime orderDate; | ||
|
|
||
| @Column(nullable = false) | ||
| private String shippingAddress; | ||
|
|
||
| @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) | ||
| private Set<OrderItem> orderItems; | ||
|
|
||
| private boolean isDeleted = false; | ||
|
|
||
| } |
This file contains hidden or 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,45 @@ | ||
| package spring.springbootintro.model; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import java.math.BigDecimal; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.hibernate.annotations.SQLDelete; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| @SQLDelete(sql = "UPDATE order_items SET is_deleted = true WHERE id = ?") | ||
| @SQLRestriction("is_deleted = false") | ||
| @Entity | ||
| @Table(name = "order_items") | ||
| @Getter | ||
| @Setter | ||
| public class OrderItem { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
| @JoinColumn(name = "order_id") | ||
| private Order order; | ||
|
|
||
| @ManyToOne | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add LAZY |
||
| @JoinColumn(name = "book_id") | ||
| private Book book; | ||
|
|
||
| @Column | ||
| private int quantity; | ||
|
chupa-ilona marked this conversation as resolved.
|
||
|
|
||
| @Column(nullable = false) | ||
| private BigDecimal price; | ||
|
|
||
| private boolean isDeleted = false; | ||
| } | ||
This file contains hidden or 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.springbootintro.model; | ||
|
|
||
| public enum Status { | ||
| COMPLETED, | ||
| PENDING, | ||
| DELIVERED, | ||
| CANCELLED | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/spring/springbootintro/repository/OrderItemRepository.java
This file contains hidden or 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.springbootintro.repository; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
| import spring.springbootintro.model.OrderItem; | ||
|
|
||
| @Repository | ||
| public interface OrderItemRepository extends JpaRepository<OrderItem, Long> { | ||
| } |
16 changes: 16 additions & 0 deletions
16
src/main/java/spring/springbootintro/repository/OrderRepository.java
This file contains hidden or 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 spring.springbootintro.repository; | ||
|
|
||
| import java.util.Optional; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
| import spring.springbootintro.model.Order; | ||
|
|
||
| @Repository | ||
| public interface OrderRepository extends JpaRepository<Order, Long> { | ||
|
|
||
| Page<Order> findAllByUserId(Long userId, Pageable pageable); | ||
|
|
||
| Optional<Order> findByIdAndUserId(Long orderId, Long userId); | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/java/spring/springbootintro/service/OrderService.java
This file contains hidden or 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,23 @@ | ||
| package spring.springbootintro.service; | ||
|
|
||
| import java.util.List; | ||
| import org.springframework.data.domain.Page; | ||
| import org.springframework.data.domain.Pageable; | ||
| import spring.springbootintro.dto.CreateOrderRequestDto; | ||
| import spring.springbootintro.dto.OrderDto; | ||
| import spring.springbootintro.dto.OrderItemDto; | ||
| import spring.springbootintro.dto.UpdateOrderStatusRequestDto; | ||
|
|
||
| public interface OrderService { | ||
|
|
||
| Page<OrderDto> getOrderHistory(Long userId, Pageable pageable); | ||
|
|
||
| OrderDto createOrder(Long userId, CreateOrderRequestDto requestDto); | ||
|
|
||
| OrderDto updateOrderStatus(Long orderId, UpdateOrderStatusRequestDto orderDto); | ||
|
|
||
| List<OrderItemDto> getOrderItems(Long userId, Long orderId); | ||
|
|
||
| OrderItemDto getOrderItem(Long userId, Long orderId, Long itemId); | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.