|
1 | 1 | package com.project.back_end.models; |
2 | 2 |
|
| 3 | +import jakarta.persistence.*; |
| 4 | +import jakarta.validation.constraints.Future; |
| 5 | +import jakarta.validation.constraints.NotNull; |
| 6 | + |
| 7 | +import java.time.LocalDate; |
| 8 | +import java.time.LocalDateTime; |
| 9 | +import java.time.LocalTime; |
| 10 | + |
| 11 | +@Entity |
| 12 | +@Table(name = "appointments") |
3 | 13 | public class Appointment { |
4 | 14 |
|
5 | | - // @Entity annotation: |
6 | | -// - Marks the class as a JPA entity, meaning it represents a table in the database. |
7 | | -// - Required for persistence frameworks (e.g., Hibernate) to map the class to a database table. |
8 | | - |
9 | | -// 1. 'id' field: |
10 | | -// - Type: private Long |
11 | | -// - Description: |
12 | | -// - Represents the unique identifier for each appointment. |
13 | | -// - The @Id annotation marks it as the primary key. |
14 | | -// - The @GeneratedValue(strategy = GenerationType.IDENTITY) annotation auto-generates the ID value when a new record is inserted into the database. |
15 | | - |
16 | | -// 2. 'doctor' field: |
17 | | -// - Type: private Doctor |
18 | | -// - Description: |
19 | | -// - Represents the doctor assigned to this appointment. |
20 | | -// - The @ManyToOne annotation defines the relationship, indicating many appointments can be linked to one doctor. |
21 | | -// - The @NotNull annotation ensures that an appointment must be associated with a doctor when created. |
22 | | - |
23 | | -// 3. 'patient' field: |
24 | | -// - Type: private Patient |
25 | | -// - Description: |
26 | | -// - Represents the patient assigned to this appointment. |
27 | | -// - The @ManyToOne annotation defines the relationship, indicating many appointments can be linked to one patient. |
28 | | -// - The @NotNull annotation ensures that an appointment must be associated with a patient when created. |
29 | | - |
30 | | -// 4. 'appointmentTime' field: |
31 | | -// - Type: private LocalDateTime |
32 | | -// - Description: |
33 | | -// - Represents the date and time when the appointment is scheduled to occur. |
34 | | -// - The @Future annotation ensures that the appointment time is always in the future when the appointment is created. |
35 | | -// - It uses LocalDateTime, which includes both the date and time for the appointment. |
36 | | - |
37 | | -// 5. 'status' field: |
38 | | -// - Type: private int |
39 | | -// - Description: |
40 | | -// - Represents the current status of the appointment. It is an integer where: |
41 | | -// - 0 means the appointment is scheduled. |
42 | | -// - 1 means the appointment has been completed. |
43 | | -// - The @NotNull annotation ensures that the status field is not null. |
44 | | - |
45 | | -// 6. 'getEndTime' method: |
46 | | -// - Type: private LocalDateTime |
47 | | -// - Description: |
48 | | -// - This method is a transient field (not persisted in the database). |
49 | | -// - It calculates the end time of the appointment by adding one hour to the start time (appointmentTime). |
50 | | -// - It is used to get an estimated appointment end time for display purposes. |
51 | | - |
52 | | -// 7. 'getAppointmentDate' method: |
53 | | -// - Type: private LocalDate |
54 | | -// - Description: |
55 | | -// - This method extracts only the date part from the appointmentTime field. |
56 | | -// - It returns a LocalDate object representing just the date (without the time) of the scheduled appointment. |
57 | | - |
58 | | -// 8. 'getAppointmentTimeOnly' method: |
59 | | -// - Type: private LocalTime |
60 | | -// - Description: |
61 | | -// - This method extracts only the time part from the appointmentTime field. |
62 | | -// - It returns a LocalTime object representing just the time (without the date) of the scheduled appointment. |
63 | | - |
64 | | -// 9. Constructor(s): |
65 | | -// - A no-argument constructor is implicitly provided by JPA for entity creation. |
66 | | -// - A parameterized constructor can be added as needed to initialize fields. |
67 | | - |
68 | | -// 10. Getters and Setters: |
69 | | -// - Standard getter and setter methods are provided for accessing and modifying the fields: id, doctor, patient, appointmentTime, status, etc. |
| 15 | + @Id |
| 16 | + @GeneratedValue(strategy = GenerationType.IDENTITY) |
| 17 | + private Long id; |
| 18 | + |
| 19 | + @NotNull(message = "Doctor must be assigned") |
| 20 | + @ManyToOne(fetch = FetchType.LAZY) |
| 21 | + @JoinColumn(name = "doctor_id", nullable = false) |
| 22 | + private Doctor doctor; |
| 23 | + |
| 24 | + @NotNull(message = "Patient must be assigned") |
| 25 | + @ManyToOne(fetch = FetchType.LAZY) |
| 26 | + @JoinColumn(name = "patient_id", nullable = false) |
| 27 | + private Patient patient; |
| 28 | + |
| 29 | + @NotNull(message = "Appointment time must be provided") |
| 30 | + @Future(message = "Appointment time must be in the future") |
| 31 | + @Column(name = "appointment_time", nullable = false) |
| 32 | + private LocalDateTime appointmentTime; |
| 33 | + |
| 34 | + @NotNull(message = "Status is required") |
| 35 | + @Column(nullable = false) |
| 36 | + private Integer status; // 0 = scheduled, 1 = completed |
| 37 | + |
| 38 | + // Default constructor required by JPA |
| 39 | + public Appointment() { |
| 40 | + } |
| 41 | + |
| 42 | + // Parameterized constructor |
| 43 | + public Appointment(Doctor doctor, Patient patient, LocalDateTime appointmentTime, Integer status) { |
| 44 | + this.doctor = doctor; |
| 45 | + this.patient = patient; |
| 46 | + this.appointmentTime = appointmentTime; |
| 47 | + this.status = status; |
| 48 | + } |
| 49 | + |
| 50 | + // Transient method: end time is not stored in DB |
| 51 | + @Transient |
| 52 | + public LocalDateTime getEndTime() { |
| 53 | + return appointmentTime.plusHours(1); |
| 54 | + } |
| 55 | + |
| 56 | + @Transient |
| 57 | + public LocalDate getAppointmentDate() { |
| 58 | + return appointmentTime.toLocalDate(); |
| 59 | + } |
| 60 | + |
| 61 | + @Transient |
| 62 | + public LocalTime getAppointmentTimeOnly() { |
| 63 | + return appointmentTime.toLocalTime(); |
| 64 | + } |
70 | 65 |
|
| 66 | + // Getters and Setters |
| 67 | + |
| 68 | + public Long getId() { |
| 69 | + return id; |
| 70 | + } |
| 71 | + |
| 72 | + public void setId(Long id) { |
| 73 | + this.id = id; |
| 74 | + } |
| 75 | + |
| 76 | + public Doctor getDoctor() { |
| 77 | + return doctor; |
| 78 | + } |
| 79 | + |
| 80 | + public void setDoctor(Doctor doctor) { |
| 81 | + this.doctor = doctor; |
| 82 | + } |
| 83 | + |
| 84 | + public Patient getPatient() { |
| 85 | + return patient; |
| 86 | + } |
| 87 | + |
| 88 | + public void setPatient(Patient patient) { |
| 89 | + this.patient = patient; |
| 90 | + } |
| 91 | + |
| 92 | + public LocalDateTime getAppointmentTime() { |
| 93 | + return appointmentTime; |
| 94 | + } |
| 95 | + |
| 96 | + public void setAppointmentTime(LocalDateTime appointmentTime) { |
| 97 | + this.appointmentTime = appointmentTime; |
| 98 | + } |
| 99 | + |
| 100 | + public Integer getStatus() { |
| 101 | + return status; |
| 102 | + } |
| 103 | + |
| 104 | + public void setStatus(Integer status) { |
| 105 | + this.status = status; |
| 106 | + } |
71 | 107 | } |
72 | 108 |
|
| 109 | + |
0 commit comments