Skip to content

Commit

Permalink
feat: update order service
Browse files Browse the repository at this point in the history
  • Loading branch information
ngntu10 committed Oct 12, 2024
1 parent f720fe4 commit 3d26dde
Show file tree
Hide file tree
Showing 19 changed files with 201 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/Optimart/constants/Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static class Product{
public static final String UNLIKE = "/unlike";
public static final String LIKED_ME = "/liked/me";
public static final String VIEWED_ME = "viewed/me";
}

public static class Order{
public static final String BASE = API_PREFIX + "/orders";
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/Optimart/constants/MessageKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ public class MessageKeys {
public static final String PRODUCT_UNLIKED = "product.unlike_success";
public static final String PRODUCT_UNLIKED_FAILED = "product.unlike_failed";

// ORDER
public static final String ORDER_CREATE_SUCCESS = "order.create_success";
public static final String ORDER_GET_SUCCESS = "order.get_success";

}
1 change: 1 addition & 0 deletions src/main/java/com/Optimart/controllers/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public ResponseEntity<UserLoginResponse> getInfoCurrentUser(@Parameter @RequestH
String email = jwtTokenUtil.extractEmail(token.substring(7));
User user = authService.findUserByEmail(email); // Need to refactor code ****
UserLoginResponse userLoginResponse = mapper.map(user, UserLoginResponse.class);
userLoginResponse.setAddresses(user.getUserShippingAddressList());
return ResponseEntity.ok().body(userLoginResponse);
} catch (Exception e) { return ResponseEntity.badRequest().body(null);}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public ResponseEntity<DeliveryType> getDetailsDeliveryType(@PathVariable String
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = DeliveryType.class), mediaType = "application/json"))
@SecuredSwaggerOperation(summary = "Update delivery type by id")
@PutMapping(Endpoint.DeliveryType.ID)
public ResponseEntity<APIResponse<DeliveryType>> editDeliveryType(@RequestBody DeliveryTypeDTO deliveryTypeDTO){
return ResponseEntity.ok(deliveryTypeService.editDeliveryType(deliveryTypeDTO));
public ResponseEntity<APIResponse<DeliveryType>> editDeliveryType(@RequestBody DeliveryTypeDTO deliveryTypeDTO, @PathVariable String deliveryTypeId){
return ResponseEntity.ok(deliveryTypeService.editDeliveryType(deliveryTypeDTO, deliveryTypeId));
}

@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = String.class), mediaType = "application/json"))
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/Optimart/controllers/OrderController.java
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));
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/Optimart/dto/Order/CreateOrderDTO.java
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 src/main/java/com/Optimart/dto/OrderItem/OrderItemsDTO.java
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;
}
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;
}
6 changes: 3 additions & 3 deletions src/main/java/com/Optimart/models/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Order extends BaseEntity {
private Long totalPrice;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
@JoinColumn(name = "user_id")
private User user;

@Column(name = "isPaid", nullable = false)
Expand All @@ -51,10 +51,10 @@ public class Order extends BaseEntity {
@Column(name = "isDelivered", nullable = false)
private int isDelivered = 0;

@Column(name = "paidAt", nullable = false)
@Column(name = "paidAt")
private Date paidAt;

@Column(name = "deliveryAt", nullable = false)
@Column(name = "deliveryAt")
private Date deliveryAt;

@Enumerated(EnumType.STRING)
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/Optimart/models/OrderItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Optimart.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -17,23 +18,31 @@ public class OrderItem extends BaseEntity {
@Column(name = "id")
private UUID id;

@Column(name = "name", nullable = false)
private String name;

@Column(name = "amount", nullable = false)
private int amount;

@Column(name = "image", nullable = false)
private String image;

@Column(name = "price", nullable = false)
private Long price;

@Column(name = "discount", nullable = false)
private int discount;

@Column(name = "price", nullable = false)
private Long price;
@Column(name = "slug", nullable = false)
private String slug;

@ManyToOne
@JsonIgnore
@JoinColumn(name = "order_id", nullable = false)
private Order order;

@ManyToOne
@JsonIgnore
@JoinColumn(name = "product_id", nullable = false)
private Product product;
}
5 changes: 2 additions & 3 deletions src/main/java/com/Optimart/models/Paymenttype.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ public class Paymenttype extends BaseEntity {
@Column(name = "name", nullable = false, unique = true)
private String name;

@OneToMany(mappedBy = "paymentMethod")
List<Order> orders;
// @OneToMany(mappedBy = "paymentMethod")
// List<Order> orders;

// @Enumerated(EnumType.STRING)
@Column(name = "type", nullable = false)
private String type;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/Optimart/models/userShippingAddress.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Optimart.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;

Expand Down Expand Up @@ -31,6 +32,7 @@ public class userShippingAddress extends BaseEntity {
private int isDefault =0;

@ManyToOne
@JsonIgnore
@JoinColumn(name = "user_id")
private User user;
}
9 changes: 9 additions & 0 deletions src/main/java/com/Optimart/repositories/OrderRepository.java
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> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.Optimart.repositories.Specification;

public class OrderSpecification {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.Optimart.responses.Auth;

import com.Optimart.models.Role;
import com.Optimart.models.ShippingAddress;
import com.Optimart.models.userShippingAddress;
import lombok.*;

import java.util.List;
Expand All @@ -23,4 +25,5 @@ public class UserLoginResponse {
private String lastName;
private String imageUrl;
private String phoneNumber;
private List<userShippingAddress> addresses;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public DeliveryType getDeliveryType(String id) {
}

@Override
public APIResponse<DeliveryType> editDeliveryType(DeliveryTypeDTO deliveryTypeDTO) {
DeliveryType deliveryType = deliveryTypeRepository.findByName(deliveryTypeDTO.getName()).get();
public APIResponse<DeliveryType> editDeliveryType(DeliveryTypeDTO deliveryTypeDTO, String deliveryTypeId) {
DeliveryType deliveryType = deliveryTypeRepository.findById(UUID.fromString(deliveryTypeId)).get();
mapper.map(deliveryTypeDTO, deliveryType);
deliveryTypeRepository.save(deliveryType);
return new APIResponse<>(deliveryType, localizationUtils.getLocalizedMessage(MessageKeys.DELIVERY_TYPE_UPDATE_SUCCESS));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IDeliveryTypeService {
PagingResponse<List<DeliveryType>> findAll(DeliveryTypeSearchDTO deliveryTypeSearchDTO);
APIResponse<DeliveryType> createType(DeliveryTypeDTO createDeliveryTypeDTO);
DeliveryType getDeliveryType(String id);
APIResponse<DeliveryType> editDeliveryType(DeliveryTypeDTO deliveryTypeDTO);
APIResponse<DeliveryType> editDeliveryType(DeliveryTypeDTO deliveryTypeDTO, String deliveryTypeId);
APIResponse<Boolean> deleteDeliveryType(String id);
APIResponse<Boolean> deleteMutilDeliveryType(DeliveryTypeMutilDeleteDTO deliveryTypeMutilDeleteDTO);

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/Optimart/services/Order/IOrderService.java
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 src/main/java/com/Optimart/services/Order/OrderService.java
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));
}
}

0 comments on commit 3d26dde

Please sign in to comment.