Skip to content

Commit

Permalink
Merge pull request #165 from CoolPeace-yanolza/develop
Browse files Browse the repository at this point in the history
01.22 17시 배포
  • Loading branch information
KwonJuHwan authored Jan 22, 2024
2 parents ee9df94 + 6c256a7 commit 102b037
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.coolpeace.domain.settlement.dto.response;

import java.util.List;

public record PageSettlementResponse(
Long totalSettlementCount,
int totalPageCount,
List<SettlementResponse> settlementResponses
) {

public static PageSettlementResponse from(Long totalSettlementCount, int totalPageCount,
List<SettlementResponse> settlementResponses) {
return new PageSettlementResponse(
totalSettlementCount,
totalPageCount,
settlementResponses);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public record SettlementResponse(
int couponCount,
int discountPrice,
int cancelPrice,
int supplyPrice,
int sumPrice,
LocalDate completeAt
) {
Expand All @@ -23,23 +22,21 @@ public static SettlementResponse from(Settlement settlement) {
settlement.getCouponCount(),
settlement.getDiscountPrice(),
settlement.getCancelPrice(),
settlement.getSupplyPrice(),
settlement.getSumPrice(),
settlement.getCompleteAt()
);
}

public static SettlementResponse from(LocalDate couponUseDate, String couponNumber,
String couponName, int couponCount, int discountPrice, int cancelPrice,
int supplyPrice, int sumPrice, LocalDate completeAt) {
int sumPrice, LocalDate completeAt) {
return new SettlementResponse(
couponUseDate,
couponNumber,
couponName,
couponCount,
discountPrice,
cancelPrice,
supplyPrice,
sumPrice,
completeAt
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public class Settlement extends BaseTimeEntity {
private int discountPrice = 0;
@Column(nullable = false)
private int cancelPrice = 0;
@Column(nullable = false)
private int supplyPrice = 0;

@Column(nullable = false)
private int sumPrice = 0;
Expand All @@ -47,14 +45,13 @@ public class Settlement extends BaseTimeEntity {
private Accommodation accommodation;

public Settlement(Long id, LocalDate couponUseDate, int couponCount,
int discountPrice, int cancelPrice, int supplyPrice, int sumPrice,
int discountPrice, int cancelPrice, int sumPrice,
LocalDate completeAt,Coupon coupon, Accommodation accommodation) {
this.id = id;
this.couponUseDate = couponUseDate;
this.couponCount = couponCount;
this.discountPrice = discountPrice;
this.cancelPrice = cancelPrice;
this.supplyPrice = supplyPrice;
this.sumPrice = sumPrice;
this.completeAt = completeAt;
this.coupon = coupon;
Expand All @@ -66,6 +63,6 @@ public void completeSettlement() {
}

public void sumPrice() {
this.sumPrice = this.discountPrice + this.cancelPrice + this.supplyPrice;
this.sumPrice = this.discountPrice + this.cancelPrice ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.coolpeace.domain.member.exception.MemberNotFoundException;
import com.coolpeace.domain.member.repository.MemberRepository;
import com.coolpeace.domain.settlement.dto.request.SearchSettlementParams;
import com.coolpeace.domain.settlement.dto.response.PageSettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SumSettlementResponse;
import com.coolpeace.domain.statistics.entity.MonthlySearchDate;
Expand Down Expand Up @@ -56,7 +57,7 @@ public SumSettlementResponse sumSettlement(String memberId, Long accommodationId
return SumSettlementResponse.from(thisMonthSumSettlement, lastMonthSumSettlement);
}

public List<SettlementResponse> searchSettlement(String memberId, Long accommodationId,
public PageSettlementResponse searchSettlement(String memberId, Long accommodationId,
SearchSettlementParams searchSettlementParams, int page, int pageSize) {

Accommodation accommodation = checkAccommodationMatchMember(memberId, accommodationId);
Expand All @@ -77,7 +78,15 @@ public List<SettlementResponse> searchSettlement(String memberId, Long accommoda
.findAllByAccommodationAndCouponUseDateGreaterThanEqualAndCouponUseDateLessThanEqualOrderByCouponCountDesc
(PageRequest.of(page,pageSize), accommodation, startDate, endDate);
};
return settlements.stream().map(SettlementResponse::from).toList();

long totalElements = settlements.getTotalElements();
int totalPages = settlements.getTotalPages();

List<SettlementResponse> settlementResponses =
settlements.stream().map(SettlementResponse::from).toList();

return PageSettlementResponse.from(totalElements, totalPages, settlementResponses);

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.coolpeace.domain.settlement.controller.SettlementController;
import com.coolpeace.domain.settlement.dto.request.SearchSettlementParams;
import com.coolpeace.domain.settlement.dto.response.PageSettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SumSettlementResponse;
import com.coolpeace.domain.settlement.repository.OrderBy;
Expand Down Expand Up @@ -74,19 +75,21 @@ void searchSettlement_success() throws Exception {
//given
SettlementResponse settlementResponse1 = SettlementResponse
.from(LocalDate.now(),"YC000011","크리스마스 쿠폰", 2,
10000, 0, 0, 10000,
10000, 0, 10000,
LocalDate.now().plusMonths(1));
SettlementResponse settlementResponse2 = SettlementResponse
.from(LocalDate.now(),"YC000012","대박 할인 쿠폰", 2,
20000, 1000, 0, 19000,
20000, -1000, 19000,
LocalDate.now().plusMonths(1));

List<SettlementResponse> settlementResponseList = new ArrayList<>();
settlementResponseList.add(settlementResponse1);
settlementResponseList.add(settlementResponse2);
PageSettlementResponse pageSettlementResponse =
PageSettlementResponse.from(2L, 1, settlementResponseList);

given(settlementService.searchSettlement(any(), anyLong(), any(SearchSettlementParams.class),
anyInt(),anyInt())).willReturn(settlementResponseList);
anyInt(),anyInt())).willReturn(pageSettlementResponse);
//when,then
mockMvc.perform(RestDocumentationRequestBuilders.get("/v1/settlements/{accommodation_id}", 1)
.queryParam("start","2023-09-27")
Expand All @@ -107,15 +110,16 @@ void searchSettlement_success() throws Exception {
)
.responseSchema(Schema.schema(SettlementResponse.class.getSimpleName()))
.responseFields(
fieldWithPath("[].coupon_use_date").type(JsonFieldType.STRING).description("쿠폰 사용일"),
fieldWithPath("[].coupon_number").type(JsonFieldType.STRING).description("쿠폰 번호"),
fieldWithPath("[].coupon_name").type(JsonFieldType.STRING).description("관리 쿠폰명"),
fieldWithPath("[].coupon_count").type(JsonFieldType.NUMBER).description("사용건수"),
fieldWithPath("[].discount_price").type(JsonFieldType.NUMBER).description("쿠폰할인금액"),
fieldWithPath("[].cancel_price").type(JsonFieldType.NUMBER).description("쿠폰취소금액"),
fieldWithPath("[].supply_price").type(JsonFieldType.NUMBER).description("오늘까지 정산 내역"),
fieldWithPath("[].sum_price").type(JsonFieldType.NUMBER).description("지원 금액"),
fieldWithPath("[].complete_at").type(JsonFieldType.STRING).description("정산 완료일")
fieldWithPath(".total_settlement_count").type(JsonFieldType.NUMBER).description("검색결과 총 개수"),
fieldWithPath(".total_page_count").type(JsonFieldType.NUMBER).description("검색결과 페이지 총 개수"),
fieldWithPath(".settlement_responses[].coupon_use_date").type(JsonFieldType.STRING).description("쿠폰 사용일"),
fieldWithPath(".settlement_responses[].coupon_number").type(JsonFieldType.STRING).description("쿠폰 번호"),
fieldWithPath(".settlement_responses[].coupon_name").type(JsonFieldType.STRING).description("관리 쿠폰명"),
fieldWithPath(".settlement_responses[].coupon_count").type(JsonFieldType.NUMBER).description("사용건수"),
fieldWithPath(".settlement_responses[].discount_price").type(JsonFieldType.NUMBER).description("쿠폰할인금액"),
fieldWithPath(".settlement_responses[].cancel_price").type(JsonFieldType.NUMBER).description("쿠폰취소금액"),
fieldWithPath(".settlement_responses[].sum_price").type(JsonFieldType.NUMBER).description("정산 금액"),
fieldWithPath(".settlement_responses[].complete_at").type(JsonFieldType.STRING).description("정산 완료일")
)
.build()
)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.coolpeace.domain.settlement.dto.request.SearchSettlementParams;
import com.coolpeace.domain.settlement.dto.response.PageSettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SumSettlementResponse;
import com.coolpeace.domain.settlement.entity.Settlement;
import com.coolpeace.domain.settlement.repository.OrderBy;
import com.coolpeace.domain.settlement.service.SettlementService;
import java.time.LocalDate;
Expand All @@ -24,6 +26,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;

Expand Down Expand Up @@ -61,44 +66,49 @@ void searchSettlement_success() throws Exception {
//given
SettlementResponse settlementResponse1 = SettlementResponse
.from(LocalDate.now(),"YC000011","크리스마스 쿠폰", 2,
10000, 0, 0, 10000,
10000, 0, 10000,
LocalDate.now().plusMonths(1));
SettlementResponse settlementResponse2 = SettlementResponse
.from(LocalDate.now(),"YC000012","대박 할인 쿠폰", 2,
20000, 1000, 0, 19000,
20000, -1000, 19000,
LocalDate.now().plusMonths(1));


List<SettlementResponse> settlementResponseList = new ArrayList<>();
settlementResponseList.add(settlementResponse1);
settlementResponseList.add(settlementResponse2);

PageSettlementResponse pageSettlementResponse =
PageSettlementResponse.from(2L, 1, settlementResponseList);

given(settlementService.searchSettlement(any(), anyLong(), any(SearchSettlementParams.class),
anyInt(),anyInt())).willReturn(settlementResponseList);
anyInt(),anyInt())).willReturn(pageSettlementResponse);
//when,then
mockMvc.perform(get("/v1/settlements/{accommodation_id}", 1L)
.queryParam("start", "2023-09-27")
.queryParam("order", String.valueOf(OrderBy.COUPON_USE_DATE))
.queryParam("end", "2023-12-03"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].coupon_number").isString())
.andExpect(jsonPath("$[0].coupon_name").isString())
.andExpect(jsonPath("$[0].coupon_use_date").exists())
.andExpect(jsonPath("$[0].coupon_count").isNumber())
.andExpect(jsonPath("$[0].discount_price").isNumber())
.andExpect(jsonPath("$[0].cancel_price").isNumber())
.andExpect(jsonPath("$[0].supply_price").isNumber())
.andExpect(jsonPath("$[0].sum_price").isNumber())
.andExpect(jsonPath("$[0].complete_at").exists())
.andExpect(jsonPath("$[1].coupon_number").isString())
.andExpect(jsonPath("$[1].coupon_name").isString())
.andExpect(jsonPath("$[1].coupon_use_date").exists())
.andExpect(jsonPath("$[1].coupon_count").isNumber())
.andExpect(jsonPath("$[1].discount_price").isNumber())
.andExpect(jsonPath("$[1].cancel_price").isNumber())
.andExpect(jsonPath("$[1].supply_price").isNumber())
.andExpect(jsonPath("$[1].sum_price").isNumber())
.andExpect(jsonPath("$[1].complete_at").exists())
.andExpect(jsonPath("$.total_settlement_count").isNumber())
.andExpect(jsonPath("$.total_page_count").isNumber())
.andExpect(jsonPath("$.settlement_responses[0].coupon_number").isString())
.andExpect(jsonPath("$.settlement_responses[0].coupon_name").isString())
.andExpect(jsonPath("$.settlement_responses[0].coupon_use_date").exists())
.andExpect(jsonPath("$.settlement_responses[0].coupon_count").isNumber())
.andExpect(jsonPath("$.settlement_responses[0].discount_price").isNumber())
.andExpect(jsonPath("$.settlement_responses[0].cancel_price").isNumber())
.andExpect(jsonPath("$.settlement_responses[0].sum_price").isNumber())
.andExpect(jsonPath("$.settlement_responses[0].complete_at").exists())
.andExpect(jsonPath("$.settlement_responses[1].coupon_number").isString())
.andExpect(jsonPath("$.settlement_responses[1].coupon_name").isString())
.andExpect(jsonPath("$.settlement_responses[1].coupon_use_date").exists())
.andExpect(jsonPath("$.settlement_responses[1].coupon_count").isNumber())
.andExpect(jsonPath("$.settlement_responses[1].discount_price").isNumber())
.andExpect(jsonPath("$.settlement_responses[1].cancel_price").isNumber())
.andExpect(jsonPath("$.settlement_responses[1].sum_price").isNumber())
.andExpect(jsonPath("$.settlement_responses[1].complete_at").exists())
.andDo(print());

verify(settlementService,times(1))
.searchSettlement(any(), anyLong(), any(SearchSettlementParams.class), anyInt(),anyInt());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ void findAllByAccommodationAndCouponUseDateAfterAndCouponUseDateBeforeOrderByCou
Coupon coupon = new CouponTestBuilder(accommodation, member, randomsRooms).build();

Settlement settlement1 = new Settlement(1L, LocalDate.of(2023, 10, 27), 10, 1000,
0, 0, 1000, LocalDate.of(2023, 11, 10),coupon,accommodation);
0, 1000, LocalDate.of(2023, 11, 10),coupon,accommodation);
Settlement settlement2 = new Settlement(2L, LocalDate.of(2023, 12, 27), 10, 1000,
0, 0, 1000, LocalDate.of(2024, 1, 10),coupon,accommodation);
0, 1000, LocalDate.of(2024, 1, 10),coupon,accommodation);
Settlement settlement3 = new Settlement(3L, LocalDate.of(2023, 8, 27), 10, 1000,
0, 0, 1000, LocalDate.of(2023, 9, 10),coupon,accommodation);
0, 1000, LocalDate.of(2023, 9, 10),coupon,accommodation);
memberRepository.save(member);
accommodationRepository.save(accommodation);
roomRepository.saveAll(randomsRooms);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.coolpeace.domain.settlement.service;

import static org.assertj.core.api.Assertions.as;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -17,6 +18,7 @@
import com.coolpeace.domain.member.repository.MemberRepository;
import com.coolpeace.domain.room.entity.Room;
import com.coolpeace.domain.settlement.dto.request.SearchSettlementParams;
import com.coolpeace.domain.settlement.dto.response.PageSettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SettlementResponse;
import com.coolpeace.domain.settlement.dto.response.SumSettlementResponse;
import com.coolpeace.domain.settlement.entity.Settlement;
Expand Down Expand Up @@ -187,10 +189,10 @@ void _success() {

Coupon coupon = new CouponTestBuilder(accommodation, member, randomsRooms).build();
Settlement settlement1 = new Settlement(1L, LocalDate.now(), 10,
1000, 0, 0, 1000,
1000, 0, 1000,
LocalDate.now().plusMonths(1),coupon,accommodation);
Settlement settlement2 = new Settlement(2L, LocalDate.now(), 10,
1000, 0, 0, 1000,
1000, 0, 1000,
LocalDate.now().plusMonths(1),coupon,accommodation);
List<Settlement> settlements = new ArrayList<>();
settlements.add(settlement1);
Expand All @@ -207,23 +209,27 @@ void _success() {
.findAllByAccommodationAndCouponUseDateGreaterThanEqualAndCouponUseDateLessThanEqualOrderByCouponUseDateDesc
(any(Pageable.class), any(Accommodation.class), any(LocalDate.class),
any(LocalDate.class))).willReturn(settlementPage);


//when
List<SettlementResponse> settlementResponses = settlementService.searchSettlement("1",
1L, settlementParams, 1,10);
PageSettlementResponse pageSettlementResponse = settlementService.searchSettlement("1",
1L, settlementParams, 1, 10);
//then
assertThat(settlementResponses.get(0)).extracting
assertThat(pageSettlementResponse.totalSettlementCount()).isEqualTo(
settlementPage.getTotalElements());
assertThat(pageSettlementResponse.totalPageCount()).isEqualTo(
settlementPage.getTotalPages());
assertThat(pageSettlementResponse.settlementResponses().get(0)).extracting
("couponUseDate", "couponCount", "discountPrice", "cancelPrice",
"supplyPrice", "sumPrice", "completeAt")
"sumPrice", "completeAt")
.containsExactly(settlement1.getCouponUseDate(), settlement1.getCouponCount(),
settlement1.getDiscountPrice(), settlement1.getCancelPrice(),
settlement1.getSupplyPrice(),
settlement1.getSumPrice(), settlement1.getCompleteAt());
assertThat(settlementResponses.get(1)).extracting
assertThat(pageSettlementResponse.settlementResponses().get(1)).extracting
("couponUseDate", "couponCount", "discountPrice", "cancelPrice",
"supplyPrice", "sumPrice", "completeAt")
"sumPrice", "completeAt")
.containsExactly(settlement1.getCouponUseDate(), settlement2.getCouponCount(),
settlement2.getDiscountPrice(), settlement2.getCancelPrice(),
settlement2.getSupplyPrice(),
settlement2.getSumPrice(), settlement2.getCompleteAt());

}
Expand Down

0 comments on commit 102b037

Please sign in to comment.