이번 포스팅에서는
약복용정보를 기록할 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> {
}
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를 구현해보도록 하겠다.
[WithParents] 약알림 수정 기능 구현, modifyMedication api 구현 (0) | 2022.12.02 |
---|---|
[WithParents] 약알림 추가 기능, createMedication api 구현 (0) | 2022.11.23 |
[WithParents] 예외처리 | custom exception | RestControllerAdvice | ExceptionHandler (0) | 2022.11.16 |
[WithParents] UserController 생성 | member, family 생성 api 구현 (0) | 2022.11.09 |
[WithParents] Family, Member DTO 정의 | UserService 생성 (0) | 2022.11.09 |