I have 2 entity models Appointment
and reasons
with many to many relationship.
Appointment
model:
@Entity
public class Appointment extends PanacheEntityBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(
name = "appointment_reason",
joinColumns = {@JoinColumn(name = "appointment_id")},
inverseJoinColumns = {@JoinColumn(name = "reason_id")})
public Set<Reason> reasons = new HashSet<>();
}
Reason
model:
@Entity
public class Reason extends PanacheEntityBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public String title;
@ManyToMany(
cascade = CascadeType.ALL,
mappedBy = "reasons"
)
public Set<Appointment> appointments = new HashSet<>();
}
WaitList
model:
@Entity
public class WaitList extends PanacheEntityBase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
public LocalDateTime startTime;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "appointment_id")
public Appointment appointment;
}
Just refer WaitList
model
I am going to make new appointment with the list of reasons with reasonId and the list of WaitList times.
AppointmentPattern
is follow:
mutation {
createAppointment(appointmentPattern: {
waitList: [
{startTime: "2023-02-04T08:00"},
{startTime: "2023-02-05T08:50"},
{startTime: "2023-02-06T08:00"},
{startTime: "2023-02-07T08:50"},
{startTime: "2023-02-08T09:40"}
],
reasonList: [
{id: 1},
{id: 2},
{id: 3},
{id: 4},
]
}) {
id,
startTime
}
}
This is my endpoint to make new appointment and then to give the relationship with between new made appointment and Reason and WaitList.
@Mutation
@Description("Create new appointment")
public Uni<List<TimeSlot>> createAppointment(AppointmentPattern appointmentPattern) {
return Appointment.addAppointment(appointmentPattern)
.call(appointment -> Mutiny.fetch(appointment.reasons))
.call(appointment -> storeReasonToAppointment(appointment, appointmentPattern.getReasonList()))
.flatMap(appointment ->
{
List<TimeSlot> timeSlots = TimeSlot.fromList(appointmentPattern.getWaitList(), appointment);
return Panache.withTransaction(() -> PanacheEntityBase.persist(timeSlots))
.replaceWith(timeSlots);
}
)
;
}
I made 2 functions to break the functionality down.
addAppointment(appointmentPattern)
: persist appointment and return new appointment.
public static Uni<Appointment> addAppointment(AppointmentPattern appointmentPattern) {
Appointment appointment = from(appointmentPattern);
return Panache
.withTransaction(appointment::persist)
.replaceWith(appointment)
.ifNoItem()
.after(Duration.ofMillis(10000))
.fail()
.onFailure()
.transform(t -> new IllegalStateException(t));
}
storeReasonToAppointment(appointment, appointmentPattern.getReasonList())
: to give relationship with Appointment and Reason.
public Uni<Void> storeReasonToAppointment(Appointment appointment, List<ReasonPattern> reasons) {
/** I didn't success to do ): */
// code here...
}
Can anyone help me?