Skip to content

Commit

Permalink
Merge pull request #53 from Orange-Co/main
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
Kang1221 authored Aug 18, 2024
2 parents 961b651 + b6854a9 commit 616f2a5
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/main/java/co/orange/ddanzi/common/error/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,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 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;
}
}
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
@@ -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);
}
50 changes: 46 additions & 4 deletions src/main/java/co/orange/ddanzi/service/OrderService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package co.orange.ddanzi.service;

import co.orange.ddanzi.common.error.Error;
import co.orange.ddanzi.common.exception.DiscountNotFoundException;
import co.orange.ddanzi.common.exception.PaymentNotFoundException;
import co.orange.ddanzi.common.exception.*;
import co.orange.ddanzi.domain.order.Order;
import co.orange.ddanzi.domain.order.OrderAgreement;
import co.orange.ddanzi.domain.order.Payment;
import co.orange.ddanzi.domain.order.enums.OrderStatus;
import co.orange.ddanzi.domain.order.enums.PayStatus;
import co.orange.ddanzi.domain.order.pk.OrderAgreementId;
import co.orange.ddanzi.domain.product.Discount;
Expand All @@ -19,11 +19,10 @@
import co.orange.ddanzi.dto.AddressInfo;
import co.orange.ddanzi.dto.order.CheckProductResponseDto;
import co.orange.ddanzi.dto.order.CreateOrderRequestDto;
import co.orange.ddanzi.common.exception.ItemNotFoundException;
import co.orange.ddanzi.common.exception.ProductNotFoundException;
import co.orange.ddanzi.common.response.ApiResponse;
import co.orange.ddanzi.common.response.Success;
import co.orange.ddanzi.dto.order.OrderResponseDto;
import co.orange.ddanzi.dto.order.UpdateOrderResponseDto;
import co.orange.ddanzi.global.jwt.AuthUtils;
import co.orange.ddanzi.repository.*;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -119,6 +118,48 @@ public ApiResponse<?> createOrder(CreateOrderRequestDto requestDto){
return ApiResponse.onSuccess(Success.CREATE_ORDER_SUCCESS, setOrderResponseDto(user, order, item, payment));
}

@Transactional
public ApiResponse<?> getOrder(String orderId){
User user = authUtils.getUser();
Order order = orderRepository.findById(orderId).orElseThrow(() -> new OrderNotFoundException());
Item item = order.getItem();
Payment payment = paymentRepository.findByBuyerAndItem(user, item);
return ApiResponse.onSuccess(Success.GET_ORDER_DETAIL_SUCCESS, setOrderResponseDto(user, order, item, payment));
}

@Transactional
public ApiResponse<?> confirmedOrderToBuy(String orderId){
User user = authUtils.getUser();
Order order = orderRepository.findById(orderId).orElseThrow(() -> new OrderNotFoundException());

if(!order.getBuyer().equals(user) || order.getStatus()!=OrderStatus.SHIPPING)
return ApiResponse.onFailure(Error.UNAUTHORIZED_USER,null);

order.updateStatus(OrderStatus.COMPLETED);

return ApiResponse.onSuccess(Success.GET_ORDER_DETAIL_SUCCESS, UpdateOrderResponseDto.builder()
.orderId(order.getId())
.orderStatus(order.getStatus())
.build());
}

@Transactional
public ApiResponse<?> confirmedOrderToSale(String orderId){
User user = authUtils.getUser();
Order order = orderRepository.findById(orderId).orElseThrow(() -> new OrderNotFoundException());

if(!order.getItem().getSeller().equals(user) || order.getStatus()!=OrderStatus.ORDER_PLACE)
return ApiResponse.onFailure(Error.UNAUTHORIZED_USER,null);

order.updateStatus(OrderStatus.SHIPPING);

return ApiResponse.onSuccess(Success.GET_ORDER_DETAIL_SUCCESS, UpdateOrderResponseDto.builder()
.orderId(order.getId())
.orderStatus(order.getStatus())
.build());
}


private Order createOrderRecord(CreateOrderRequestDto requestDto, User user, Item item, OptionDetail optionDetail){
String orderId = createOrderId(requestDto.getItemId());
Order order = requestDto.toOrder(orderId, user, item, optionDetail);
Expand Down Expand Up @@ -154,4 +195,5 @@ private OrderResponseDto setOrderResponseDto(User user, Order order, Item item,

return responseDto;
}

}

0 comments on commit 616f2a5

Please sign in to comment.