-
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
12 changed files
with
298 additions
and
31 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
44 changes: 44 additions & 0 deletions
44
src/main/java/com/kasina/automobileapi/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,44 @@ | ||
package com.kasina.automobileapi.controller; | ||
|
||
import com.kasina.automobileapi.dto.OrderRequest; | ||
import com.kasina.automobileapi.dto.ProductDto; | ||
import com.kasina.automobileapi.model.Order; | ||
import com.kasina.automobileapi.model.Product; | ||
import com.kasina.automobileapi.model.User; | ||
import com.kasina.automobileapi.service.OrderService; | ||
import com.kasina.automobileapi.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.crossstore.ChangeSetPersister; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/categories") | ||
@RequiredArgsConstructor | ||
public class OrderController { | ||
private final OrderService orderService; | ||
private final UserService userService; | ||
|
||
|
||
@PostMapping | ||
public ResponseEntity<?> createOrder(@RequestBody OrderRequest orderRequest){ | ||
Order order = orderService.createOrder(orderRequest); | ||
return ResponseEntity.ok(order); | ||
} | ||
|
||
@GetMapping("/user/{userId}") | ||
public ResponseEntity<List<Order>> getOrdersByUser(@PathVariable Long userId) { | ||
User user = userService.getUserById(userId); | ||
List<Order> orders = orderService.getOrdersByUser(user); | ||
return ResponseEntity.ok(orders); | ||
} | ||
|
||
@GetMapping("/{orderId}") | ||
public ResponseEntity<Order> getOrderById(@PathVariable Long orderId) { | ||
User currentUser = userService.getCurrentUser(); | ||
Order order = orderService.getOrderByIdAndUser(orderId, currentUser); | ||
return ResponseEntity.ok(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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/kasina/automobileapi/dto/OrderItemRequest.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 com.kasina.automobileapi.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
@Data | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
public class OrderItemRequest { | ||
private Long productId; | ||
private int quantity; | ||
private BigDecimal subTotal; | ||
|
||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/kasina/automobileapi/dto/OrderRequest.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,18 @@ | ||
package com.kasina.automobileapi.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
@Data | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
public class OrderRequest { | ||
private List<OrderItemRequest> items; | ||
private BigDecimal price; | ||
private String status; | ||
private String shippingAddress; | ||
// Add more fields for payment details, etc. | ||
} |
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,49 @@ | ||
package com.kasina.automobileapi.model; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Setter | ||
@Getter | ||
@Builder | ||
@Entity | ||
@Table(name = "orders") | ||
@RequiredArgsConstructor | ||
public class Order { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@CreatedDate | ||
private LocalDateTime orderDate; | ||
private String status; | ||
private String shippingAddress; | ||
private BigDecimal totalPrice; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL) | ||
private List<OrderItem> orderItems = new ArrayList<>(); | ||
|
||
public void addOrderItem(OrderItem orderItem) { | ||
orderItems.add(orderItem); | ||
orderItem.setOrder(this); | ||
} | ||
|
||
public void removeOrderItem(OrderItem orderItem) { | ||
orderItems.remove(orderItem); | ||
orderItem.setOrder(null); | ||
} | ||
|
||
|
||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/kasina/automobileapi/model/OrderItem.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,33 @@ | ||
package com.kasina.automobileapi.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Setter | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Entity | ||
public class OrderItem { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@ManyToOne | ||
@JoinColumn(name = "product_id") | ||
private Product product; | ||
private int quantity; | ||
private BigDecimal subTotal; | ||
|
||
@ManyToOne | ||
@JsonIgnore | ||
private Order order; | ||
|
||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/kasina/automobileapi/repository/OrderRepository.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.kasina.automobileapi.repository; | ||
|
||
import com.kasina.automobileapi.model.Order; | ||
import com.kasina.automobileapi.model.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface OrderRepository extends JpaRepository<Order, Long> { | ||
List<Order> findByUser(User user); | ||
Optional<Order> findByIdAndUser(Long orderId, User 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
65 changes: 65 additions & 0 deletions
65
src/main/java/com/kasina/automobileapi/service/OrderService.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,65 @@ | ||
package com.kasina.automobileapi.service; | ||
|
||
import com.kasina.automobileapi.dto.OrderItemRequest; | ||
import com.kasina.automobileapi.dto.OrderRequest; | ||
import com.kasina.automobileapi.model.Order; | ||
import com.kasina.automobileapi.model.OrderItem; | ||
import com.kasina.automobileapi.model.Product; | ||
import com.kasina.automobileapi.model.User; | ||
import com.kasina.automobileapi.repository.OrderRepository; | ||
import com.kasina.automobileapi.repository.ProductRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.crossstore.ChangeSetPersister; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OrderService { | ||
private final OrderRepository orderRepository; | ||
private final ProductRepository productRepository; | ||
private final UserService userService; | ||
|
||
|
||
public Order createOrder(OrderRequest orderRequest) { | ||
User currentUser = userService.getCurrentUser(); | ||
BigDecimal grandTotal = BigDecimal.ZERO; | ||
|
||
// Create the order entity | ||
Order order = new Order(); | ||
order.setUser(currentUser); | ||
order.setStatus(orderRequest.getStatus()); | ||
order.setShippingAddress(orderRequest.getShippingAddress()); | ||
|
||
|
||
// Create order items and add them to the order | ||
for (OrderItemRequest itemRequest : orderRequest.getItems()) { | ||
OrderItem orderItem = new OrderItem(); | ||
Product product = productRepository.findById(itemRequest.getProductId()) | ||
.orElseThrow(() -> new IllegalArgumentException("Product not found")); | ||
|
||
orderItem.setProduct(product); | ||
orderItem.setQuantity(itemRequest.getQuantity()); | ||
orderItem.setSubTotal(product.getPrice().multiply(BigDecimal.valueOf(itemRequest.getQuantity()))); | ||
|
||
// Calculate and set other order item attributes if needed | ||
|
||
order.addOrderItem(orderItem); | ||
} | ||
|
||
// Save the order in the database | ||
return orderRepository.save(order); | ||
} | ||
|
||
public List<Order> getOrdersByUser(User user) { | ||
return orderRepository.findByUser(user); | ||
} | ||
public Order getOrderByIdAndUser(Long orderId, User user) { | ||
return orderRepository.findByIdAndUser(orderId, user) | ||
.orElseThrow(() -> new IllegalArgumentException("Order not found")); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.