-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 쿠폰 등록 이벤트 기능 구현 #356
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6aa50c7
feat: Coupon 엔티티 추가 (#355)
sangu1026 83968a6
feat: Event관련 Exception과 ExceptionType 정의 (#355)
sangu1026 23bf110
feat: Coupon 이벤트 관련 요청 및 응답 DTO 정의 (#355)
sangu1026 3827b70
feat: CouponRepository에 쿠폰 코드로 쿠폰 엔티티 찾는 메서드 구현 (#355)
sangu1026 b87e71a
feat: CouponService에서 쿠폰 사용 메서드 구현 (#355)
sangu1026 366f08e
feat: 쿠폰 등록 API 구현 (#355)
sangu1026 d5c1b24
chore: coupon 테이블 생성 마이그레이션 스크립트 작성 (#355)
sangu1026 f817190
refactor: 개행 컨벤션에 따라 마지막 줄 개행 제거 (#355)
sangu1026 d2dfc7e
refactor: ENUM 명에 맞게 COUPON_EVENT_REWARD의 displayName 수정 (#355)
sangu1026 8c7c426
refactor: 개행 컨벤션에 따라 클래스내 첫번째 줄은 개행 추가 (#355)
sangu1026 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/com/team/buddyya/event/controller/EventController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.team.buddyya.event.controller; | ||
|
|
||
| import com.team.buddyya.auth.domain.CustomUserDetails; | ||
| import com.team.buddyya.event.dto.CouponRequest; | ||
| import com.team.buddyya.event.dto.CouponResponse; | ||
| import com.team.buddyya.event.service.EventService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/events") | ||
| @RequiredArgsConstructor | ||
| public class EventController { | ||
|
|
||
| private final EventService eventService; | ||
|
|
||
| @PostMapping("/coupon") | ||
| public ResponseEntity<CouponResponse> registerCoupon(@AuthenticationPrincipal CustomUserDetails userDetails, | ||
| @RequestBody CouponRequest request) { | ||
| CouponResponse response = eventService.useCoupon(userDetails.getStudentInfo(), request); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.team.buddyya.event.domain; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import static jakarta.persistence.GenerationType.IDENTITY; | ||
|
|
||
| @Entity | ||
| @Table(name = "coupon") | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class Coupon { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "code", length = 10, nullable = false, unique = true) | ||
| private String code; | ||
|
|
||
| @Column(name = "used", nullable = false) | ||
| private Boolean isUsed; | ||
|
|
||
| @Builder | ||
| public Coupon(String code) { | ||
| this.code = code; | ||
| this.isUsed = false; | ||
| } | ||
|
|
||
| public void markAsUsed() { | ||
| this.isUsed = true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.team.buddyya.event.dto; | ||
|
|
||
| public record CouponRequest(String code) { | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/team/buddyya/event/dto/CouponResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.team.buddyya.event.dto; | ||
|
|
||
| import com.team.buddyya.point.domain.Point; | ||
| import com.team.buddyya.point.domain.PointType; | ||
|
|
||
| public record CouponResponse(Integer point, | ||
| int pointChange) { | ||
|
|
||
| public static CouponResponse from(Point point, PointType pointType) { | ||
| return new CouponResponse(point.getCurrentPoint(), pointType.getPointChange()); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/team/buddyya/event/exception/EventException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.team.buddyya.event.exception; | ||
|
|
||
| import com.team.buddyya.common.exception.BaseException; | ||
| import com.team.buddyya.common.exception.BaseExceptionType; | ||
|
|
||
| public class EventException extends BaseException { | ||
|
|
||
| private final EventExceptionType exceptionType; | ||
|
|
||
| public EventException(EventExceptionType exceptionType) { | ||
| this.exceptionType = exceptionType; | ||
| } | ||
|
|
||
| @Override | ||
| public BaseExceptionType exceptionType() { | ||
| return exceptionType; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/team/buddyya/event/exception/EventExceptionType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.team.buddyya.event.exception; | ||
|
|
||
| import com.team.buddyya.common.exception.BaseExceptionType; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public enum EventExceptionType implements BaseExceptionType { | ||
|
|
||
| COUPON_NOT_FOUND(12000, HttpStatus.NOT_FOUND, "The coupon does not exist."), | ||
| COUPON_ALREADY_USED(12001, HttpStatus.BAD_REQUEST, "The coupon has already been used."); | ||
|
|
||
| private final int errorCode; | ||
| private final HttpStatus httpStatus; | ||
| private final String errorMessage; | ||
|
|
||
| EventExceptionType(int errorCode, HttpStatus httpStatus, String errorMessage) { | ||
| this.errorCode = errorCode; | ||
| this.httpStatus = httpStatus; | ||
| this.errorMessage = errorMessage; | ||
| } | ||
|
|
||
| @Override | ||
| public int errorCode() { | ||
| return errorCode; | ||
| } | ||
|
|
||
| @Override | ||
| public HttpStatus httpStatus() { | ||
| return httpStatus; | ||
| } | ||
|
|
||
| @Override | ||
| public String errorMessage() { | ||
| return errorMessage; | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/team/buddyya/event/repository/CouponRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.team.buddyya.event.repository; | ||
|
|
||
| import com.team.buddyya.event.domain.Coupon; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface CouponRepository extends JpaRepository<Coupon, Long> { | ||
|
|
||
| Optional<Coupon> findByCode(String code); | ||
| } | ||
39 changes: 39 additions & 0 deletions
39
src/main/java/com/team/buddyya/event/service/EventService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.team.buddyya.event.service; | ||
|
|
||
| import com.team.buddyya.auth.domain.StudentInfo; | ||
| import com.team.buddyya.event.domain.Coupon; | ||
| import com.team.buddyya.event.dto.CouponRequest; | ||
| import com.team.buddyya.event.dto.CouponResponse; | ||
| import com.team.buddyya.event.exception.EventException; | ||
| import com.team.buddyya.event.exception.EventExceptionType; | ||
| import com.team.buddyya.event.repository.CouponRepository; | ||
| import com.team.buddyya.point.domain.Point; | ||
| import com.team.buddyya.point.domain.PointType; | ||
| import com.team.buddyya.point.service.UpdatePointService; | ||
| import com.team.buddyya.student.domain.Student; | ||
| import com.team.buddyya.student.service.FindStudentService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| public class EventService { | ||
|
|
||
| private final FindStudentService findStudentService; | ||
| private final UpdatePointService updatePointService; | ||
| private final CouponRepository couponRepository; | ||
|
|
||
| public CouponResponse useCoupon(StudentInfo studentInfo, CouponRequest request) { | ||
| Coupon coupon = couponRepository.findByCode(request.code()) | ||
| .orElseThrow(() -> new EventException(EventExceptionType.COUPON_NOT_FOUND)); | ||
| if (coupon.getIsUsed()) { | ||
| throw new EventException(EventExceptionType.COUPON_ALREADY_USED); | ||
| } | ||
| Student student = findStudentService.findByStudentId(studentInfo.id()); | ||
| Point updatedPoint = updatePointService.updatePoint(student, PointType.COUPON_EVENT_REWARD); | ||
| coupon.markAsUsed(); | ||
| return CouponResponse.from(updatedPoint, PointType.COUPON_EVENT_REWARD); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/resources/db/migration/V31__Create_coupon_table.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| CREATE TABLE coupon | ||
| ( | ||
| id BIGINT AUTO_INCREMENT NOT NULL, | ||
| code VARCHAR(10) NOT NULL, | ||
| used BIT(1) NOT NULL, | ||
| CONSTRAINT pk_coupon PRIMARY KEY (id) | ||
| ); | ||
|
|
||
| ALTER TABLE coupon | ||
| ADD CONSTRAINT uc_coupon_code UNIQUE (code); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: 컨벤션상 개행 필요해보입니다!