Skip to content

Commit

Permalink
Merge pull request #210 from Team-BC-1/feat/userRefund
Browse files Browse the repository at this point in the history
Feat/user refund
  • Loading branch information
Binsreoun authored Jan 30, 2024
2 parents 412aaa2 + 85ba5af commit 75e3197
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@

import bc1.gream.domain.admin.dto.request.AdminGetRefundRequestDto;
import bc1.gream.domain.admin.dto.request.AdminProductRequestDto;
import bc1.gream.domain.admin.dto.request.AdminRefundPassResponseDto;
import bc1.gream.domain.admin.dto.response.AdminGetRefundResponseDto;
import bc1.gream.domain.admin.dto.response.AdminProductResponseDto;
import bc1.gream.domain.admin.mapper.RefundMapper;
import bc1.gream.domain.product.service.query.ProductService;
import bc1.gream.domain.user.service.command.RefundCommandService;
import bc1.gream.domain.user.service.query.RefundQueryService;
import bc1.gream.global.common.RestResponse;
import io.swagger.v3.oas.annotations.Operation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -23,9 +27,9 @@
@RequestMapping("/api/admin")
public class AdminController {


private final ProductService productService;
private final RefundQueryService refundQueryService;
private final RefundCommandService refundCommandService;

@GetMapping("/refunds")
@Operation(summary = "신청된 환급 리스트 조회 요청", description = "사용자가 신청한 환급 요청 리스트를 반환합니다.")
Expand All @@ -37,15 +41,25 @@ public RestResponse<List<AdminGetRefundResponseDto>> getRefunds(AdminGetRefundRe
.toList();

return RestResponse.success(response);

}

@PostMapping("/products")
public RestResponse<AdminProductResponseDto> addProducts(
@RequestBody AdminProductRequestDto adminProductRequestDto
) {
productService.addProduct(adminProductRequestDto);

return RestResponse.success(new AdminProductResponseDto());
}


@DeleteMapping("/refund/{id}")
@Operation(summary = "유저 환급 승인", description = "유저가 신청한 환급 요청을 승인해주는 기능입니다.")
public RestResponse<AdminRefundPassResponseDto> approveRefund(
@PathVariable Long id
) {
AdminRefundPassResponseDto responseDto = refundCommandService.approveRefund(id);

return RestResponse.success(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bc1.gream.domain.admin.dto.request;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties
public record AdminRefundPassResponseDto() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bc1.gream.domain.admin.dto.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties
public record AdminRefundPassRequestDto() {

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public List<Gifticon> findAllSoldBySeller(User user) {
.selectFrom(QGifticon.gifticon)
.leftJoin(QGifticon.gifticon.order, QOrder.order)
.where(QGifticon.gifticon.order.seller.eq(user))
.orderBy(QGifticon.gifticon.modifiedAt.desc())
.fetch();
}

Expand All @@ -28,6 +29,7 @@ public List<Gifticon> findAllBoughtByBuyer(User user) {
.selectFrom(QGifticon.gifticon)
.leftJoin(QGifticon.gifticon.order, QOrder.order)
.where(QGifticon.gifticon.order.buyer.eq(user))
.orderBy(QGifticon.gifticon.modifiedAt.desc())
.fetch();
}
}
14 changes: 14 additions & 0 deletions src/main/java/bc1/gream/domain/user/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package bc1.gream.domain.user.controller;

import bc1.gream.domain.user.dto.request.UserPointRefundRequestDto;
import bc1.gream.domain.user.dto.request.UserSignupRequestDto;
import bc1.gream.domain.user.dto.response.UserPointRefundResponseDto;
import bc1.gream.domain.user.dto.response.UserPointResponseDto;
import bc1.gream.domain.user.dto.response.UserSignupResponseDto;
import bc1.gream.domain.user.service.UserService;
import bc1.gream.domain.user.service.command.RefundCommandService;
import bc1.gream.global.common.RestResponse;
import bc1.gream.global.security.UserDetailsImpl;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -23,6 +26,7 @@
public class UserController {

private final UserService userService;
private final RefundCommandService refundCommandService;

@PostMapping("/signup")
@SecurityRequirements() // Swagger Security 적용 X
Expand All @@ -38,4 +42,14 @@ public RestResponse<UserPointResponseDto> pointsCheck(@AuthenticationPrincipal U
UserPointResponseDto response = userService.pointsCheck(userDetails);
return RestResponse.success(response);
}

@PostMapping("/points/refunds")
@Operation(summary = "사용자 포인트 환급 요청", description = "사용자의 포인트에 대한 환급요청을 처리합니다.")
public RestResponse<UserPointRefundResponseDto> refundsPoint(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody UserPointRefundRequestDto requestDto
) {
UserPointRefundResponseDto responseDto = refundCommandService.refundsPoint(userDetails.getUser(), requestDto);
return RestResponse.success(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package bc1.gream.domain.user.dto.request;

import lombok.Builder;

@Builder // 테스트용
public record UserPointRefundRequestDto(
Long point,
String bank,
String accountNumber
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package bc1.gream.domain.user.dto.response;

import lombok.Builder;

@Builder
public record UserPointRefundResponseDto(
Long refundPoint,
Long userPoint
) {

}
19 changes: 19 additions & 0 deletions src/main/java/bc1/gream/domain/user/mapper/RefundMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package bc1.gream.domain.user.mapper;

import bc1.gream.domain.user.dto.response.UserPointRefundResponseDto;
import bc1.gream.domain.user.entity.Refund;
import bc1.gream.domain.user.entity.User;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

@Mapper
public interface RefundMapper {

RefundMapper INSTANCE = Mappers.getMapper(RefundMapper.class);

@Mapping(expression = "java(refund.getPoint())", target = "refundPoint")
@Mapping(expression = "java(user.getPoint())", target = "userPoint")
UserPointRefundResponseDto toUserPointRefundResponseDto(Refund refund, User user);

}
11 changes: 11 additions & 0 deletions src/main/java/bc1/gream/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import bc1.gream.domain.user.entity.User;
import bc1.gream.domain.user.entity.UserRole;
import bc1.gream.domain.user.mapper.UserMapper;
import bc1.gream.domain.user.repository.RefundRepository;
import bc1.gream.domain.user.repository.UserRepository;
import bc1.gream.global.common.ResultCase;
import bc1.gream.global.exception.GlobalException;
Expand All @@ -21,6 +22,7 @@
public class UserService {

private final UserRepository userRepository;
private final RefundRepository refundRepository;
private final PasswordEncoder passwordEncoder;

@Transactional
Expand Down Expand Up @@ -54,4 +56,13 @@ private void validateExistingUser(UserSignupRequestDto request) {
throw new GlobalException(ResultCase.DUPLICATED_NICKNAME);
}
}

public User findUser(String loginId) {
User user = userRepository.findByLoginId(loginId).orElseThrow(
() -> new GlobalException(ResultCase.USER_NOT_FOUND)
);

return user;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package bc1.gream.domain.user.service.command;

import bc1.gream.domain.admin.dto.request.AdminRefundPassResponseDto;
import bc1.gream.domain.user.dto.request.UserPointRefundRequestDto;
import bc1.gream.domain.user.dto.response.UserPointRefundResponseDto;
import bc1.gream.domain.user.entity.Refund;
import bc1.gream.domain.user.entity.User;
import bc1.gream.domain.user.mapper.RefundMapper;
import bc1.gream.domain.user.repository.RefundRepository;
import bc1.gream.domain.user.service.UserService;
import bc1.gream.domain.user.service.query.RefundQueryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class RefundCommandService {

private final RefundRepository refundRepository;
private final RefundQueryService refundQueryService;
private final UserService userService;

@Transactional
public UserPointRefundResponseDto refundsPoint(User user, UserPointRefundRequestDto requestDto) {
User findUser = userService.findUser(user.getLoginId());

Refund refund = Refund.builder()
.point(requestDto.point())
.bank(requestDto.bank())
.accountNumber(requestDto.accountNumber())
.user(findUser)
.build();

findUser.decreasePoint(requestDto.point());

Refund savedRefund = refundRepository.save(refund);

return RefundMapper.INSTANCE.toUserPointRefundResponseDto(savedRefund, findUser);
}

@Transactional
public AdminRefundPassResponseDto approveRefund(Long id) {
Refund refund = refundQueryService.findRefund(id);

refundRepository.delete(refund);

return new AdminRefundPassResponseDto();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import bc1.gream.domain.user.entity.Refund;
import bc1.gream.domain.user.repository.RefundRepository;
import bc1.gream.global.common.ResultCase;
import bc1.gream.global.exception.GlobalException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -20,4 +22,14 @@ public class RefundQueryService {
public List<Refund> getRefunds() {
return refundRepository.findAll();
}

/**
* 특정 id에 해당하는 환급 정보를 반환
*/
public Refund findRefund(Long refundId) {
return refundRepository.findById(refundId).orElseThrow(
() -> new GlobalException(ResultCase.REFUND_NOT_FOUND)
);
}

}
1 change: 1 addition & 0 deletions src/main/java/bc1/gream/global/common/ResultCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum ResultCase {
// 만료된 리프레쉬 토큰 401
EXPIRED_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, 1006, "만료된 Refresh Token"),
NOT_ENOUGH_POINT(HttpStatus.FORBIDDEN, 1007, "유저의 포인트가 부족합니다"),
REFUND_NOT_FOUND(HttpStatus.NOT_FOUND, 1008, "환급 정보가 없습니다."),

// 상품 2000번대
// 검색 결과 없음 404
Expand Down

0 comments on commit 75e3197

Please sign in to comment.