-
Notifications
You must be signed in to change notification settings - Fork 0
Order Model || HW #12
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bd749e4
added Order entity and OrderItem entity
Jelors fd1c1a3
added OrderItem dto and OrderResponseDto
Jelors 54cbc42
changed order's entities, added required repositories
Jelors 2da8bb3
added OrderMapper, new UserNotFoundException, implemented OrderService
Jelors 81e7c1b
added Order Controller and yaml files that creates tables
Jelors 7ecfd37
added Order Item service and other little fixes
Jelors 5dea659
added all required things
Jelors 76c918d
added validation for Status dto
Jelors 90217e9
removed OrderItem controller and service, also removed yaml file for …
Jelors 302b7b8
fixes due to mentor's advice
Jelors 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
87 changes: 87 additions & 0 deletions
87
src/main/java/com/springm/store/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,87 @@ | ||
| package com.springm.store.controller; | ||
|
|
||
| import com.springm.store.dto.order.AddShippingAddressDto; | ||
| import com.springm.store.dto.order.OrderResponseDto; | ||
| import com.springm.store.dto.order.UpdateOrderStatusDto; | ||
| import com.springm.store.dto.order.item.OrderItemDto; | ||
| import com.springm.store.service.OrderService; | ||
| 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.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.access.prepost.PreAuthorize; | ||
| 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 orders") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping("/orders") | ||
| public class OrderController { | ||
| private final OrderService orderService; | ||
|
|
||
| @PostMapping | ||
| @PreAuthorize("hasAnyRole('USER', 'ADMIN')") | ||
| @Operation(summary = "Place an order", description = "Place an order") | ||
| public ResponseEntity<OrderResponseDto> placeOrder( | ||
| @RequestBody AddShippingAddressDto shippingAddress | ||
| ) { | ||
| return new ResponseEntity<OrderResponseDto>( | ||
| orderService.placeOrder(shippingAddress), | ||
| HttpStatus.CREATED | ||
| ); | ||
| } | ||
|
|
||
| @GetMapping | ||
| @PreAuthorize("hasAnyRole('USER', 'ADMIN')") | ||
| @Operation(summary = "Receive orders history", description = "Receive orders history") | ||
| public ResponseEntity<List<OrderResponseDto>> getOrderHistory() { | ||
| return new ResponseEntity<List<OrderResponseDto>>( | ||
| orderService.receiveOrderHistory(), | ||
| HttpStatus.OK | ||
| ); | ||
| } | ||
|
|
||
| @PatchMapping("/{id}") | ||
| @PreAuthorize("hasRole('ADMIN')") | ||
| @Operation( | ||
| summary = "Update order status", | ||
| description = "Updates order status with specified ID" | ||
| ) | ||
| public ResponseEntity<OrderResponseDto> updateStatus( | ||
| @PathVariable Long id, | ||
| @RequestBody UpdateOrderStatusDto orderStatusDto | ||
| ) { | ||
| return new ResponseEntity<OrderResponseDto>( | ||
| orderService.updateOrderStatus(id, orderStatusDto), | ||
| HttpStatus.NO_CONTENT | ||
| ); | ||
| } | ||
|
|
||
| @GetMapping("/{orderId}/items") | ||
| @PreAuthorize("hasAnyRole('USER', 'ADMIN')") | ||
| public ResponseEntity<List<OrderItemDto>> getOrderItems(@PathVariable Long orderId) { | ||
| return new ResponseEntity<List<OrderItemDto>>( | ||
| orderService.getItemsByOrderId(orderId), | ||
| HttpStatus.OK | ||
| ); | ||
| } | ||
|
|
||
| @GetMapping("/{orderId}/items/{id}") | ||
| @PreAuthorize("hasAnyRole('USER', 'ADMIN')") | ||
| public ResponseEntity<OrderItemDto> getOrderItem(@PathVariable Long orderId, | ||
| @PathVariable Long id) { | ||
| return new ResponseEntity<OrderItemDto>( | ||
| orderService.getItemByOrderIdAndItemId(orderId, id), | ||
| HttpStatus.OK | ||
| ); | ||
| } | ||
|
|
||
| } |
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/springm/store/dto/order/AddShippingAddressDto.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 com.springm.store.dto.order; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class AddShippingAddressDto { | ||
| @NotBlank | ||
| private String shippingAddress; | ||
| } |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/springm/store/dto/order/OrderResponseDto.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,20 @@ | ||
| package com.springm.store.dto.order; | ||
|
|
||
| import com.springm.store.dto.order.item.OrderItemDto; | ||
| import com.springm.store.model.Order; | ||
| import java.math.BigDecimal; | ||
| import java.time.LocalDateTime; | ||
| import java.util.List; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class OrderResponseDto { | ||
| private Long id; | ||
| private Long userId; | ||
| private LocalDateTime orderDate; | ||
| private BigDecimal total; | ||
| private Order.Status orderStatus; | ||
| private List<OrderItemDto> orderItems; | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/springm/store/dto/order/UpdateOrderStatusDto.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 com.springm.store.dto.order; | ||
|
|
||
| import com.springm.store.model.Order; | ||
| import com.springm.store.validation.order.Status; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class UpdateOrderStatusDto { | ||
| @Status(enumClass = Order.Status.class) | ||
| private Order.Status status; | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/main/java/com/springm/store/dto/order/item/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,15 @@ | ||
| package com.springm.store.dto.order.item; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class OrderItemDto { | ||
| private Long id; | ||
| private Long bookId; | ||
| private String bookTitle; | ||
| private int quantity; | ||
| private BigDecimal price; | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/springm/store/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,24 @@ | ||
| package com.springm.store.mapper; | ||
|
|
||
| import com.springm.store.config.MapperConfig; | ||
| import com.springm.store.dto.cart.item.CartItemDto; | ||
| import com.springm.store.dto.order.item.OrderItemDto; | ||
| import com.springm.store.model.OrderItem; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper(config = MapperConfig.class) | ||
| public interface OrderItemMapper { | ||
|
|
||
| @Mapping(target = "id", ignore = true) | ||
| OrderItemDto toDtoFromCart(CartItemDto cartItemDto); | ||
|
|
||
| @Mapping(source = "bookId", target = "book.id") | ||
| @Mapping(source = "bookTitle", target = "book.title") | ||
| OrderItem toModel(OrderItemDto orderItemDto); | ||
|
|
||
| @Mapping(source = "book.id", target = "bookId") | ||
| @Mapping(source = "book.title", target = "bookTitle") | ||
| OrderItemDto toDto(OrderItem orderItem); | ||
|
|
||
| } |
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,17 @@ | ||
| package com.springm.store.mapper; | ||
|
|
||
| import com.springm.store.config.MapperConfig; | ||
| import com.springm.store.dto.order.OrderResponseDto; | ||
| import com.springm.store.model.Order; | ||
| import org.mapstruct.Mapper; | ||
| import org.mapstruct.Mapping; | ||
|
|
||
| @Mapper(config = MapperConfig.class, uses = OrderItemMapper.class) | ||
| public interface OrderMapper { | ||
| @Mapping(source = "id", target = "id") | ||
| @Mapping(source = "user.id", target = "userId") | ||
| @Mapping(source = "status", target = "orderStatus") | ||
| @Mapping(source = "orderItems", target = "orderItems") | ||
| OrderResponseDto toResponseDto(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
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,66 @@ | ||
| package com.springm.store.model; | ||
|
|
||
| import jakarta.persistence.CascadeType; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| 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.HashSet; | ||
| import java.util.Set; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import lombok.ToString; | ||
| import org.hibernate.annotations.SQLDelete; | ||
| import org.hibernate.annotations.SQLRestriction; | ||
|
|
||
| @Entity | ||
| @Table(name = "orders") | ||
| @SQLDelete(sql = "UPDATE orders SET is_deleted = true WHERE id=?") | ||
| @SQLRestriction("is_deleted = false") | ||
| @Getter | ||
| @Setter | ||
| public class Order { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id", nullable = false) | ||
| private User user; | ||
|
|
||
| @Column(nullable = false) | ||
| @Enumerated(EnumType.STRING) | ||
| 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) | ||
| @ToString.Exclude | ||
| @EqualsAndHashCode.Exclude | ||
| private Set<OrderItem> orderItems = new HashSet<>(); | ||
|
|
||
| public enum Status { | ||
| ORDER_PLACED, | ||
| PENDING, | ||
| DELIVERED, | ||
| COMPLETED | ||
| } | ||
| } |
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,38 @@ | ||
| package com.springm.store.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; | ||
|
|
||
| @Entity | ||
| @Table(name = "order_items") | ||
| @Getter | ||
| @Setter | ||
| public class OrderItem { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(optional = false, fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "order_id", nullable = false) | ||
| private Order order; | ||
|
|
||
| @ManyToOne(optional = false, fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "book_id", nullable = false) | ||
| private Book book; | ||
|
|
||
| @Column(nullable = false) | ||
| private int quantity; | ||
|
|
||
| @Column(nullable = false) | ||
| private BigDecimal price; | ||
| } |
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
2 changes: 2 additions & 0 deletions
2
src/main/java/com/springm/store/repository/cart/ShoppingCartRepository.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 |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| package com.springm.store.repository.cart; | ||
|
|
||
| import com.springm.store.model.ShoppingCart; | ||
| import com.springm.store.model.User; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface ShoppingCartRepository extends JpaRepository<ShoppingCart, Long> { | ||
| Optional<ShoppingCart> findByUserId(Long userId); | ||
|
|
||
| Optional<ShoppingCart> findByUser(User user); | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/springm/store/repository/order/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,10 @@ | ||
| package com.springm.store.repository.order; | ||
|
|
||
| import com.springm.store.model.Order; | ||
| import com.springm.store.model.User; | ||
| import java.util.List; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface OrderRepository extends JpaRepository<Order, Long> { | ||
| List<Order> findByUser(User user); | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/springm/store/repository/order/item/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,13 @@ | ||
| package com.springm.store.repository.order.item; | ||
|
|
||
| import com.springm.store.model.OrderItem; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface OrderItemRepository extends JpaRepository<OrderItem, Long> { | ||
| List<OrderItem> findByOrderId(Long orderId); | ||
|
|
||
| Optional<OrderItem> findByOrderIdAndId(Long orderId, Long id); | ||
|
|
||
| } |
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 com.springm.store.service; | ||
|
|
||
| import com.springm.store.dto.order.AddShippingAddressDto; | ||
| import com.springm.store.dto.order.OrderResponseDto; | ||
| import com.springm.store.dto.order.UpdateOrderStatusDto; | ||
| import com.springm.store.dto.order.item.OrderItemDto; | ||
| import java.util.List; | ||
|
|
||
| public interface OrderService { | ||
| OrderResponseDto placeOrder(AddShippingAddressDto shippingAddressDto); | ||
|
|
||
| List<OrderResponseDto> receiveOrderHistory(); | ||
|
|
||
| OrderResponseDto updateOrderStatus(Long orderId, UpdateOrderStatusDto orderStatus); | ||
|
|
||
| List<OrderItemDto> getItemsByOrderId(Long orderId); | ||
|
|
||
| OrderItemDto getItemByOrderIdAndItemId(Long orderId, Long itemId); | ||
| } |
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.
add validation