Skip to content
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

[ADD] 데일리 루틴 달성 취소 로직 추가 #312

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ResponseEntity<SuccessResponse<MemberDailyRoutineAchieveResponse>> achiev
) {
val memberId = Long.parseLong(principal.getName());
val response = MemberDailyRoutineAchieveResponse
.of(memberRoutineUpdateService.achieveMemberRoutine(
.of(memberRoutineUpdateService.updateAchievementStatus(
MemberRoutineAchieveServiceRequest.of(memberId, routineId)));
return ResponseEntity.ok(success(SUCCESS_ACHIEVE_ROUTINE.getMessage(), response));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public ResponseEntity<BaseResponse> achieveMemberHappinessRoutine(
@PathVariable Long routineId
) {
val memberId = Long.parseLong(principal.getName());
memberRoutineUpdateService.achieveMemberRoutine(MemberRoutineAchieveServiceRequest.of(memberId, routineId));
memberRoutineUpdateService.updateAchievementStatus(MemberRoutineAchieveServiceRequest.of(memberId, routineId));
return ResponseEntity.ok(success(SUCCESS_ACHIEVE_ROUTINE.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
public record MemberDailyRoutineAchieveResponse(
long routineId,
boolean isAchieve,
int achieveCount
int achieveCount,
boolean hasCotton
Chan531 marked this conversation as resolved.
Show resolved Hide resolved
) {

public static MemberDailyRoutineAchieveResponse of(MemberRoutineAchieveServiceResponse response) {
return MemberDailyRoutineAchieveResponse.builder()
.routineId(response.routineId())
.isAchieve(response.isAchieve())
.achieveCount(response.achieveCount())
.hasCotton(response.hasCotton())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class MemberRoutine {

private boolean isAchieve;

private boolean isAchieveToday;

private int achieveCount;

@Enumerated(value = STRING)
Expand All @@ -49,6 +51,7 @@ public class MemberRoutine {

public MemberRoutine(Member member, Routine routine) {
this.isAchieve = false;
this.isAchieveToday = false;
this.achieveCount = 0;
this.type = routine.getType();
this.routineId = routine.getId();
Expand All @@ -57,6 +60,7 @@ public MemberRoutine(Member member, Routine routine) {

public MemberRoutine(Member member, Challenge challenge) {
this.isAchieve = false;
this.isAchieveToday = false;
this.achieveCount = 0;
this.type = CHALLENGE;
this.routineId = challenge.getId();
Expand All @@ -65,6 +69,7 @@ public MemberRoutine(Member member, Challenge challenge) {

public MemberRoutine(DeletedMemberRoutine deletedMemberRoutine) {
this.isAchieve = isAchievedToday(deletedMemberRoutine);
this.isAchieveToday = isAchievedToday(deletedMemberRoutine);
this.achieveCount = deletedMemberRoutine.getAchieveCount();
this.type = deletedMemberRoutine.getType();
this.routineId = deletedMemberRoutine.getRoutineId();
Expand All @@ -77,6 +82,7 @@ public MemberRoutine(
) {
this.id = id;
this.isAchieve = isAchieve;
this.isAchieveToday = isAchieve;
this.achieveCount = achieveCount;
this.type = type;
this.routineId = routineId;
Expand All @@ -85,11 +91,18 @@ public MemberRoutine(

public void achieve() {
this.isAchieve = true;
this.isAchieveToday = true;
this.achieveCount++;
}

public void initAchieve() {
this.isAchieve = false;
this.isAchieveToday = false;
}

public void cancelAchievement() {
this.isAchieve = false;
this.achieveCount--;
}

public void checkMemberHas(Member member) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ public class MemberRoutineUpdateService {
private final MemberRoutineDeleter memberRoutineDeleter;
private final MemberFinder memberFinder;

public MemberRoutineAchieveServiceResponse achieveMemberRoutine(MemberRoutineAchieveServiceRequest request) {
public MemberRoutineAchieveServiceResponse updateAchievementStatus(MemberRoutineAchieveServiceRequest request) {
val member = memberFinder.findById(request.memberId());
val memberRoutine = memberRoutineFinder.findById(request.memberRoutineId());
val isAchievedToday = memberRoutine.isAchieveToday();

memberRoutine.checkMemberHas(member);
memberRoutine.achieve();
member.addCottonCount(memberRoutine.getType());
deleteMemberRoutineIfTypeIsOneTime(memberRoutine);
return MemberRoutineAchieveServiceResponse.of(memberRoutine);

if (memberRoutine.isAchieve()) {
memberRoutine.cancelAchievement();
} else {
if (!isAchievedToday) {
member.addCottonCount(memberRoutine.getType());
}
memberRoutine.achieve();
deleteMemberRoutineIfTypeIsOneTime(memberRoutine);
}

return MemberRoutineAchieveServiceResponse.of(memberRoutine, !isAchievedToday);
}

public void initDailyRoutines() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
public record MemberRoutineAchieveServiceResponse(
long routineId,
boolean isAchieve,
int achieveCount
int achieveCount,
boolean hasCotton
) {

public static MemberRoutineAchieveServiceResponse of(MemberRoutine routine) {
public static MemberRoutineAchieveServiceResponse of(MemberRoutine routine, boolean hasCotton) {
return MemberRoutineAchieveServiceResponse.builder()
.routineId(routine.getId())
.isAchieve(routine.isAchieve())
.achieveCount(routine.getAchieveCount())
.hasCotton(hasCotton)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void shouldUpdateAchieveCountAndCottonCountWhenAchieveDailyRoutine() {
memberRoutine.getId());

// when
memberRoutineUpdateService.achieveMemberRoutine(request);
memberRoutineUpdateService.updateAchievementStatus(request);

// then
assertThat(memberRoutine.isAchieve()).isTrue();
Expand Down Expand Up @@ -95,7 +95,7 @@ void shouldUpdateAchieveCountAndCottonCountWhenAchieveHappinessRoutine() {
memberRoutine.getId());

// when
memberRoutineUpdateService.achieveMemberRoutine(request);
memberRoutineUpdateService.updateAchievementStatus(request);

// then
assertThat(memberRoutine.isAchieve()).isTrue();
Expand Down
Loading