상세 컨텐츠

본문 제목

[WithParents] 오늘의 약알림 조회 기능 구현 | getTodayMedicationList api 구현

Project/WithParents

by yooputer 2022. 12. 2. 16:42

본문

이번 포스팅에서는 오늘의 약 알림을 조회하는 api를 구현해보도록 하겠다


MedicationService에서 getTodayMedicationList 메서드 구현하기

MedicationService에 다음과 같이 메서드를 작성한다

public List<MedicationDTO> getTodayMedicationList(
        Long memberId
) {

    Member member = memberRepo.findById(memberId)
            .orElseThrow(() -> new InvalidIdException(memberId, "member"));

    // TODO: 현재 로그인한 유저와 memberId에 해당하는 유저가 같은 family_id를 가지고 있는지 확인

    
    String dayOfTheWeek = String.valueOf(LocalDate.now().getDayOfWeek().getValue());

    return medicationRepo.findAllByMemberId(memberId)
            .stream()
            .filter(e -> { return e.getDayOfTheWeekList().contains(dayOfTheWeek); })
            .map(e -> MedicationDTO.entityToDto(e))
            .collect(Collectors.toList());

}

MedicationController에서 getTodayMedicationList 메서드 구현하기

MedicationController에 다음과 같이 메서드를 작성한다

@GetMapping("/api/medication/getTodayMedicationList")
public List<MedicationDTO> getTodayMedicationList(
        @RequestParam Long memberId
){
    return medicationService.getTodayMedicationList(memberId);
}

요청 보내보기

데이터베이스에 다음과 같이 medication 데이터가 저장되어있다

 

위와 같이 request를 보내면 아래와 같이 금요일에만 해당하는 medication이 반환되는 것을 확인할 수 있다

(포스팅을 작성하는 오늘은 금요일이다..!)

관련글 더보기