Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import com.flytrap.venusplanner.api.plan.domain.RecurringOption;
import com.flytrap.venusplanner.api.plan.infrastructure.repository.PlanRepository;
import com.flytrap.venusplanner.api.plan.infrastructure.repository.RecurringOptionRepository;
import com.flytrap.venusplanner.api.plan.presentation.dto.request.PlanCreateRequest;
import com.flytrap.venusplanner.api.plan.util.DateTimeUtils;
import com.flytrap.venusplanner.api.plan_category.domain.PlanCategory;
import com.flytrap.venusplanner.api.study.domain.Study;
import com.flytrap.venusplanner.api.plan.presentation.dto.request.PlanCreateRequest;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -21,21 +26,52 @@ public class PlanService {
private final RecurringOptionRepository recurringOptionRepository;

public Long savePlan(Study study, PlanCategory planCategory, PlanCreateRequest request) {
//TODO: 반복 옵션 설정시 DB에 여러 plan 저장 로직 추가
Plan plan = request.toEntity(study, planCategory);
planRepository.save(plan);

if (plan.getRecurringOption() != null) {
List<Plan> recurringPlan = createPlansWithRecurringPlan(plan);
planRepository.saveAll(recurringPlan);
}

return plan.getId();
}

private List<Plan> createPlansWithRecurringPlan(Plan planTemplate) {
int recurrenceCount = planTemplate.getRecurringOption().getRecurrenceCount();
ZonedDateTime startTimeZDT = DateTimeUtils.toZonedDateTime(planTemplate.getStartTime());
ZonedDateTime endTimeZDT = DateTimeUtils.toZonedDateTime(planTemplate.getEndTime());
ChronoUnit chronoUnit = planTemplate.getRecurringOption().getFrequency().getChronoUnit();

List<Plan> createdPlans = new ArrayList<>();
for (int i = 1; i < recurrenceCount; i++) {
Instant startTime = DateTimeUtils.toInstant(startTimeZDT.plus(i, chronoUnit));
Instant endTime = DateTimeUtils.toInstant(endTimeZDT.plus(i, chronoUnit));

Plan newPlan = Plan.builder()
.study(planTemplate.getStudy())
.planCategory(planTemplate.getPlanCategory())
.recurringOption(planTemplate.getRecurringOption())
.title(planTemplate.getTitle())
.description(planTemplate.getDescription())
.startTime(startTime)
.endTime(endTime)
.notificationTime(planTemplate.getNotificationTime())
.build();
Comment on lines +51 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

서비스 로직 코드 읽는데 빌더가 너무 길어서 Plan에 static으로 메서드 하나 만드는건 어떨까요


createdPlans.add(planRepository.save(newPlan));
}

return createdPlans;
}

public List<Plan> findAllByStudyIdAndYearAndMonth(Long studyId, int year, int month) {
return planRepository.findAllByStudyIdAndYearAndMonth(studyId, year, month);
}

@Transactional
public void deleteById(Long planId) {
//TODO: 멤버의 권한 검증 로직
//TODO: 반복옵션 전체 삭제 여부
//TODO: plan이 없을 때 예외처리
planRepository.deleteById(planId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.time.temporal.ChronoUnit;

public enum Frequency implements EndOptionCalculate {
WEEKLY {
WEEKLY(ChronoUnit.WEEKS) {
@Override
public Instant calculateEndDate(Instant startDate, int count) {
ZonedDateTime zdtStart = DateTimeUtils.toZonedDateTime(startDate);
Expand All @@ -22,7 +22,7 @@ public int calculateCount(Instant startDate, Instant endDate) {
return (int) ChronoUnit.WEEKS.between(zdtStart, zdtEnd) + 1;
}
},
MONTHLY {
MONTHLY(ChronoUnit.MONTHS) {
@Override
public Instant calculateEndDate(Instant startDate, int count) {
ZonedDateTime zdtStart = startDate.atZone(ZoneId.systemDefault());
Expand All @@ -38,7 +38,7 @@ public int calculateCount(Instant startDate, Instant endDate) {
return (int) monthsBetween + 1;
}
},
YEARLY {
YEARLY(ChronoUnit.YEARS) {
@Override
public Instant calculateEndDate(Instant startDate, int count) {
ZonedDateTime zdtStart = startDate.atZone(ZoneId.systemDefault());
Expand All @@ -54,4 +54,14 @@ public int calculateCount(Instant startDate, Instant endDate) {
return (int) yearsBetween + 1;
}
};

private final ChronoUnit chronoUnit;

Frequency(ChronoUnit chronoUnit) {
this.chronoUnit = chronoUnit;
}

public ChronoUnit getChronoUnit() {
return chronoUnit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public RecurringOption(Frequency frequency, EndOption endOption,

public void calculate(Instant startDate) {
//TODO:(의견1) endOption에서 계산 로직 실행
if (recurrenceCount != null && endDate != null) {
return;
}

if (endOption == EndOption.COUNT && recurrenceCount != null) {
this.endDate = frequency.calculateEndDate(startDate, recurrenceCount);
} else if (endOption == EndOption.DATE && endDate != null) {
Expand Down