-
Notifications
You must be signed in to change notification settings - Fork 0
feat #48: chatroom에서 심부름 완료 버튼 클릭시 수행자 리워드 update 구현 #62
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
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
23 changes: 23 additions & 0 deletions
23
src/main/java/com/dangsim/reward/controller/RewardController.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,23 @@ | ||
| package com.dangsim.reward.controller; | ||
|
|
||
| import com.dangsim.reward.dto.response.RewardChatResponse; | ||
| import com.dangsim.reward.service.RewardService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class RewardController { | ||
| private final RewardService rewardService; | ||
|
|
||
| // "심부름 수행 버튼 클릭시" | ||
| @PostMapping("/api/reward/chat/{chatId}") | ||
| public RewardChatResponse rewardToPerformer(@PathVariable Long chatId) { | ||
|
|
||
| rewardService.updateRewardByTaskCompleteBtn(chatId); | ||
|
|
||
| return rewardService.rewardByChatId(chatId); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/dangsim/reward/dto/request/RewardChatRequest.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,10 @@ | ||
| package com.dangsim.reward.dto.request; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor | ||
| public class RewardChatRequest { // 프론트 채팅방이 전달하는 id | ||
| private Long chatId; | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/dangsim/reward/dto/response/RewardChatResponse.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.dangsim.reward.dto.response; | ||
|
|
||
| import com.dangsim.reward.entity.Reward; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @Builder | ||
| public class RewardChatResponse { | ||
| private Long performerId; | ||
| private BigDecimal beforeReward; | ||
| private BigDecimal amount; | ||
| private BigDecimal afterReward; | ||
| private LocalDateTime completedAt; | ||
|
|
||
| public static RewardChatResponse from(Reward statement) { | ||
| return RewardChatResponse.builder() | ||
| .performerId(statement.getUser().getId()) | ||
| .beforeReward(statement.getBeforeReward()) | ||
| .amount(statement.getAmount()) | ||
| .afterReward(statement.getBeforeReward().add(statement.getAmount())) | ||
| .completedAt(statement.getCompletedAt()) | ||
| .build(); | ||
| } | ||
| } |
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,82 @@ | ||
| package com.dangsim.reward.entity; | ||
|
|
||
| import com.dangsim.common.entity.BaseEntity; | ||
| import com.dangsim.task.entity.Task; | ||
| import com.dangsim.user.entity.User; | ||
| import jakarta.persistence.*; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import lombok.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| import static lombok.AccessLevel.PRIVATE; | ||
|
|
||
| @Entity | ||
| @Table(name = "reward_statement") | ||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| public class Reward extends BaseEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| @Column(name = "reward_statement_id") | ||
| private Long id; | ||
|
|
||
| // 수행자가 가지고 있던 리워드 값 | ||
| @NotNull | ||
| @Column(name = "before_reward", nullable = false) | ||
| private BigDecimal beforeReward; | ||
|
|
||
| // task의 리워드 값 | ||
| @NotNull | ||
| @Column(name = "amount", nullable = false) | ||
| private BigDecimal amount; | ||
|
|
||
| // 수행자의 최종 리워드 값 | ||
| @NotNull | ||
| @Column(name = "after_reward", nullable = false) | ||
| private BigDecimal afterReward; | ||
|
|
||
| @NotNull | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "task_id", nullable = false, foreignKey = @ForeignKey(name = "fk_reward_task")) | ||
| private Task task; | ||
|
|
||
| @NotNull | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id", nullable = false, foreignKey = @ForeignKey(name = "fk_reward_user")) | ||
| private User user; | ||
|
|
||
| @NotNull | ||
| @Column(name = "requested_at", nullable = false) | ||
| private LocalDateTime requestedAt; | ||
|
|
||
| @Column(name = "completed_at") | ||
| private LocalDateTime completedAt; | ||
|
|
||
| @Builder(access = PRIVATE) | ||
| public Reward(BigDecimal amount, Task task, User user, LocalDateTime completedAt) { | ||
| this.amount = amount; | ||
| this.task = task; | ||
| this.user = user; | ||
| this.requestedAt = LocalDateTime.now(); | ||
| this.completedAt = completedAt; | ||
| } | ||
|
|
||
| public static Reward of(BigDecimal amount, Task task, User user, LocalDateTime completedAt) { | ||
| return Reward.builder() | ||
| .amount(amount) | ||
| .task(task) | ||
| .user(user) | ||
| .requestedAt(LocalDateTime.now()) | ||
| .completedAt(completedAt) | ||
| .build(); | ||
| } | ||
|
|
||
| public void markCompleted() { | ||
| this.completedAt = LocalDateTime.now(); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/dangsim/reward/repository/RewardRepository.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.dangsim.reward.repository; | ||
|
|
||
| import com.dangsim.reward.entity.Reward; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| @Repository | ||
| public interface RewardRepository extends JpaRepository<Reward, Long> { | ||
| Optional<Reward> findTopByTaskIdAndUserIdOrderByCreatedAtDesc(Long taskId, Long userId); | ||
| } |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/dangsim/reward/service/RewardService.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,93 @@ | ||
| package com.dangsim.reward.service; | ||
|
|
||
| import com.dangsim.chat.entity.ChatRoom; | ||
| import com.dangsim.chat.repository.ChatRoomRepository; | ||
| import com.dangsim.payment.entity.Payment; | ||
| import com.dangsim.payment.repository.PaymentRepository; | ||
| import com.dangsim.reward.dto.response.RewardChatResponse; | ||
| import com.dangsim.reward.entity.Reward; | ||
| import com.dangsim.reward.repository.RewardRepository; | ||
| import com.dangsim.task.entity.Task; | ||
| import com.dangsim.task.repository.TaskRepository; | ||
| import com.dangsim.user.entity.User; | ||
| import com.dangsim.user.repository.UserRepository; | ||
| import jakarta.persistence.EntityNotFoundException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.LocalDateTime; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class RewardService { | ||
|
|
||
| private final ChatRoomRepository chatRoomRepository; | ||
| private final TaskRepository taskRepository; | ||
| private final PaymentRepository paymentRepository; | ||
| private final UserRepository userRepository; | ||
| private final RewardRepository rewardStatementRepository; | ||
|
|
||
| /** | ||
| * 1. 실제 리워드 지급 로직 (심부름 완료 버튼 클릭 시 호출) | ||
| */ | ||
| @Transactional | ||
| public Reward updateRewardByTaskCompleteBtn(Long chatId) { | ||
| // 1. chatId → ChatRoom | ||
| ChatRoom chatRoom = chatRoomRepository.findById(chatId) | ||
| .orElseThrow(() -> new EntityNotFoundException("해당 채팅방을 찾을 수 없습니다.")); | ||
|
|
||
| // 2. ChatRoom → Task | ||
| Task task = chatRoom.getTask(); | ||
| Long taskId = task.getId(); | ||
|
|
||
| // 3. Task → 리워드, 마감 시간 | ||
| BigDecimal rewardAmount = task.getReward(); | ||
| LocalDateTime deadline = task.getDeadline(); | ||
|
|
||
| // 4. Task → Payment → performerId | ||
| Payment payment = paymentRepository.findByTaskId(taskId) | ||
| .orElseThrow(() -> new EntityNotFoundException("해당 Task에 대한 결제 정보를 찾을 수 없습니다.")); | ||
| Long performerId = payment.getPerformer().getId(); | ||
|
|
||
| // 5. performer → User | ||
| User performer = userRepository.findById(performerId) | ||
| .orElseThrow(() -> new EntityNotFoundException("해당 수행자 유저를 찾을 수 없습니다.")); | ||
|
|
||
| BigDecimal beforeReward = performer.getReward(); | ||
| BigDecimal afterReward = beforeReward.add(rewardAmount); | ||
|
|
||
| // 6. User reward 값 업데이트 | ||
| performer.updateReward(afterReward); | ||
| userRepository.save(performer); | ||
|
|
||
| // 7. 정산 내역 저장 | ||
| Reward statement = Reward.of( | ||
| rewardAmount, | ||
| task, | ||
| performer, | ||
| LocalDateTime.now() | ||
| ); | ||
| rewardStatementRepository.save(statement); | ||
|
|
||
| return statement; | ||
| } | ||
|
|
||
| /** | ||
| * 2. 리워드 정산 후 결과 반환 | ||
| */ | ||
| @Transactional(readOnly = true) | ||
| public RewardChatResponse rewardByChatId (Long chatId){ | ||
| ChatRoom chatRoom = chatRoomRepository.findById(chatId) | ||
| .orElseThrow(() -> new EntityNotFoundException("해당 채팅방을 찾을 수 없습니다.")); | ||
| Long taskId = chatRoom.getTask().getId(); | ||
| Long performerId = chatRoom.getPerformer().getId(); | ||
|
|
||
| Reward statement = rewardStatementRepository | ||
| .findTopByTaskIdAndUserIdOrderByCreatedAtDesc(taskId, performerId) | ||
| .orElseThrow(() -> new EntityNotFoundException("정산 내역을 찾을 수 없습니다.")); | ||
|
|
||
| return RewardChatResponse.from(statement); | ||
| } | ||
| } | ||
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
5 changes: 2 additions & 3 deletions
5
src/main/java/com/dangsim/rewardRefund/repository/RewardRefundRepository.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 |
|---|---|---|
| @@ -1,11 +1,10 @@ | ||
| package com.dangsim.rewardRefund.repository; | ||
|
|
||
| import com.dangsim.rewardRefund.entity.RewardRefund; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import com.dangsim.rewardRefund.entity.RewardRefundEntity; | ||
|
|
||
| @Repository | ||
| public interface RewardRefundRepository extends JpaRepository<RewardRefundEntity, Long> { | ||
| public interface RewardRefundRepository extends JpaRepository<RewardRefund, Long> { | ||
|
|
||
| } |
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.
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.
프로젝트후에 JPA 변경 감지에 대해서 공부해보세요~~