diff --git a/src/main/java/com/coniverse/dangjang/domain/notification/entity/Notification.java b/src/main/java/com/coniverse/dangjang/domain/notification/entity/Notification.java index b1486895..a14f9c35 100644 --- a/src/main/java/com/coniverse/dangjang/domain/notification/entity/Notification.java +++ b/src/main/java/com/coniverse/dangjang/domain/notification/entity/Notification.java @@ -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; diff --git a/src/main/java/com/coniverse/dangjang/domain/point/controller/PointController.java b/src/main/java/com/coniverse/dangjang/domain/point/controller/PointController.java index 29b8a6d9..977d8269 100644 --- a/src/main/java/com/coniverse/dangjang/domain/point/controller/PointController.java +++ b/src/main/java/com/coniverse/dangjang/domain/point/controller/PointController.java @@ -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> usePoint(@Valid @RequestBody UsePointRequest request, + public ResponseEntity> purchaseProduct(@Valid @RequestBody UsePointRequest request, @AuthenticationPrincipal User user) { UsePointResponse response = pointService.purchaseProduct(user.getUsername(), request); return ResponseEntity.ok(new SuccessSingleResponse<>(HttpStatus.OK.getReasonPhrase(), response)); diff --git a/src/main/java/com/coniverse/dangjang/domain/point/dto/request/UsePointRequest.java b/src/main/java/com/coniverse/dangjang/domain/point/dto/request/UsePointRequest.java index e4c63a56..b9ec4ee6 100644 --- a/src/main/java/com/coniverse/dangjang/domain/point/dto/request/UsePointRequest.java +++ b/src/main/java/com/coniverse/dangjang/domain/point/dto/request/UsePointRequest.java @@ -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) { } diff --git a/src/main/java/com/coniverse/dangjang/domain/point/dto/response/UsePointResponse.java b/src/main/java/com/coniverse/dangjang/domain/point/dto/response/UsePointResponse.java index 4a017157..b7e53cd5 100644 --- a/src/main/java/com/coniverse/dangjang/domain/point/dto/response/UsePointResponse.java +++ b/src/main/java/com/coniverse/dangjang/domain/point/dto/response/UsePointResponse.java @@ -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) { } diff --git a/src/main/java/com/coniverse/dangjang/domain/point/entity/PurchaseHistory.java b/src/main/java/com/coniverse/dangjang/domain/point/entity/PurchaseHistory.java index 29d7b3a7..8c21db9c 100644 --- a/src/main/java/com/coniverse/dangjang/domain/point/entity/PurchaseHistory.java +++ b/src/main/java/com/coniverse/dangjang/domain/point/entity/PurchaseHistory.java @@ -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; @@ -31,9 +32,14 @@ public class PurchaseHistory implements Persistable { @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) @@ -44,12 +50,14 @@ public class PurchaseHistory implements Persistable { 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() { diff --git a/src/main/java/com/coniverse/dangjang/domain/point/mapper/PointMapper.java b/src/main/java/com/coniverse/dangjang/domain/point/mapper/PointMapper.java index a3785739..bdace4e6 100644 --- a/src/main/java/com/coniverse/dangjang/domain/point/mapper/PointMapper.java +++ b/src/main/java/com/coniverse/dangjang/domain/point/mapper/PointMapper.java @@ -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; @@ -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); } diff --git a/src/main/java/com/coniverse/dangjang/domain/point/service/PointService.java b/src/main/java/com/coniverse/dangjang/domain/point/service/PointService.java index 282be4cc..b0ab7660 100644 --- a/src/main/java/com/coniverse/dangjang/domain/point/service/PointService.java +++ b/src/main/java/com/coniverse/dangjang/domain/point/service/PointService.java @@ -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()); } /** diff --git a/src/main/resources/schema-dev.sql b/src/main/resources/schema-dev.sql index c905bd02..4ef2f0e9 100644 --- a/src/main/resources/schema-dev.sql +++ b/src/main/resources/schema-dev.sql @@ -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, diff --git a/src/test/java/com/coniverse/dangjang/domain/point/controller/PointHistoryControllerTest.java b/src/test/java/com/coniverse/dangjang/domain/point/controller/PointHistoryControllerTest.java index 77080c4a..7f617aa7 100644 --- a/src/test/java/com/coniverse/dangjang/domain/point/controller/PointHistoryControllerTest.java +++ b/src/test/java/com/coniverse/dangjang/domain/point/controller/PointHistoryControllerTest.java @@ -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); @@ -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) + ); + } } diff --git a/src/test/java/com/coniverse/dangjang/domain/point/service/PointHistoryServiceTest.java b/src/test/java/com/coniverse/dangjang/domain/point/service/PointHistoryServiceTest.java index ec1c4ab0..00a54cc4 100644 --- a/src/test/java/com/coniverse/dangjang/domain/point/service/PointHistoryServiceTest.java +++ b/src/test/java/com/coniverse/dangjang/domain/point/service/PointHistoryServiceTest.java @@ -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); @@ -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); @@ -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)) @@ -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(); diff --git a/src/test/java/com/coniverse/dangjang/fixture/PointFixture.java b/src/test/java/com/coniverse/dangjang/fixture/PointFixture.java index d211a371..08a67b42 100644 --- a/src/test/java/com/coniverse/dangjang/fixture/PointFixture.java +++ b/src/test/java/com/coniverse/dangjang/fixture/PointFixture.java @@ -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 @@ -44,4 +46,8 @@ public class PointFixture { .toList(); } + public static UsePointRequest 포인트_사용_요청(User user, String type) { + return new UsePointRequest(user.getOauthId(), type, "이름", "코멘트"); + } + } diff --git a/src/test/resources/schema.sql b/src/test/resources/schema.sql index 0ab3cd66..987c60f7 100644 --- a/src/test/resources/schema.sql +++ b/src/test/resources/schema.sql @@ -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`),