상세 컨텐츠

본문 제목

[WithParents] Medication 엔티티, 레파지토리, MedicationDTO 생성

Project/WithParents

by yooputer 2022. 11. 23. 16:44

본문

이번 포스팅에서는

약복용정보를 기록할 Medication 엔티티와 MedicationRepository, MedicationDTO를 생성해보겠다.


브랜치 생성

약복용알림이라는 브랜치를 생성하고 해당 브랜치에서 개발을 진행하도록 하겠다.

인텔리제이 우측하단


엔티티 생성

entity 패키지 아래에 Medication 클래스를 생성한다.

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Medication {

    @Id
    @Column(name = "medication_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @JoinColumn(name = "member_id")
    private Member member;

    @Column(nullable = false, length = 30)
    private String description;

	@Column(length = 15)
    private String dayOfTheWeekList;

    private LocalTime dosingTime;

}

 

id는 데이터베이스에서 자동으로 생성하고 

member에는 member 객체가 저장되고 

description에는 약에대한 정보가 저장되고

dayOfTheWeek에는 약을 먹는 요일이 저장되고 (요일은 정수로 저장한다.)

dosingTime에는 약 복용 시간이 저장된다.

 

요일을 저장하는 방법에 대해 고민해봤는데 

요일을 정수로 표현하고 (0은 일요일, 1은 월요일, 2는 화요일, ...

공백으로 연결하여 문자열로 저장하려고 한다

예를들어 일요일과 화요일, 목요일에 먹는 약은 dayOfTheWeekList에 "0 2 4"라고 저장한다


레파지토리 생성

repository 패키지아래에 MedicationRepository를 생성하고 다음과 같이 JpaRepository를 상속받는다

public interface MedicationRepository extends JpaRepository<Medication, Long> {
}

 

MedicationDTO 정의

dto 패키지 아래에 medication 패키지를 생성하고 medication 패키지 아래에 MedicationDTO 클래스를 생성한다

@Getter
@Setter
@AllArgsConstructor
public class MedicationDTO {

    private Long id;
    private Long memberId;
    private String description;
    private String dayOfTheWeekList;
    private LocalTime dosingTime;

    public static MedicationDTO entityToDto(Medication e){
        return new MedicationDTO(
                e.getId(),
                e.getMember().getId(),
                e.getDescription(),
                e.getDayOfTheWeekList(),
                e.getDosingTime()
    	);
    }

}

다음포스팅에서는 Medication을 생성하는 api를 구현해보도록 하겠다.

관련글 더보기