0

我有 2 个实体模型Appointmentreasons多对多关系。

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

这是我进行新约会的端点,然后给出新约会与原因和 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