Skip to content

Commit

Permalink
feat: Plan 날짜별로 조회 기능 추가(#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
heej-ng committed May 29, 2022
1 parent e1e226d commit d515248
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package planshare.server.planshare.plan.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import planshare.server.planshare.domain.Plan;
import planshare.server.planshare.plan.dto.PlanEx;
import planshare.server.planshare.plan.dto.PlanForm;
import planshare.server.planshare.plan.dto.PlanName;
import planshare.server.planshare.plan.service.PlanService;
import planshare.server.planshare.user.dto.CustomUserDetailsVO;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RequiredArgsConstructor
Expand Down Expand Up @@ -44,6 +46,15 @@ public List<Plan> readPlansByGoal(@AuthenticationPrincipal CustomUserDetailsVO u
return planService.findPlansOfGoal(userDetailsVO, goalId);
}

@ApiOperation(value = "해당 goal에 속하고 months에 해당하는 달의 plan 목록 조회 API", notes = "일별로 정렬한 plan 리스트 반환")
@GetMapping("/users/{memberId}/goals/plans/years/{year}/months/{month}")
public Map<String, List<PlanEx>> readPlanByDate(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
@PathVariable(name = "memberId") Long memberId,
@PathVariable(name = "year") int year,
@PathVariable(name = "month") int month){
return planService.findPlanByDate(userDetailsVO, memberId, year, month);
}

@ApiOperation(value = "특정 plan 이름 수정 API", notes = "수정한 plan 객체 반환")
@PutMapping("/goals/{goalId}/plans/{planId}")
public Plan modifyNameOfPlan(@AuthenticationPrincipal CustomUserDetailsVO userDetailsVO,
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/planshare/server/planshare/plan/dto/PlanEx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package planshare.server.planshare.plan.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Getter;
import lombok.Setter;
import planshare.server.planshare.domain.Goal;

@Getter
@Setter
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PlanEx {

private Long id;

private String name;

private boolean checkStatus;

private Goal goal;

public static PlanEx createPlanEx(Long id, String name, boolean checkStatus, Goal goal){
PlanEx planEx = new PlanEx();
planEx.id = id;
planEx.name = name;
planEx.checkStatus = checkStatus;
planEx.goal = goal;

return planEx;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public List<Plan> findByGoal(Goal goal) {
}

@Override
public List<Plan> findByDate(LocalDateTime date) {
return em.createQuery("select p from Plan p where p.date = :planDate", Plan.class)
.setParameter("planDate", date)
public List<Plan> findByDate(int year, int month) {
return em.createQuery("select p from Plan p where FUNCTION('YEAR', p.date) = :planYear and FUNCTION('MONTH', p.date) = :planMonth", Plan.class)
.setParameter("planYear", year)
.setParameter("planMonth", month)
.getResultList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface PlanRepository {
Plan save(Plan plan);
Optional<Plan> findById(Long id);
List<Plan> findByGoal(Goal goal);
List<Plan> findByDate(LocalDateTime date);
List<Plan> findByDate(int year, int month);
List<Plan> findAll();
int deleteById(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import planshare.server.planshare.domain.Member;
import planshare.server.planshare.domain.Plan;
import planshare.server.planshare.goal.repository.GoalRepository;
import planshare.server.planshare.plan.dto.PlanEx;
import planshare.server.planshare.plan.dto.PlanForm;
import planshare.server.planshare.plan.repository.PlanRepository;
import planshare.server.planshare.repository.MemberRepository;
import planshare.server.planshare.user.dto.CustomUserDetailsVO;

import java.util.List;
import java.util.Optional;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -78,6 +80,40 @@ public List<Plan> findPlansOfGoal(CustomUserDetailsVO userDetailsVO, Long goalId
}
}

/**
* select plan list by date
* 일별로 묶어서 반환
*/
public Map<String, List<PlanEx>> findPlanByDate(CustomUserDetailsVO userDetailsVO, Long memberId, int year, int month){

Optional<Member> user = memberRepository.findByEmail(userDetailsVO.getUsername()); // 요청한 사용자
Optional<Member> member = memberRepository.findById(memberId); // 요청된 멤버
List<Goal> goalList = goalRepository.findByMember(member.get());

Map<String, List<PlanEx>> planDate = new HashMap<>();
List<Plan> planList = new ArrayList<>();

for (Goal goal : goalList){
if(goal.isVisibility() || user.get().getId() == goal.getMember().getId()){
planList.addAll(planRepository.findByGoal(goal));
}
}
for (Plan plan : planList){
LocalDate localDate = plan.getDate().toLocalDate();
if (localDate.getYear() == year && localDate.getMonthValue() == month){
String convertedDate = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

List<PlanEx> planExes = new ArrayList<>();
List<PlanEx> planExList = planDate.getOrDefault(convertedDate, planExes);

PlanEx planEx = PlanEx.createPlanEx(plan.getId(), plan.getName(), plan.isCheckStatus(), plan.getGoal());
planExList.add(planEx);
planDate.put(convertedDate, planExList);
}
}
return planDate;
}

/**
* plan name update
*/
Expand Down

0 comments on commit d515248

Please sign in to comment.