Skip to content

Commit

Permalink
Merge pull request #312 from Team-Sopetit/fix/#274-daily-routine-achi…
Browse files Browse the repository at this point in the history
…eved

[ADD] 데일리 루틴 달성 취소 로직 추가
  • Loading branch information
thguss authored Aug 13, 2024
2 parents 061b302 + 383098d commit 44f17bc
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
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
) {

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

0 comments on commit 44f17bc

Please sign in to comment.