-
Notifications
You must be signed in to change notification settings - Fork 15
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
19 changed files
with
201 additions
and
13 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
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
27 changes: 27 additions & 0 deletions
27
src/main/java/com/Optimart/controllers/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 |
---|---|---|
@@ -1,4 +1,31 @@ | ||
package com.Optimart.controllers; | ||
|
||
import com.Optimart.annotations.SecuredSwaggerOperation; | ||
import com.Optimart.constants.Endpoint; | ||
import com.Optimart.dto.Order.CreateOrderDTO; | ||
import com.Optimart.responses.User.UserResponse; | ||
import com.Optimart.services.Order.OrderService; | ||
import io.swagger.v3.oas.annotations.media.ArraySchema; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
@Tag(name = "Orders", description = "Everything about orders") | ||
@RequestMapping(Endpoint.Order.BASE) | ||
public class OrderController { | ||
|
||
public final OrderService orderService; | ||
|
||
@ApiResponse(responseCode = "201", description = "CREATED", content = @Content(schema = @Schema(implementation = Object.class), mediaType = "application/json")) | ||
@SecuredSwaggerOperation(summary = "Create a new order") | ||
@PostMapping | ||
public ResponseEntity<?> createOrder(@RequestBody CreateOrderDTO createOrderDTO){ | ||
return ResponseEntity.ok(orderService.createOrder(createOrderDTO)); | ||
} | ||
} |
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,32 @@ | ||
package com.Optimart.dto.Order; | ||
|
||
import com.Optimart.dto.OrderItem.OrderItemsDTO; | ||
import com.Optimart.dto.ShippingAddress.ShippingAddressDTO; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Setter | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreateOrderDTO { | ||
private String address; | ||
private String city; | ||
private String deliveryMethod; | ||
private String fullName; | ||
private Long itemsPrice; | ||
private List<OrderItemsDTO> orderItems; | ||
private String paymentMethod; | ||
private String phone; | ||
private Long shippingPrice; | ||
private Long totalPrice; | ||
@JsonProperty("user") | ||
private String userId; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/Optimart/dto/OrderItem/OrderItemsDTO.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,22 @@ | ||
package com.Optimart.dto.OrderItem; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class OrderItemsDTO { | ||
private String name; | ||
private int amount; | ||
private String image; | ||
private Long price; | ||
private int discount; | ||
@JsonProperty("product") | ||
private String productId; | ||
private String slug; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/Optimart/dto/ShippingAddress/ShippingAddressDTO.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.Optimart.dto.ShippingAddress; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ShippingAddressDTO { | ||
private String fullName; | ||
private String address; | ||
private String city; | ||
private String phoneNumber; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.Optimart.repositories; | ||
|
||
import com.Optimart.models.Order; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.UUID; | ||
|
||
public interface OrderRepository extends JpaRepository<Order, UUID> { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/Optimart/repositories/Specification/OrderSpecification.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,4 @@ | ||
package com.Optimart.repositories.Specification; | ||
|
||
public class OrderSpecification { | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.Optimart.services.Order; | ||
|
||
import com.Optimart.dto.Order.CreateOrderDTO; | ||
import com.Optimart.models.Order; | ||
import com.Optimart.responses.APIResponse; | ||
|
||
public interface IOrderService { | ||
APIResponse<Order> createOrder(CreateOrderDTO createOrderDTO); | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/Optimart/services/Order/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,47 @@ | ||
package com.Optimart.services.Order; | ||
|
||
import com.Optimart.constants.MessageKeys; | ||
import com.Optimart.dto.Order.CreateOrderDTO; | ||
import com.Optimart.dto.OrderItem.OrderItemsDTO; | ||
import com.Optimart.models.DeliveryType; | ||
import com.Optimart.models.Order; | ||
import com.Optimart.models.OrderItem; | ||
import com.Optimart.models.Paymenttype; | ||
import com.Optimart.repositories.DeliveryTypeRepository; | ||
import com.Optimart.repositories.OrderRepository; | ||
import com.Optimart.repositories.PaymentTypeRepository; | ||
import com.Optimart.responses.APIResponse; | ||
import com.Optimart.utils.LocalizationUtils; | ||
import lombok.RequiredArgsConstructor; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OrderService implements IOrderService{ | ||
private final ModelMapper modelMapper; | ||
private final OrderRepository orderRepository; | ||
private final PaymentTypeRepository paymentTypeRepository; | ||
private final DeliveryTypeRepository deliveryTypeRepository; | ||
private final LocalizationUtils localizationUtils; | ||
@Override | ||
public APIResponse<Order> createOrder(CreateOrderDTO createOrderDTO) { | ||
Order order = modelMapper.map(createOrderDTO, Order.class); | ||
List<OrderItem> orderItems = order.getOrderItemList().stream() | ||
.map(item -> { | ||
OrderItem orderItem = modelMapper.map(item, OrderItem.class); | ||
return orderItem; | ||
}).toList(); | ||
order.setOrderItemList(orderItems); | ||
Paymenttype paymenttype = paymentTypeRepository.findById(UUID.fromString(createOrderDTO.getPaymentMethod())).get(); | ||
DeliveryType deliveryType = deliveryTypeRepository.findById(UUID.fromString(createOrderDTO.getDeliveryMethod())).get(); | ||
order.setPaymentMethod(paymenttype); | ||
order.setDeliveryMethod(deliveryType); | ||
orderRepository.save(order); | ||
return new APIResponse<>(order, localizationUtils.getLocalizedMessage(MessageKeys.ORDER_CREATE_SUCCESS)); | ||
} | ||
} |