Skip to content

Commit

Permalink
Merge pull request #65 from co-niverse/refactor/purchaseRequest
Browse files Browse the repository at this point in the history
포인트 상품 구매 response 및 request 수정 & notification_seq 버그 수정
  • Loading branch information
GIVEN53 authored Oct 24, 2023
2 parents be7814c + 1f3f74d commit 6bd0541
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
public class Notification {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long notificationId;
private String title;
private String content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public class PointController {
private final PointService pointService;

/**
* 포인트 사용
* 포인트 상품 구매
*
* @param request 포인트 사용하고자 하는 내역 정보
* @return response 사용자 사용 완료 내역
* @param request 포인트 구매하고자 하는 내역 정보
* @return response 사용자 구매 완료 내역
* @since 1.0.0
*/
@PostMapping
public ResponseEntity<SuccessSingleResponse<UsePointResponse>> usePoint(@Valid @RequestBody UsePointRequest request,
public ResponseEntity<SuccessSingleResponse<UsePointResponse>> purchaseProduct(@Valid @RequestBody UsePointRequest request,
@AuthenticationPrincipal User user) {
UsePointResponse response = pointService.purchaseProduct(user.getUsername(), request);
return ResponseEntity.ok(new SuccessSingleResponse<>(HttpStatus.OK.getReasonPhrase(), response));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.coniverse.dangjang.domain.point.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;

/**
* 포인트 사용 Request
*
* @author EVE
* @since 1.0.0
*/
public record UsePointRequest(String phone, String type) {
public record UsePointRequest(@Pattern(regexp = "^010-\\d{4}-\\d{4}$", message = "010-XXXX-XXXX 조건에 맞는 전화번호가 필요합니다.") String phone,
@NotBlank(message = "상품 타입은 필수입니다.") String type, @NotBlank(message = "이름은 필수입니다.") String name,
String comment) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
* @author EVE
* @since 1.0.0
*/
public record UsePointResponse(String phone, String type, int changePoint, int balancedPoint) {
public record UsePointResponse(String phone, String type, int changePoint, int balancedPoint, String name, String comment) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.coniverse.dangjang.domain.user.entity.User;

import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -31,9 +32,14 @@ public class PurchaseHistory implements Persistable<PointId> {
@EmbeddedId
@Getter(AccessLevel.PRIVATE)
private PointId pointId;
@Column(nullable = false)
private String phone;
@ColumnDefault("false")
private boolean completed;
@Column(nullable = false)
private String name;
private String comment;

@MapsId("oauthId")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "oauth_id", insertable = false, updatable = false)
Expand All @@ -44,12 +50,14 @@ public class PurchaseHistory implements Persistable<PointId> {
private PointProduct pointProduct;

@Builder
public PurchaseHistory(User user, String phone, PointProduct pointProduct, boolean completed) {
public PurchaseHistory(User user, String phone, PointProduct pointProduct, boolean completed, String name, String comment) {
this.pointId = new PointId();
this.phone = phone;
this.completed = completed;
this.user = user;
this.pointProduct = pointProduct;
this.name = name;
this.comment = comment;
}

public String getOauthId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

import com.coniverse.dangjang.domain.point.dto.request.UsePointRequest;
import com.coniverse.dangjang.domain.point.entity.PointHistory;
import com.coniverse.dangjang.domain.point.entity.PointProduct;
import com.coniverse.dangjang.domain.point.entity.PurchaseHistory;
Expand Down Expand Up @@ -38,5 +39,5 @@ public interface PointMapper {
*
* @since 1.0.0
*/
PurchaseHistory toEntity(User user, PointProduct pointProduct, String phone);
PurchaseHistory toEntity(User user, PointProduct pointProduct, UsePointRequest userInfo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ public void addHealthConnectPoint(User user) {
public UsePointResponse purchaseProduct(String oauthId, UsePointRequest request) {
User user = userSearchService.findUserByOauthId(oauthId);
PointHistory savedPointHistory = addPointEvent(request.type(), user);
PurchaseHistory purchase = purchaseHistoryRepository.save(pointMapper.toEntity(user, savedPointHistory.getPointProduct(), request.phone()));
PurchaseHistory purchase = purchaseHistoryRepository.save(pointMapper.toEntity(user, savedPointHistory.getPointProduct(), request));
return new UsePointResponse(purchase.getPhone(), purchase.getPointProduct().getProductName(), savedPointHistory.getChangePoint(),
savedPointHistory.getBalancePoint());
savedPointHistory.getBalancePoint(), purchase.getName(), purchase.getComment());
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/schema-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ CREATE TABLE `PURCHASE_HISTORY`
`OAUTH_ID` varchar(50) NOT NULL,
`PRODUCT_NAME` varchar(255) NOT NULL,
`CREATED_AT` dateTime NOT NULL,
`PHONE` varchar(20) NOT NULL,
`PHONE` varchar(15) NOT NULL,
`NAME` varchar(20) NOT NULL,
`COMMENT` varchar(255),
`COMPLETED` boolean,
PRIMARY KEY (`OAUTH_ID`, `PRODUCT_NAME`, `CREATED_AT`),
FOREIGN KEY (`OAUTH_ID`) REFERENCES USERS (`OAUTH_ID`) ON DELETE CASCADE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class PointHistoryControllerTest extends ControllerTest {
@Test
void 포인트_상품_구매한다() throws Exception {
// given
UsePointRequest request = new UsePointRequest("010-xxxx-xxxx", "CU오천원금액권");
UsePointResponse response = new UsePointResponse("010-xxxx-xxxx", "CU오천원금액권", 5000, 1000);
UsePointRequest request = new UsePointRequest("010-0000-0000", "CU오천원금액권", "이름", "코멘트");
UsePointResponse response = new UsePointResponse("010-0000-0000", "CU오천원금액권", 5000, 1000, "이름", "코멘트");
when(pointService.purchaseProduct(any(), any())).thenReturn(response);
String content = objectMapper.writeValueAsString(request);

Expand All @@ -69,4 +69,20 @@ class PointHistoryControllerTest extends ControllerTest {
jsonPath("$.data.type").value(request.type())
);
}

@Test
void 조건에_맞지않는_request로_포인트_상품_구매를_요청하면_예외를_던진다() throws Exception {
// given
UsePointRequest request = new UsePointRequest("010-22-0000", "CU오천원금액권", "이름", "코멘트");

String content = objectMapper.writeValueAsString(request);

// when
ResultActions resultActions = post(mockMvc, URL, content);
// then
resultActions.andExpectAll(
status().isBadRequest(),
jsonPath("$.errorCode").value(400)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void tearDown() {
//given
유저 = userRepository.save(포인트_유저(today));
userPointRepository.save(유저_포인트_생성(유저.getOauthId(), 500));
UsePointRequest request = new UsePointRequest(유저.getOauthId(), "스타벅스 오천원 금액권");
UsePointRequest request = 포인트_사용_요청(유저, "다이소 오천원 금액권");
//when&then
assertThatThrownBy(() -> {
pointService.purchaseProduct(유저.getOauthId(), request);
Expand All @@ -157,7 +157,7 @@ void tearDown() {
int balancePoint = 유저_포인트.getPoint();
int accessPoint = balancePoint - 5000;

UsePointRequest request = new UsePointRequest(유저.getOauthId(), product);
UsePointRequest request = 포인트_사용_요청(유저, "다이소 오천원 금액권");

//when
pointService.purchaseProduct(유저.getOauthId(), request);
Expand All @@ -177,8 +177,7 @@ void tearDown() {
//given
유저 = userRepository.save(포인트_유저(today));
UserPoint 유저_포인트 = userPointRepository.save(유저_포인트_생성(유저.getOauthId(), 6000));

UsePointRequest request = new UsePointRequest(유저.getOauthId(), "두찜_만원");
UsePointRequest request = 포인트_사용_요청(유저, "두찜 만원 금액권");

//when
assertThatThrownBy(() -> pointService.purchaseProduct(유저.getOauthId(), request))
Expand Down Expand Up @@ -207,7 +206,7 @@ void tearDown() {
//given
유저 = userRepository.save(포인트_유저(today));
UserPoint 유저_포인트 = userPointRepository.save(유저_포인트_생성(유저.getOauthId(), 6000));
UsePointRequest request = new UsePointRequest(유저.getOauthId(), "스타벅스 오천원 금액권");
UsePointRequest request = 포인트_사용_요청(유저, "다이소 오천원 금액권");
ExecutorService executorService = Executors.newFixedThreadPool(30);
AtomicInteger successCount = new AtomicInteger();
AtomicInteger failedCount = new AtomicInteger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.util.List;

import com.coniverse.dangjang.domain.point.dto.request.UsePointRequest;
import com.coniverse.dangjang.domain.point.entity.PointProduct;
import com.coniverse.dangjang.domain.point.entity.UserPoint;
import com.coniverse.dangjang.domain.point.enums.PointType;
import com.coniverse.dangjang.domain.user.entity.User;

/**
* 포인트 관련 Fixture
Expand Down Expand Up @@ -44,4 +46,8 @@ public class PointFixture {
.toList();
}

public static UsePointRequest 포인트_사용_요청(User user, String type) {
return new UsePointRequest(user.getOauthId(), type, "이름", "코멘트");
}

}
2 changes: 2 additions & 0 deletions src/test/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ CREATE TABLE `PURCHASE_HISTORY`
`PRODUCT_NAME` varchar(255) NOT NULL,
`CREATED_AT` dateTime NOT NULL,
`PHONE` varchar(20) NOT NULL,
`NAME` varchar(20) NOT NULL,
`COMMENT` varchar(255),
`COMPLETED` boolean,
PRIMARY KEY (`OAUTH_ID`, `PRODUCT_NAME`, `CREATED_AT`),
FOREIGN KEY (`OAUTH_ID`) REFERENCES USERS (`OAUTH_ID`),
Expand Down

0 comments on commit 6bd0541

Please sign in to comment.