-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat/hatch egg] #10
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
[Feat/hatch egg] #10
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4f552d9
:sparkles: feat : 부화기 등록, 애정 소모 기능 구현
ekgns33 cf20794
:truck: refactor : 디렉토리 이동
ekgns33 5a4d271
:white_check_mark: test : 부화기능 테스트 작성
ekgns33 4c64d07
:white_check_mark: test : 회원가입 서비스 테스트 수정
ekgns33 88fabc3
:hammer: script : `incubating_Eggs` 테이블 추가, 테스트 데이터 dml 추가
ekgns33 2d573d5
:white_check_mark: test : import 수정
ekgns33 7887d3c
:bug: fix : `mockito.ArgumentMatchers.eq`로 인한 에러 테스트코드 수정
ekgns33 205e4ce
:sparkles: feat : jpql 부화중인 알 조회 쿼리 수정
ekgns33 f0c0dec
:truck: refactor : controller에 존재하는 비즈니스 로직 파일 이동
ekgns33 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
71 changes: 71 additions & 0 deletions
71
src/main/java/org/runimo/runimo/user/controller/EggController.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,71 @@ | ||
| package org.runimo.runimo.user.controller; | ||
|
|
||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import jakarta.validation.Valid; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.runimo.runimo.common.response.SuccessResponse; | ||
| import org.runimo.runimo.user.controller.request.RegisterEggRequest; | ||
| import org.runimo.runimo.user.controller.request.UseLovePointRequest; | ||
| import org.runimo.runimo.user.service.dtos.*; | ||
| import org.runimo.runimo.user.enums.UserHttpResponseCode; | ||
| import org.runimo.runimo.user.service.usecases.eggs.IncubatingEggQueryUsecase; | ||
| import org.runimo.runimo.user.service.usecases.eggs.EggRegisterUsecase; | ||
| import org.runimo.runimo.user.service.usecases.eggs.GiveLovePointToEggUsecase; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.net.URI; | ||
|
|
||
| @Tag(name = "알 부화 API") | ||
| @RestController | ||
| @RequestMapping("/api/v1/users/eggs") | ||
| @RequiredArgsConstructor | ||
| public class EggController { | ||
|
|
||
| private final EggRegisterUsecase eggRegisterUsecase; | ||
| private final GiveLovePointToEggUsecase giveLovePointToEggUsecase; | ||
| private final IncubatingEggQueryUsecase incubatingEggQueryUsecase; | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<SuccessResponse<RegisterEggResponse>> registerEgg( | ||
| @UserId Long userId, | ||
| @Valid @RequestBody RegisterEggRequest request | ||
| ) { | ||
| RegisterEggResponse registerEggResponse = eggRegisterUsecase.execute( | ||
| new RegisterEggCommand(userId, request.itemId()) | ||
| ); | ||
| return ResponseEntity.created(URI.create("/api/v1/users/eggs")).body( | ||
| SuccessResponse.of( | ||
| UserHttpResponseCode.REGISTER_EGG_SUCCESS, | ||
| registerEggResponse | ||
| )); | ||
| } | ||
|
|
||
| @PatchMapping | ||
| public ResponseEntity<SuccessResponse<UseLovePointResponse>> useLovePoint( | ||
| @UserId Long userId, | ||
| @Valid @RequestBody UseLovePointRequest request | ||
| ) { | ||
| UseLovePointResponse useLovePointResponse = giveLovePointToEggUsecase.execute( | ||
| new UseLovePointCommand(userId, request.itemId(), request.lovePointAmount()) | ||
| ); | ||
| return ResponseEntity.ok().body( | ||
| SuccessResponse.of( | ||
| UserHttpResponseCode.USE_LOVE_POINT_SUCCESS, | ||
| useLovePointResponse | ||
| )); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<SuccessResponse<QueryIncubatingEggResponse>> getEgg( | ||
| @UserId Long userId | ||
| ) { | ||
| QueryIncubatingEggResponse response = incubatingEggQueryUsecase.execute(userId); | ||
| return ResponseEntity.ok().body( | ||
| SuccessResponse.of( | ||
| UserHttpResponseCode.MY_PAGE_DATA_FETCHED, | ||
| 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
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
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
6 changes: 6 additions & 0 deletions
6
src/main/java/org/runimo/runimo/user/controller/request/RegisterEggRequest.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,6 @@ | ||
| package org.runimo.runimo.user.controller.request; | ||
|
|
||
| public record RegisterEggRequest( | ||
| Long itemId | ||
| ) { | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/org/runimo/runimo/user/controller/request/UseLovePointRequest.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,7 @@ | ||
| package org.runimo.runimo.user.controller.request; | ||
|
|
||
| public record UseLovePointRequest( | ||
| Long itemId, | ||
| Long lovePointAmount | ||
| ) { | ||
| } |
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,8 @@ | ||
| package org.runimo.runimo.user.domain; | ||
|
|
||
| public enum EggStatus { | ||
| WAITING, // 부화 대기 | ||
| INCUBATING, // 부화 중 | ||
| INCUBATED, // 부화 완료 대기 | ||
| HATCHED // 부화 완료 | ||
| } |
90 changes: 90 additions & 0 deletions
90
src/main/java/org/runimo/runimo/user/domain/IncubatingEgg.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,90 @@ | ||
| package org.runimo.runimo.user.domain; | ||
|
|
||
| import jakarta.persistence.*; | ||
| import java.util.Objects; | ||
| import lombok.AccessLevel; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import org.runimo.runimo.common.BaseEntity; | ||
|
|
||
| @Table(name = "incubating_eggs") | ||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class IncubatingEgg extends BaseEntity { | ||
|
|
||
| @Column(name = "user_id", nullable = false) | ||
| private Long userId; | ||
|
|
||
| @Column(name = "egg_id", nullable = false) | ||
| private Long eggId; | ||
|
|
||
| @Column(name = "current_love_point_amount", nullable = false) | ||
| private Long currentLovePointAmount; | ||
|
|
||
| @Column(name = "hatch_require_amount", nullable = false) | ||
| private Long hatchRequireAmount; | ||
|
|
||
| @Column(name = "egg_status", nullable = false) | ||
| @Enumerated(EnumType.STRING) | ||
| private EggStatus status; | ||
|
|
||
| @Builder | ||
| public IncubatingEgg(Long userId, Long eggId, Long currentLovePointAmount, Long hatchRequireAmount, EggStatus status) { | ||
| validateCreation(status, currentLovePointAmount, hatchRequireAmount); | ||
| this.userId = userId; | ||
| this.eggId = eggId; | ||
| this.currentLovePointAmount = currentLovePointAmount; | ||
| this.hatchRequireAmount = hatchRequireAmount; | ||
| this.status = status; | ||
| } | ||
|
|
||
| public void startIncubation() { | ||
| validateStateTransition(EggStatus.WAITING, "부화 대기중인 알에만 부화 시작가능"); | ||
| this.status = EggStatus.INCUBATING; | ||
| } | ||
|
|
||
| public void gainLovePoint(final Long amount) { | ||
| validateStateTransition(EggStatus.INCUBATING, "부화중인 알에만 애정 포인트 추가가능"); | ||
| validateAmount(amount); | ||
| if (currentLovePointAmount + amount > hatchRequireAmount) { | ||
| throw new IllegalArgumentException("애정 포인트가 부화에 필요한 양을 초과했습니다. 현재: " + currentLovePointAmount + ", 추가량: " + amount + ", 필요량: " + hatchRequireAmount); | ||
| } | ||
| this.currentLovePointAmount += amount; | ||
| if (Objects.equals(this.currentLovePointAmount, hatchRequireAmount)) { | ||
| this.status = EggStatus.INCUBATED; | ||
| } | ||
| } | ||
|
|
||
| public void hatch() { | ||
| validateStateTransition(EggStatus.INCUBATED, "애정 포인트가 가득찬 알만 부화 가능"); | ||
| this.status = EggStatus.HATCHED; | ||
| } | ||
|
|
||
| private void validateCreation(final EggStatus status, final Long currentLovePointAmount, final Long hatchRequireAmount) { | ||
| if(status == EggStatus.WAITING && currentLovePointAmount != 0) { | ||
| throw new IllegalArgumentException("부화 대기중인 알은 애정 포인트가 0이어야 합니다. 현재: " + currentLovePointAmount); | ||
| } | ||
| if(hatchRequireAmount <= 0) { | ||
| throw new IllegalArgumentException("부화에 필요한 애정 포인트는 0보다 커야 합니다. 현재: " + hatchRequireAmount); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private void validateStateTransition(final EggStatus expected, final String message) { | ||
| if (status != expected) { | ||
| throw new IllegalStateException(message + " 현재 상태: " + status); | ||
| } | ||
| } | ||
|
|
||
| private void validateAmount(final Long amount) { | ||
| if (amount == null || amount <= 0) { | ||
| throw new IllegalArgumentException("애정 포인트는 0보다 커야 합니다. 현재: " + amount); | ||
| } | ||
| } | ||
|
|
||
| public boolean isReadyToHatch() { | ||
| return this.status == EggStatus.INCUBATED && currentLovePointAmount.equals(hatchRequireAmount); | ||
| } | ||
| } |
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
32 changes: 32 additions & 0 deletions
32
src/main/java/org/runimo/runimo/user/repository/IncubatingEggRepository.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,32 @@ | ||
| package org.runimo.runimo.user.repository; | ||
|
|
||
| import jakarta.persistence.LockModeType; | ||
| import jakarta.persistence.QueryHint; | ||
| import org.runimo.runimo.user.domain.IncubatingEgg; | ||
| import org.runimo.runimo.user.service.dtos.IncubatingEggView; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Lock; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.jpa.repository.QueryHints; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| @Repository | ||
| public interface IncubatingEggRepository extends JpaRepository<IncubatingEgg, Integer> { | ||
|
|
||
| @Lock(LockModeType.PESSIMISTIC_WRITE) | ||
| @QueryHints({@QueryHint(name = "jakarta.persistence.lock.timeout", value = "3000")}) | ||
| @Query("select ie from IncubatingEgg ie where ie.userId = :userId and ie.id = :eggId") | ||
| Optional<IncubatingEgg> findByUserIdAndEggIdForUpdate(Long userId, Long eggId); | ||
|
|
||
| @Query("select ie from IncubatingEgg ie where ie.userId = :userId and (ie.status = 'INCUBATING' or ie.status = 'INCUBATED')") | ||
| List<IncubatingEgg> findAllByUserId(Long userId); | ||
|
|
||
| @Query("select new org.runimo.runimo.user.service.dtos.IncubatingEggView(ie.id, e.name, e.imgUrl, ie.hatchRequireAmount, ie.currentLovePointAmount, ie.status) " + | ||
| "from IncubatingEgg ie " + | ||
| "join Egg e on e.id = ie.eggId " + | ||
| "where ie.userId = :userId and (ie.status = 'INCUBATING' or ie.status = 'INCUBATED')") | ||
| List<IncubatingEggView> findAllViewByUserId(Long userId); | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/runimo/runimo/user/service/IncubatingEggFinder.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,27 @@ | ||
| package org.runimo.runimo.user.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.runimo.runimo.user.domain.IncubatingEgg; | ||
| import org.runimo.runimo.user.repository.IncubatingEggRepository; | ||
| import org.runimo.runimo.user.service.dtos.IncubatingEggView; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class IncubatingEggFinder { | ||
|
|
||
| private final IncubatingEggRepository incubatingEggRepository; | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<IncubatingEgg> findIncubatingEggsByUserId(Long userId) { | ||
| return incubatingEggRepository.findAllByUserId(userId); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<IncubatingEggView> findIncubatingEggsViewByUserId(Long userId) { | ||
| return incubatingEggRepository.findAllViewByUserId(userId); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/main/java/org/runimo/runimo/user/service/IncubatingEggProcessor.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,38 @@ | ||
| package org.runimo.runimo.user.service; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.runimo.runimo.item.domain.Egg; | ||
| import org.runimo.runimo.user.domain.IncubatingEgg; | ||
| import org.runimo.runimo.user.repository.IncubatingEggRepository; | ||
| import org.runimo.runimo.user.service.dtos.UseLovePointCommand; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class IncubatingEggProcessor { | ||
|
|
||
| private final IncubatingEggRepository incubatingEggRepository; | ||
|
|
||
| @Transactional | ||
| public IncubatingEgg create(Long userId, Egg egg) { | ||
| IncubatingEgg incubatingEgg = IncubatingEgg.builder() | ||
| .userId(userId) | ||
| .eggId(egg.getId()) | ||
| .currentLovePointAmount(0L) | ||
| .hatchRequireAmount(egg.getHatchRequireAmount()) | ||
| .build(); | ||
| return incubatingEggRepository.save(incubatingEgg); | ||
| } | ||
|
|
||
| @Transactional | ||
| public IncubatingEgg giveLovePoint(UseLovePointCommand useLovePointCommand) { | ||
| IncubatingEgg incubatingEgg = incubatingEggRepository.findByUserIdAndEggIdForUpdate( | ||
| useLovePointCommand.userId(), | ||
| useLovePointCommand.incubatingEggId()) | ||
| .orElseThrow(() -> new IllegalArgumentException("Incubating egg not found")); | ||
| incubatingEgg.gainLovePoint(useLovePointCommand.lovePoint()); | ||
| return incubatingEgg; | ||
| } | ||
|
|
||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.