Skip to content

Commit

Permalink
Feat(*): 문제 제출 후 progress 업데이트 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
InHyeok-J committed Sep 6, 2024
1 parent 83713c9 commit 95de33f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.jabiseo.learning.dto.ProblemResultRequest;
import com.jabiseo.member.domain.Member;
import com.jabiseo.member.domain.MemberRepository;
import com.jabiseo.plan.domain.PlanProgressService;
import com.jabiseo.problem.domain.Problem;
import com.jabiseo.problem.domain.ProblemRepository;
import com.jabiseo.problem.exception.ProblemBusinessException;
Expand All @@ -31,6 +32,7 @@ public class CreateLearningUseCase {
private final ProblemRepository problemRepository;
private final LearningRepository learningRepository;
private final ProblemSolvingRepository problemSolvingRepository;
private final PlanProgressService planProgressService;

public Long execute(Long memberId, CreateLearningRequest request) {

Expand All @@ -53,7 +55,7 @@ public Long execute(Long memberId, CreateLearningRequest request) {
//ProblemSolving 생성 및 저장
List<ProblemSolving> problemSolvings = createProblemSolvings(request, solvedProblems, member, learning);
problemSolvingRepository.saveAll(problemSolvings);

planProgressService.updateProgress(learning, problemSolvings.size());
return learning.getId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jabiseo.learning.dto;

import com.jabiseo.learning.domain.Learning;
import com.jabiseo.learning.domain.LearningMode;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -17,4 +18,8 @@ public class LearningWithSolvingCountQueryDto {
private LocalDateTime createdAt;
private Long solvingCount;


public static LearningWithSolvingCountQueryDto from(Learning learning, long count){
return new LearningWithSolvingCountQueryDto(learning.getMode(), learning.getLearningTime(), learning.getCreatedAt(), count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface PlanProgressRepository extends JpaRepository<PlanProgress, Long> {

List<PlanProgress> findAllByPlanAndProgressDateBetweenOrderByProgressDate(Plan plan, LocalDate start, LocalDate end);

List<PlanProgress> findAllByProgressDateBetweenAndGoalType(LocalDate start, LocalDate end, GoalType goalType);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jabiseo.plan.domain;


import com.jabiseo.learning.domain.Learning;
import com.jabiseo.learning.domain.LearningMode;
import com.jabiseo.learning.domain.LearningRepository;
import com.jabiseo.learning.dto.LearningWithSolvingCountQueryDto;
Expand All @@ -9,8 +10,10 @@
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -19,6 +22,7 @@
public class PlanProgressService {

private final WeeklyDefineStrategy weeklyDefineStrategy;
private final PlanRepository planRepository;
private final PlanProgressRepository planProgressRepository;
private final LearningRepository learningRepository;

Expand All @@ -30,21 +34,66 @@ public List<PlanProgress> findByYearMonth(Plan plan, int year, int month) {
return planProgressRepository.findAllByPlanAndProgressDateBetweenOrderByProgressDate(plan, startQueryDate, endQueryDate);
}

public void updateProgress(Learning learning, long count) {
Member member = learning.getMember();
Optional<Plan> plans = planRepository.findFirstByCertificateAndMember(member.getCurrentCertificate(), member);

if (plans.isEmpty()) {
return;
}

List<PlanItem> planItems = plans.get().getPlanItems();
List<PlanProgress> planProgress = getPlanProgress(planItems, LearningWithSolvingCountQueryDto.from(learning, count));
planProgressRepository.saveAll(planProgress);
}


public void createCurrentPlanProgress(Member member, List<PlanItem> planItems) {
WeekPeriod currentWeekPeriod = weeklyDefineStrategy.getCurrentWeekPeriod(LocalDate.now());

List<PlanProgress> daily = planItems.stream().filter(p -> p.getGoalType().equals(GoalType.DAILY))
.map(planItem -> planItem.toPlanProgress(LocalDate.now()))
.toList();

List<PlanProgress> weekly = planItems.stream().filter(p -> p.getGoalType().equals(GoalType.WEEKLY))
.map(planItem -> planItem.toPlanProgress(currentWeekPeriod.getEnd()))
.toList();
List<PlanProgress> daily = itemToDailyPlanProgress(planItems);

List<PlanProgress> weekly = itemToWeeklyPlanProgress(planItems, currentWeekPeriod.getEnd());

learningCalculateAndSave(member, daily, LocalDate.now(), LocalDate.now());
learningCalculateAndSave(member, weekly, currentWeekPeriod.getStart(), currentWeekPeriod.getEnd());
}

private List<PlanProgress> getPlanProgress(List<PlanItem> planItems, LearningWithSolvingCountQueryDto dto) {
WeekPeriod currentWeekPeriod = weeklyDefineStrategy.getCurrentWeekPeriod(LocalDate.now());

List<PlanProgress> dailyProgress = planProgressRepository.findAllByProgressDateBetweenAndGoalType(LocalDate.now(), LocalDate.now(), GoalType.DAILY);
if (dailyProgress.isEmpty()) {
dailyProgress = itemToDailyPlanProgress(planItems);
}

List<PlanProgress> weeklyProgress = planProgressRepository.findAllByProgressDateBetweenAndGoalType(currentWeekPeriod.getStart(), currentWeekPeriod.getEnd(), GoalType.WEEKLY);
if (weeklyProgress.isEmpty()) {
weeklyProgress = itemToWeeklyPlanProgress(planItems, currentWeekPeriod.getEnd());
}

List<PlanProgress> result = new ArrayList<>();
result.addAll(calculateProgress(dailyProgress, List.of(dto)));
result.addAll(calculateProgress(weeklyProgress, List.of(dto)));
return result;
}


private List<PlanProgress> itemToDailyPlanProgress(List<PlanItem> planItems) {
return planItems.stream()
.filter(p -> p.getGoalType().equals(GoalType.DAILY))
.map(planItem -> planItem.toPlanProgress(LocalDate.now()))
.toList();
}

private List<PlanProgress> itemToWeeklyPlanProgress(List<PlanItem> planItems, LocalDate date) {
return planItems.stream()
.filter(p -> p.getGoalType().equals(GoalType.WEEKLY))
.map(planItem -> planItem.toPlanProgress(date))
.toList();
}

private void learningCalculateAndSave(Member member, List<PlanProgress> progress, LocalDate start, LocalDate end) {
// 없으면 진행하지 않는다.
if (progress.isEmpty()) {
Expand Down

0 comments on commit 95de33f

Please sign in to comment.