Skip to content

Commit

Permalink
Merge pull request #50 from Team-Growthook/fix/#49-action-plan-success
Browse files Browse the repository at this point in the history
[FIX]액션 플랜 완료 API로직 수정
  • Loading branch information
Hong0329 authored Jan 4, 2024
2 parents 1077368 + 5969d62 commit 19f5ea4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion growthookServer/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ out/

resources
../.DS_Store
.DS_Store
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import com.example.growthookserver.api.actionplan.dto.response.FinishedActionPlanGetResponseDto;
import com.example.growthookserver.api.actionplan.repository.ActionPlanRepository;
import com.example.growthookserver.api.actionplan.service.ActionPlanService;
import com.example.growthookserver.api.member.domain.Member;
import com.example.growthookserver.api.seed.domain.Seed;
import com.example.growthookserver.api.seed.repository.SeedRepository;
import com.example.growthookserver.common.exception.BadRequestException;
import com.example.growthookserver.common.response.ErrorStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -34,13 +37,9 @@ public void createActionPlan(Long seedId, ActionPlanCreateRequestDto actionPlanC

List<String> contents = actionPlanCreateRequestDto.getContents();

for(String content : contents) {
ActionPlan actionPlan = ActionPlan.builder()
.content(content)
.seed(seed)
.build();
actionPlanRepository.save(actionPlan);
}
contents.stream()
.map(content -> ActionPlan.builder().content(content).seed(seed).build())
.forEach(actionPlanRepository::save);
}

@Override
Expand Down Expand Up @@ -70,7 +69,13 @@ public void deleteActionPlan(Long actionPlanId) {
@Transactional
public void completeActionPlan(Long actionPlanId) {
ActionPlan existinActionPlan = actionPlanRepository.findActionPlanByIdOrThrow(actionPlanId);
if(existinActionPlan.getIsFinished()) {
throw new BadRequestException(ErrorStatus.ALREADY_COMPLETE_ACTIONPLAN.getMessage());
}
existinActionPlan.completeActionPlan(true);

Member member = existinActionPlan.getSeed().getCave().getMember();
member.incrementGatheredSsuk();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public Member(String nickname, String email, SocialPlatform socialPlatform, Bool
this.usedSsuk = usedSsuk;
this.gatheredSsuk = gatheredSsuk;
}

@Builder
public void incrementGatheredSsuk() {
this.gatheredSsuk = (this.gatheredSsuk == null ? 0 : this.gatheredSsuk) + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
@AllArgsConstructor(staticName = "of")
public class SeedAlarmGetResponseDto {
private int seedCount;
private int daysRemaining;
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,11 @@ public SeedAlarmGetResponseDto getSeedAlarm(Long memberId) {
List<Seed> seeds = seedRepository.findByCave_MemberIdAndLockDateBetween(memberId, now, threeDaysLater);

if(seeds.isEmpty()) {
return SeedAlarmGetResponseDto.of(0,0);
return SeedAlarmGetResponseDto.of(0);
}

int seedCount = seeds.size();

Seed earliestSeed = findEarliestSeed(seeds);
int daysRemaining = calculateDaysRemaining(now, earliestSeed.getLockDate());

return SeedAlarmGetResponseDto.of(seedCount, daysRemaining);
}

private Seed findEarliestSeed(List<Seed> seeds) {
return seeds.stream()
.min(Comparator.comparing(Seed::getLockDate))
.orElse(null);
}

private int calculateDaysRemaining(LocalDate now, LocalDate localDate) {
return (int) ChronoUnit.DAYS.between(now, localDate);
return SeedAlarmGetResponseDto.of(seedCount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum ErrorStatus {
NO_TOKEN("토큰을 넣어주세요."),
INVALID_MEMBER("유효하지 않은 유저입니다."),
ANOTHER_ACCESS_TOKEN("지원하지 않는 소셜 플랫폼입니다."),
ALREADY_COMPLETE_ACTIONPLAN("이미 완료된 액션 플랜입니다."),

/**
* 401 UNAUTHORIZED
Expand Down

0 comments on commit 19f5ea4

Please sign in to comment.