상세 컨텐츠

본문 제목

[WithParents] 약알림 조회 기능 구현, getMedicationList api 구현

Project/WithParents

by yooputer 2022. 12. 2. 16:28

본문

이번 포스팅에서는 Medication을 조회하는 api를 구현해보도록 하겠다


 

MedicationRepository에서 findAllByMemberId 메서드 명시

MedicationRepository에 다음과 같이 메서드를 명시한다


MedicationService에서 getMedicationList 메서드 구현

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

public List<MedicationDTO> getMedicationList(Long memberId) {

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

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

    return medicationRepo.findAllByMemberId(memberId)
            .stream()
            .map(e -> MedicationDTO.entityToDto(e))
            .collect(Collectors.toList());

}

MedicationController에서 getMedicationList 메서드 구현

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

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

요청 보내보기

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

 

위와 같이 request를 보내면 아래와 같이 response가 온다.

 

관련글 더보기