Skip to content

Commit

Permalink
Merge pull request #222 from marinesnow34/pointHotFix
Browse files Browse the repository at this point in the history
Fix: 포인트 세부 수정
  • Loading branch information
marinesnow34 authored Mar 24, 2024
2 parents 842e03a + 5877dad commit 43b99cb
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/readyvery/readyverydemo/domain/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@Builder
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.readyvery.readyverydemo.domain.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.readyvery.readyverydemo.domain.Order;
import com.readyvery.readyverydemo.domain.Point;
import com.readyvery.readyverydemo.domain.UserInfo;

public interface PointRepository extends JpaRepository<Point, Long> {
List<Point> findAllByUserInfo(UserInfo userInfo);

Optional<Point> findByOrder(Order order);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum ExceptionCode {
POINT_NOT_ENOUGH(400, "Point is not enough."),
INVALID_INPUT(400, "Invalid input."),
UNAUTHORIZED(400, "Already User"),
AUTH_ERROR(403, "Auth Error");
AUTH_ERROR(403, "Auth Error"),
POINT_NOT_FOUND(403, "Point not found");

private int status;
private String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import com.readyvery.readyverydemo.src.order.dto.TossCancelReq;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentDto;
import com.readyvery.readyverydemo.src.order.dto.TosspaymentMakeRes;
import com.readyvery.readyverydemo.src.point.PointServiceFacade;
import com.readyvery.readyverydemo.src.user.UserServiceFacade;

import jakarta.transaction.Transactional;
Expand All @@ -91,6 +92,7 @@ public class OrderServiceImpl implements OrderService {
private final ReceiptRepository receiptRepository;
private final CouponRepository couponRepository;
private final PointRepository pointRepository;
private final PointServiceFacade pointServiceFacade;

@Override
public FoodyDetailRes getFoody(Long storeId, Long foodyId, Long inout) {
Expand Down Expand Up @@ -384,6 +386,8 @@ public CurrentRes getCurrent(String orderId) {
public Object cancelTossPayment(CustomUserDetails userDetails, TossCancelReq tossCancelReq) {
UserInfo user = userServiceFacade.getUserInfoWithPessimisticLock(userDetails.getId());
Order order = getOrder(tossCancelReq.getOrderId());
Point point = pointServiceFacade.getPointByOrder(order);

verifyCancel(order, user);
TosspaymentDto tosspaymentDto = null;
if (order.getAmount() > 0L) {
Expand All @@ -394,6 +398,7 @@ public Object cancelTossPayment(CustomUserDetails userDetails, TossCancelReq tos

applyCancelTosspaymentDto(order, tosspaymentDto);
userServiceFacade.saveUserPoint(user, user.getPoint() - order.getPoint());
pointServiceFacade.cancelPoint(point);

ordersRepository.save(order);
return orderMapper.tosspaymentDtoToCancelRes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.readyvery.readyverydemo.src.point;

import java.util.List;

import org.springframework.stereotype.Service;

import com.readyvery.readyverydemo.domain.Order;
import com.readyvery.readyverydemo.domain.Point;
import com.readyvery.readyverydemo.domain.UserInfo;
import com.readyvery.readyverydemo.domain.repository.PointRepository;
import com.readyvery.readyverydemo.global.exception.BusinessLogicException;
import com.readyvery.readyverydemo.global.exception.ExceptionCode;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class PointServiceFacade {
private final PointRepository pointRepository;

public List<Point> findAllByUserInfo(UserInfo userInfo) {
return pointRepository.findAllByUserInfo(userInfo);
}

public Point getPointByOrder(Order order) {
return pointRepository.findByOrder(order)
.orElseThrow(() -> new BusinessLogicException(ExceptionCode.POINT_NOT_FOUND));
}

public void cancelPoint(Point point) {
point.setIsDeleted(true);
pointRepository.save(point);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,30 @@
import org.springframework.stereotype.Service;

import com.readyvery.readyverydemo.domain.UserInfo;
import com.readyvery.readyverydemo.domain.repository.PointRepository;
import com.readyvery.readyverydemo.domain.repository.UserRepository;
import com.readyvery.readyverydemo.global.exception.BusinessLogicException;
import com.readyvery.readyverydemo.global.exception.ExceptionCode;
import com.readyvery.readyverydemo.security.jwt.dto.CustomUserDetails;
import com.readyvery.readyverydemo.src.point.dto.GetPointHistoryRes;
import com.readyvery.readyverydemo.src.point.dto.GetPointRes;
import com.readyvery.readyverydemo.src.point.dto.PointMapper;
import com.readyvery.readyverydemo.src.user.UserServiceFacade;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class PointServiceImpl implements PointService {
private final UserRepository userRepository;
private final PointRepository pointRepository;
private final UserServiceFacade userServiceFacade;
private final PointServiceFacade pointServiceFacade;
private final PointMapper pointMapper;

@Override
public GetPointRes getPoint(CustomUserDetails userDetails) {
UserInfo userInfo = getUserInfo(userDetails.getId());
UserInfo userInfo = userServiceFacade.getUserInfo(userDetails.getId());
return pointMapper.toGetPointRes(userInfo.getPoint());
}

@Override
public GetPointHistoryRes getPointHistory(CustomUserDetails userDetails) {
UserInfo userInfo = getUserInfo(userDetails.getId());
return pointMapper.toGetPointHistoryRes(pointRepository.findAllByUserInfo(userInfo));
}

private UserInfo getUserInfo(Long id) {
return userRepository.findById(id).orElseThrow(
() -> new BusinessLogicException(ExceptionCode.USER_NOT_FOUND)
);
UserInfo userInfo = userServiceFacade.getUserInfo(userDetails.getId());
return pointMapper.toGetPointHistoryRes(pointServiceFacade.findAllByUserInfo(userInfo));
}
}

0 comments on commit 43b99cb

Please sign in to comment.