0

2 つのエンティティ モデルがAppointmentありreasons、多対多の関係があります。

Appointmentモデル:

@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モデル:

@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モデル:

@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;

}

WaitListモデルを参照するだけ

reasonId の理由のリストと WaitList の時間のリストを使用して、新しいアポイントメントを作成します。

AppointmentPatternは次のとおりです。

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
    
  } 
}

これは、新しい予約を作成し、新しい予約と Reason および 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);
                        }
                )
                ;
    }

機能を分解するために2つの機能を作成しました。 addAppointment(appointmentPattern): アポイントを保持し、新しいアポイントを返します。

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()): Appointment および Reason との関係を与える。

public Uni<Void> storeReasonToAppointment(Appointment appointment, List<ReasonPattern> reasons) {

        /** I didn't success to do ): */
    // code here...
}

誰でも私を助けることができますか?

4

0 に答える 0