Skip to content

Commit

Permalink
Merge pull request #52 from Orange-Co/feature/order
Browse files Browse the repository at this point in the history
Feat: implement Order APIs
  • Loading branch information
Kang1221 authored Aug 18, 2024
2 parents 3da831b + f8ee763 commit b6854a9
Show file tree
Hide file tree
Showing 18 changed files with 366 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/main/java/co/orange/ddanzi/common/error/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public enum Error {
DUE_DATE_IS_INCORRECT(HttpStatus.BAD_REQUEST, "The due date is incorrect."),
ITEM_IS_NOT_ON_SALE(HttpStatus.BAD_REQUEST, "The item is not on sale."),

//402 Payment Required
PAYMENT_REQUIRED(HttpStatus.PAYMENT_REQUIRED, "Payment is required to place the order."),

// 403 UNAUTHORIZED
UNAUTHORIZED_USER(HttpStatus.UNAUTHORIZED,"Unauthorized user"),
INVALID_JWT_EXCEPTION(HttpStatus.UNAUTHORIZED, "Invalid JWT"),
Expand Down Expand Up @@ -47,6 +50,7 @@ public enum Error {
ACCOUNT_ALREADY_EXISTS(HttpStatus.CONFLICT, "The account already exists."),
AUTHENTICATION_ALREADY_EXISTS(HttpStatus.CONFLICT, "The identity authentication of user already exists."),
PAYMENT_CANNOT_CHANGE(HttpStatus.CONFLICT, "The payment status cannot be changed."),
ORDER_STATUS_CANNOT_CHANGE(HttpStatus.CONFLICT, "The order status cannot be changed."),

// 500 INTERNAL SERVER ERROR
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Internal Server Error"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.orange.ddanzi.common.exception;

import co.orange.ddanzi.common.error.Error;

public class DiscountNotFoundException extends ApiException{
public DiscountNotFoundException() {
super(Error.DISCOUNT_INFO_NOT_FOUND, Error.DISCOUNT_INFO_NOT_FOUND.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.orange.ddanzi.common.exception;

import co.orange.ddanzi.common.error.Error;

public class OrderNotFoundException extends ApiException {
public OrderNotFoundException() {
super(Error.ORDER_NOT_FOUND, Error.ORDER_NOT_FOUND.getMessage());
}
}
16 changes: 16 additions & 0 deletions src/main/java/co/orange/ddanzi/controller/OrderController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.orange.ddanzi.controller;

import co.orange.ddanzi.domain.order.enums.OrderStatus;
import co.orange.ddanzi.dto.order.CreateOrderRequestDto;
import co.orange.ddanzi.common.response.ApiResponse;
import co.orange.ddanzi.service.OrderService;
Expand All @@ -21,4 +22,19 @@ ApiResponse<?> checkOrderProduct(@PathVariable("id") String id) {
ApiResponse<?> createOrder(@RequestBody CreateOrderRequestDto requestDto){
return orderService.createOrder(requestDto);
}

@GetMapping("/{id}")
ApiResponse<?> getOrder(@PathVariable("id") String id){
return orderService.getOrder(id);
}

@PatchMapping("/{id}/buy")
ApiResponse<?> confirmedOrderToBuy(@PathVariable("id") String id){
return orderService.confirmedOrderToBuy(id);
}

@PatchMapping("/{id}/sale")
ApiResponse<?> confirmedOrderToSale(@PathVariable("id") String id){
return orderService.confirmedOrderToSale(id);
}
}
4 changes: 4 additions & 0 deletions src/main/java/co/orange/ddanzi/domain/order/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ public Order(String id, OrderStatus status, LocalDateTime createdAt, LocalDateTi
this.selectedOptionDetail = selectedOptionDetail;

}

public void updateStatus(OrderStatus newStatus) {
this.status = newStatus;
}
}
9 changes: 8 additions & 1 deletion src/main/java/co/orange/ddanzi/domain/order/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import co.orange.ddanzi.domain.order.enums.PayMethod;
import co.orange.ddanzi.domain.order.enums.PayStatus;
import co.orange.ddanzi.domain.product.Item;
import co.orange.ddanzi.domain.user.User;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -44,15 +45,21 @@ public class Payment {
@JoinColumn(name = "item_id")
private Item item; //판매 제품


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "buyer_id")
private User buyer; //구매자

@Builder
public Payment(Integer charge, Integer totalPrice, PayMethod method, PayStatus payStatus, LocalDateTime startedAt, LocalDateTime endedAt, Item item) {
public Payment(Integer charge, Integer totalPrice, PayMethod method, PayStatus payStatus, LocalDateTime startedAt, LocalDateTime endedAt, Item item, User buyer) {
this.charge = charge;
this.totalPrice = totalPrice;
this.method = method;
this.payStatus = payStatus;
this.startedAt = startedAt;
this.endedAt = endedAt;
this.item = item;
this.buyer = buyer;
}

public void updatePaymentStatus(PayStatus newStatus) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package co.orange.ddanzi.dto.order;

import co.orange.ddanzi.domain.order.Order;
import co.orange.ddanzi.domain.order.enums.OrderStatus;
import co.orange.ddanzi.domain.product.Item;
import co.orange.ddanzi.domain.product.OptionDetail;
import co.orange.ddanzi.domain.user.User;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class CreateOrderRequestDto {
private String itemId;
private String paymentId;
private Long paymentId;
private Long selectedOptionDetailId;
private Boolean orderTerm1;
private Boolean orderTerm2;
private Boolean orderTerm3;

public Order toOrder(String orderId, User buyer, Item item, OptionDetail optionDetail) {
return Order.builder()
.id(orderId)
.buyer(buyer)
.item(item)
.selectedOptionDetail(optionDetail)
.createdAt(LocalDateTime.now())
.status(OrderStatus.ORDER_PLACE)
.build();
}
}
26 changes: 26 additions & 0 deletions src/main/java/co/orange/ddanzi/dto/order/OrderResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package co.orange.ddanzi.dto.order;


import co.orange.ddanzi.domain.order.enums.OrderStatus;
import co.orange.ddanzi.domain.order.enums.PayMethod;
import co.orange.ddanzi.dto.AddressInfo;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@Builder
public class OrderResponseDto {
private String orderId;
private OrderStatus orderStatus;
private String productName;
private String imgUrl;
private Integer originPrice;
private AddressInfo addressInfo;
private PayMethod paymentMethod;
private LocalDateTime paidAt;
private Integer discountPrice;
private Integer charge;
private Integer totalPrice;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package co.orange.ddanzi.dto.order;

import co.orange.ddanzi.domain.order.enums.OrderStatus;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class UpdateOrderResponseDto {
private String orderId;
private OrderStatus orderStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import co.orange.ddanzi.domain.order.enums.PayMethod;
import co.orange.ddanzi.domain.order.enums.PayStatus;
import co.orange.ddanzi.domain.product.Item;
import co.orange.ddanzi.domain.user.User;
import lombok.Getter;

import java.time.LocalDateTime;
Expand All @@ -15,7 +16,7 @@ public class CreatePaymentRequestDto {
private Integer totalPrice;
private PayMethod method;

public Payment toEntity(Item item){
public Payment toEntity(Item item, User buyer){
return Payment.builder()
.charge(charge)
.totalPrice(totalPrice)
Expand All @@ -24,6 +25,7 @@ public Payment toEntity(Item item){
.startedAt(LocalDateTime.now())
.endedAt(null)
.item(item)
.buyer(buyer)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.orange.ddanzi.repository;

import co.orange.ddanzi.domain.order.Order;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderRepository extends JpaRepository<Order, String> {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package co.orange.ddanzi.repository;

import co.orange.ddanzi.domain.order.Payment;
import co.orange.ddanzi.domain.product.Item;
import co.orange.ddanzi.domain.user.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PaymentRepository extends JpaRepository<Payment, Long> {
@Query("SELECT p from Payment p WHERE p.buyer = :buyer and p.item = :item and p.payStatus = 'PAID'")
Payment findByBuyerAndItem(@Param("buyer") User buyer, @Param("item")Item item);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.orange.ddanzi.repository.term;

import co.orange.ddanzi.domain.order.OrderAgreement;
import co.orange.ddanzi.domain.order.pk.OrderAgreementId;
import org.springframework.data.jpa.repository.JpaRepository;

public interface OrderAgreementRepository extends JpaRepository<OrderAgreement, OrderAgreementId> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.orange.ddanzi.repository.term;

import co.orange.ddanzi.domain.term.TermOrder;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TermOrderRepository extends JpaRepository<TermOrder, Long> {
}
40 changes: 40 additions & 0 deletions src/main/java/co/orange/ddanzi/service/AddressService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package co.orange.ddanzi.service;

import co.orange.ddanzi.domain.user.Address;
import co.orange.ddanzi.domain.user.User;
import co.orange.ddanzi.dto.AddressInfo;
import co.orange.ddanzi.dto.setting.AddressResponseDto;
import co.orange.ddanzi.repository.AddressRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@RequiredArgsConstructor
@Service
public class AddressService {
private final AddressRepository addressRepository;

public AddressInfo setAddressInfo(User user){
Address address = addressRepository.findByUser(user);
return AddressInfo.builder()
.recipient(address != null ? address.getRecipient() : null)
.zipCode(address != null ? address.getZipCode() : null)
.address(address != null ? address.getAddress() + " " + address.getDetailAddress() : null)
.recipientPhone(address != null ? address.getRecipientPhone() : null)
.build();
}

public AddressResponseDto setAddressDto(User user){
Address address = addressRepository.findByUser(user);
return AddressResponseDto.builder()
.addressId(address != null ? address.getId() : null)
.recipient(address != null ? address.getRecipient() : null)
.zipCode(address != null ? address.getZipCode() : null)
.type(address != null ? address.getType() : null)
.address(address != null ? address.getAddress() : null)
.detailAddress(address != null ? address.getDetailAddress() : null)
.recipientPhone(address != null ? address.getRecipientPhone() : null)
.build();
}
}
Loading

0 comments on commit b6854a9

Please sign in to comment.