Skip to content

Commit

Permalink
fix: memberGoal 추가 api quests 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
koosco committed Dec 7, 2024
1 parent be8309f commit 2ff8064
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
16 changes: 6 additions & 10 deletions src/main/java/com/groom/orbit/goal/app/MemberGoalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,11 @@ public CommonSuccessDto deleteMemberGoal(Long memberId, Long memberGoalId) {

public GetMemberGoalResponseDto createGoal(Long memberId, MemberGoalRequestDto dto) {
Member member = memberQueryService.findMember(memberId);
Goal goal = getGoal(dto.title(), dto.category());

MemberGoal memberGoal = MemberGoal.create(member, goal);
goal.increaseCount();

int MemberGoalLen = memberGoalRepository.findAllByMemberIdAndIsCompleteFalse(memberId).size();

memberGoal.setSequence(MemberGoalLen + 1);
Goal goal = findGoal(dto.title(), dto.category());
int memberGoalSize = memberGoalRepository.findAllByMemberIdAndIsCompleteFalse(memberId).size();

MemberGoal memberGoal = MemberGoal.create(member, goal, memberGoalSize);
dto.quests().forEach(quest -> Quest.copyQuest(quest.title(), memberGoal));
MemberGoal savedMemberGoal = memberGoalRepository.save(memberGoal);
saveVector(memberId, goal);

Expand Down Expand Up @@ -125,7 +121,7 @@ private void saveVector(Long memberId, Goal goal) {
public CommonSuccessDto updateMemberGoal(
Long memberId, Long memberGoalId, MemberGoalRequestDto dto) {
MemberGoal memberGoal = findMemberGoal(memberGoalId);
Goal goal = getGoal(dto.title(), dto.category());
Goal goal = findGoal(dto.title(), dto.category());

memberGoal.validateMember(memberId);
updateVector(memberId, dto, memberGoal);
Expand All @@ -147,7 +143,7 @@ private void updateVector(Long memberId, MemberGoalRequestDto dto, MemberGoal me
vectorService.updateGoal(updateDto);
}

private Goal getGoal(String title, String category) {
private Goal findGoal(String title, String category) {
Optional<Goal> findGoal = goalQueryService.findGoalByTitleAndCategory(title, category);

return findGoal.orElseGet(() -> goalCommandService.createGoal(title, category));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package com.groom.orbit.goal.app.dto.request;

public record MemberGoalRequestDto(String title, String category) {}
import java.util.List;

public record MemberGoalRequestDto(
String title, String category, List<QuestTitleRequestDto> quests) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.groom.orbit.goal.app.dto.request;

public record QuestTitleRequestDto(String title) {}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ public class MemberGoal extends BaseTimeEntity {
@OneToMany(mappedBy = "memberGoal", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Quest> quests = new ArrayList<>();

public static MemberGoal create(Member member, Goal goal) {
public static MemberGoal create(Member member, Goal goal, int memberGoalSize) {
MemberGoal memberGoal = new MemberGoal();
memberGoal.member = member;
memberGoal.goal = goal;
memberGoal.sequence = memberGoalSize + 1;
goal.increaseCount();

return memberGoal;
}
Expand Down
29 changes: 9 additions & 20 deletions src/main/java/com/groom/orbit/quest/dao/entity/Quest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,20 @@ public static Quest create(
return quest;
}

public static Quest copyQuest(String title, MemberGoal memberGoal) {
Quest quest = new Quest();
quest.title = title;
quest.memberGoal = memberGoal;
memberGoal.getQuests().add(quest);

return quest;
}

public void validateMember(Long memberId) {
Member member = this.memberGoal.getMember();
member.validateId(memberId);
}

public int compareWithId(Long questId) {
if (this.questId.equals(questId)) {
return 0;
}

if (this.questId > questId) {
return 1;
}

return -1;
}

public void update(String title, Boolean isComplete, LocalDate deadline) {
if (title != null && !title.equals(this.title)) {
this.title = title;
Expand All @@ -82,12 +79,4 @@ public void update(String title, Boolean isComplete, LocalDate deadline) {
this.deadline = deadline;
}
}

public static Quest copyQuest(String title, MemberGoal memberGoal) {
Quest quest = new Quest();
quest.title = title;
quest.memberGoal = memberGoal;

return quest;
}
}

0 comments on commit 2ff8064

Please sign in to comment.