Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/#43/내가 요청한 정산 리스트 api 개발 #46

Merged
30 changes: 26 additions & 4 deletions src/main/java/space/space_spring/controller/PayController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.argument_resolver.jwtLogin.JwtLoginAuth;
import space.space_spring.dto.pay.GetPayViewResponse;
import space.space_spring.dto.pay.GetRequestPayViewResponse;
import space.space_spring.dto.pay.PayReceiveInfoDto;
import space.space_spring.dto.pay.PayRequestInfoDto;
import space.space_spring.entity.UserSpace;
Expand All @@ -26,16 +27,21 @@ public class PayController {
private final PayService payService;
private final UserSpaceUtils userSpaceUtils;

/**
* 정산 홈 view
*/
@GetMapping("/space/{spaceId}/pay")
public BaseResponse<GetPayViewResponse> showPayListForUser(@JwtLoginAuth Long userId, @PathVariable Long spaceId) {
// TODO 1. 유저가 스페이스에 속하는 지 검증
validateIsUserInSpace(userId, spaceId);

// TODO 2. 유저가 요청한 정산 리스트 get
List<PayRequestInfoDto> payRequestInfoDtoList = payService.getPayRequestInfoForUser(userId, spaceId);
// TODO 2. 유저가 요청한 정산 중 현재 진행중인 정산 리스트 get
// 현재 진행중인 정산 -> isComplete = false
List<PayRequestInfoDto> payRequestInfoDtoList = payService.getPayRequestInfoForUser(userId, spaceId, false);

// TODO 3. 유저가 요청받은 정산 리스트 get
List<PayReceiveInfoDto> payReceiveInfoDtoList = payService.getPayReceiveInfoForUser(userId, spaceId);
// TODO 3. 유저가 요청받은 정산 중 현재 진행중인 정산 리스트 get
// 현재 진행중인 정산 -> isComplete = false
List<PayReceiveInfoDto> payReceiveInfoDtoList = payService.getPayReceiveInfoForUser(userId, spaceId, false);

return new BaseResponse<>(new GetPayViewResponse(payRequestInfoDtoList, payReceiveInfoDtoList));
}
Expand All @@ -46,4 +52,20 @@ private void validateIsUserInSpace(Long userId, Long spaceId) {
userSpaceUtils.isUserInSpace(userId, spaceId);
}

/**
* 내가 요청한 정산 view
*/
@GetMapping("/space/{spaceId}/pay/request")
public BaseResponse<GetRequestPayViewResponse> showRequestPayListForUser(@JwtLoginAuth Long userId, @PathVariable Long spaceId) {
// TODO 1. 유저가 스페이스에 속하는 지 검증 -> 추후에 인터셉터에서 처리하게끔 리펙토링 필요
validateIsUserInSpace(userId, spaceId);

// TODO 2. 유저가 요청한 정산 중 현재 진행중인 정산 리스트 get -> 아직 완료되지 않은 정산 : isComplete = false
List<PayRequestInfoDto> payRequestInfoDtoListInComplete = payService.getPayRequestInfoForUser(userId, spaceId, false);

// TODO 3. 유저가 요청한 정산 중 완료한 정산 리스트 get -> 완료된 정산 : isComplete = true
List<PayRequestInfoDto> payRequestInfoDtoListComplete = payService.getPayRequestInfoForUser(userId, spaceId, true);

return new BaseResponse<>(new GetRequestPayViewResponse(payRequestInfoDtoListInComplete, payRequestInfoDtoListComplete));
}
}
12 changes: 6 additions & 6 deletions src/main/java/space/space_spring/dao/PayDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public class PayDao {
@PersistenceContext
private EntityManager em;

public List<PayRequest> findPayRequestListByUser(User user, Space space) {
public List<PayRequest> findPayRequestListByUser(User user, Space space, boolean isComplete) {
// 유저가 해당 스페이스에서 요청한 정산 목록만을 select
// 아직 정산이 완료되지 않은 payRequest 엔티티만 select
String jpql = "SELECT pr FROM PayRequest pr WHERE pr.payCreateUser = :user AND pr.space = :space AND pr.isComplete = false";
String jpql = "SELECT pr FROM PayRequest pr WHERE pr.payCreateUser = :user AND pr.space = :space AND pr.isComplete = :isComplete";

return em.createQuery(jpql, PayRequest.class)
.setParameter("user", user)
.setParameter("space", space)
.setParameter("isComplete", isComplete)
.getResultList();
}

Expand All @@ -39,16 +39,16 @@ public List<PayRequestTarget> findPayRequestTargetListByPayRequest(PayRequest pa
.getResultList();
}

public List<PayRequestTarget> findPayRequestTargetListByUser(User user, Space space) {
public List<PayRequestTarget> findPayRequestTargetListByUser(User user, Space space, boolean isComplete) {
// 유저가 해당 스페이스에서 요청받은 정산 목록만을 select
// 유저가 요청받은 정산 중 아직 완료되지 않은 payRequestTarget 엔티티만 select
String jpql = "SELECT prt FROM PayRequestTarget prt " +
"JOIN prt.payRequest pr " +
"WHERE prt.targetUserId = :userId AND pr.space = :space AND pr.isComplete = false";
"WHERE prt.targetUserId = :userId AND pr.space = :space AND pr.isComplete = :isComplete";

return em.createQuery(jpql, PayRequestTarget.class)
.setParameter("userId", user.getUserId())
.setParameter("space", space)
.setParameter("isComplete", isComplete)
.getResultList();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package space.space_spring.dto.pay;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
@AllArgsConstructor
public class GetRequestPayViewResponse {

private List<PayRequestInfoDto> payRequestInfoDtoListInComplete = new ArrayList<>();

private List<PayRequestInfoDto> payRequestInfoDtoListComplete = new ArrayList<>();
}
12 changes: 6 additions & 6 deletions src/main/java/space/space_spring/service/PayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public class PayService {
private final UserUtils userUtils;
private final SpaceUtils spaceUtils;

public List<PayRequestInfoDto> getPayRequestInfoForUser(Long userId, Long spaceId) {
public List<PayRequestInfoDto> getPayRequestInfoForUser(Long userId, Long spaceId, boolean isComplete) {
// TODO 1. userId에 해당하는 user find
User userByUserId = userUtils.findUserByUserId(userId);

// TODO 2. spaceId에 해당하는 space find
Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId);

// TODO 3. 유저가 요청한 정산 중 진행 중인 정산 리스트 select
List<PayRequest> payRequestListByUser = payDao.findPayRequestListByUser(userByUserId, spaceBySpaceId);
// TODO 3. 유저가 요청한 정산 리스트 select
List<PayRequest> payRequestListByUser = payDao.findPayRequestListByUser(userByUserId, spaceBySpaceId, isComplete);

// TODO 4. return 타입 구성
// 3-1. 각 payRequest 에 해당하는 모든 payRequestTarget 을 loop로 돌면서 데이터 수집
Expand Down Expand Up @@ -62,15 +62,15 @@ public List<PayRequestInfoDto> getPayRequestInfoForUser(Long userId, Long spaceI
return payRequestInfoDtoList;
}

public List<PayReceiveInfoDto> getPayReceiveInfoForUser(Long userId, Long spaceId) {
public List<PayReceiveInfoDto> getPayReceiveInfoForUser(Long userId, Long spaceId, boolean isComplete) {
// TODO 1. userId에 해당하는 유저 find
User userByUserId = userUtils.findUserByUserId(userId);

// TODO 2. spaceId에 해당하는 space find
Space spaceBySpaceId = spaceUtils.findSpaceBySpaceId(spaceId);

// TODO 3. 유저가 요청받은 정산 중 진행 중인 정산 리스트 select
List<PayRequestTarget> payRequestTargetListByUser = payDao.findPayRequestTargetListByUser(userByUserId, spaceBySpaceId);
// TODO 3. 유저가 요청받은 정산 리스트 select
List<PayRequestTarget> payRequestTargetListByUser = payDao.findPayRequestTargetListByUser(userByUserId, spaceBySpaceId, isComplete);

// TODO 4. return 타입 구성
// 3-1. 각 payRequestTarget 에 해당하는 정산 요청자, 정산 요청 금액 을 loop를 돌면서 데이터 수집
Expand Down
88 changes: 68 additions & 20 deletions src/test/java/space/space_spring/service/PayServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,74 +41,122 @@ class PayServiceTest {

private User user1;
private User user2;
private User user3;
private User user4;
private Space testSpace;
private PayRequest testPayRequest;
private PayRequestTarget testPayRequestTarget;
private PayRequestTarget testPayRequestTarget_1;
private PayRequestTarget testPayRequestTarget_2;
private PayRequestTarget testPayRequestTarget_3;

@BeforeEach
public void 테스트_셋업() {
/**
* user2이 같은 스페이스에 속한 user1에게 정산을 요청한 상황 가정
* user1이 같은 스페이스에 속한 user2, 3, 4 에게 정산을 요청한 상황 가정
* user 2, 3은 아직 정산 진행 중
* user 4 는 정산 완료
*/
user1 = new User();
user1.saveUser("[email protected]", "abcDEF123!@", "user1", UserSignupType.LOCAL);

user2 = new User();
user2.saveUser("[email protected]", "abcDEF123!@", "user2", UserSignupType.LOCAL);

user3 = new User();
user3.saveUser("[email protected]", "abcDEF123!@", "user3", UserSignupType.LOCAL);

user4 = new User();
user4.saveUser("[email protected]", "abcDEF123!@", "user4", UserSignupType.LOCAL);

testSpace = new Space();
testSpace.saveSpace("testSpace", "test_profile_img_url");

testPayRequest = new PayRequest();
testPayRequest.savePayRequest(user2, testSpace, 30000, "우리은행", "111-111-111", false);
testPayRequest.savePayRequest(user1, testSpace, 30000, "우리은행", "111-111-111", false);

testPayRequestTarget_1 = new PayRequestTarget();
testPayRequestTarget_1.savePayRequestTarget(testPayRequest, user2.getUserId(), 10000, false);

testPayRequestTarget_2 = new PayRequestTarget();
testPayRequestTarget_2.savePayRequestTarget(testPayRequest, user3.getUserId(), 10000, false);

testPayRequestTarget_3 = new PayRequestTarget();
testPayRequestTarget_3.savePayRequestTarget(testPayRequest, user4.getUserId(), 10000, true);

testPayRequestTarget = new PayRequestTarget();
testPayRequestTarget.savePayRequestTarget(testPayRequest, user1.getUserId(), 10000, false);
}

@Test
@DisplayName("getPayRequestInfoForUser_메서드_테스트")
void 유저가_요청한_정산_중_진행중인_정산리스트_찾는_메서드_테스트() throws Exception {
@DisplayName("user1이_testSpace에서_요청한_정산중_현재진행중인_정산리스트_찾기")
void user1이_testSpace에서_요청한_정산중_현재진행중인_정산리스트_찾기() throws Exception {
//given
when(userUtils.findUserByUserId(user2.getUserId())).thenReturn(user2);
when(userUtils.findUserByUserId(user1.getUserId())).thenReturn(user1);
when(spaceUtils.findSpaceBySpaceId(testSpace.getSpaceId())).thenReturn(testSpace);
when(payDao.findPayRequestListByUser(user2, testSpace)).thenReturn(List.of(testPayRequest));
when(payDao.findPayRequestTargetListByPayRequest(testPayRequest)).thenReturn(List.of(testPayRequestTarget));
when(payDao.findPayRequestListByUser(user1, testSpace, false)).thenReturn(List.of(testPayRequest));
when(payDao.findPayRequestTargetListByPayRequest(testPayRequest)).thenReturn(List.of(testPayRequestTarget_1, testPayRequestTarget_2, testPayRequestTarget_3));

//when
List<PayRequestInfoDto> payRequestInfoForUser = payService.getPayRequestInfoForUser(user2.getUserId(), testSpace.getSpaceId());
List<PayRequestInfoDto> payRequestInfoForUser = payService.getPayRequestInfoForUser(user2.getUserId(), testSpace.getSpaceId(), false);

//then
assertThat(payRequestInfoForUser.size()).isEqualTo(1);

for (PayRequestInfoDto payRequestInfoDto : payRequestInfoForUser) {
assertThat(payRequestInfoDto.getTotalAmount()).isEqualTo(30000);
assertThat(payRequestInfoDto.getReceiveAmount()).isEqualTo(0);
assertThat(payRequestInfoDto.getTotalTargetNum()).isEqualTo(1);
assertThat(payRequestInfoDto.getReceiveTargetNum()).isEqualTo(0);
assertThat(payRequestInfoDto.getReceiveAmount()).isEqualTo(10000);
assertThat(payRequestInfoDto.getTotalTargetNum()).isEqualTo(3);
assertThat(payRequestInfoDto.getReceiveTargetNum()).isEqualTo(1);
}
}

@Test
@DisplayName("Test name")
void 유저가_요청받은_정산_중_진행중인_정산리스트_찾는_메서드_테스트() throws Exception {
@DisplayName("user2가_testSpace에서_요청받은_정산중_현재진행중인_정산리스트_찾기")
void user2가_testSpace에서_요청받은_정산중_현재진행중인_정산리스트_찾기() throws Exception {
//given
when(userUtils.findUserByUserId(user1.getUserId())).thenReturn(user1);
when(userUtils.findUserByUserId(user2.getUserId())).thenReturn(user2);
when(spaceUtils.findSpaceBySpaceId(testSpace.getSpaceId())).thenReturn(testSpace);
when(payDao.findPayRequestTargetListByUser(user1, testSpace)).thenReturn(List.of(testPayRequestTarget));
when(payDao.findPayRequestTargetListByUser(user2, testSpace, false)).thenReturn(List.of(testPayRequestTarget_1));

//when
List<PayReceiveInfoDto> payReceiveInfoForUser = payService.getPayReceiveInfoForUser(user1.getUserId(), testSpace.getSpaceId());
List<PayReceiveInfoDto> payReceiveInfoForUser = payService.getPayReceiveInfoForUser(user1.getUserId(), testSpace.getSpaceId(), false);

//then
assertThat(payReceiveInfoForUser.size()).isEqualTo(1);
for (PayReceiveInfoDto payReceiveInfoDto : payReceiveInfoForUser) {
assertThat(payReceiveInfoDto.getPayCreatorName()).isEqualTo(user2.getUserName());
assertThat(payReceiveInfoDto.getPayCreatorName()).isEqualTo(user1.getUserName());
assertThat(payReceiveInfoDto.getRequestAmount()).isEqualTo(10000);
}
}

@Test
@DisplayName("user2가_testSpace에서_요청한_정산중_완료된_정산리스트_찾기")
void user2가_TestSpace에서_요청한_정산중_완료된_정산리스트_찾기() throws Exception {
//given
/**
* user2 가 testSpace에서 user1에게 정산을 요청 & 이 정산은 완료된 상황을 가정
*/
PayRequest testPayRequest = new PayRequest();
testPayRequest.savePayRequest(user2, testSpace, 10000, "우리은행", "111-111-111", true);
PayRequestTarget testPayRequestTarget = new PayRequestTarget();
testPayRequestTarget.savePayRequestTarget(testPayRequest, user1.getUserId(), 10000, true);

when(userUtils.findUserByUserId(user2.getUserId())).thenReturn(user2);
when(spaceUtils.findSpaceBySpaceId(testSpace.getSpaceId())).thenReturn(testSpace);
when(payDao.findPayRequestListByUser(user2, testSpace, true)).thenReturn(List.of(testPayRequest));
when(payDao.findPayRequestTargetListByPayRequest(testPayRequest)).thenReturn(List.of(testPayRequestTarget));

//when
List<PayRequestInfoDto> payRequestInfoForUser = payService.getPayRequestInfoForUser(user2.getUserId(), testSpace.getSpaceId(), true);

//then
assertThat(payRequestInfoForUser.size()).isEqualTo(1);

for (PayRequestInfoDto payRequestInfoDto : payRequestInfoForUser) {
assertThat(payRequestInfoDto.getTotalAmount()).isEqualTo(10000);
assertThat(payRequestInfoDto.getReceiveAmount()).isEqualTo(10000);
assertThat(payRequestInfoDto.getTotalTargetNum()).isEqualTo(1);
assertThat(payRequestInfoDto.getReceiveTargetNum()).isEqualTo(1);
}
}


}
Loading