package de.etecture.ga.model; import java.time.Duration; import java.util.HashSet; import java.util.List; import java.util.Set; import org.springframework.data.annotation.Id; import org.springframework.data.jdbc.core.mapping.AggregateReference; import org.springframework.data.relational.core.mapping.MappedCollection; import org.springframework.util.Assert; import lombok.Data; import lombok.experimental.Accessors; @Data @Accessors(fluent = true, chain = true) public class Garage { @Id private Long id; private String code; private String name; private Integer maxAppointments = 1; @MappedCollection(idColumn = "GARAGE_ID") private Set appointments = new HashSet<>(); @MappedCollection(idColumn = "GARAGE_ID") private Set garageServices = new HashSet<>(); public Garage addAppointment(Appointment appointment) { boolean added = this.appointments.add(appointment); if(!added) { appointment.slot(appointment.slot() + 1); this.addAppointment(appointment); } return this; } public List appointmentsSorted() { return this.appointments.stream().sorted().toList(); } public Garage addService(MDService service) { garageServices.add(createGarageService(service, null)); return this; } public void addService(MDService service, Duration duration) { garageServices.add(createGarageService(service, duration)); } private GarageServices createGarageService(MDService service, Duration duration) { Assert.notNull(service, "Service must not be null"); Assert.notNull(service.id(), "Service id, must not be null"); duration = duration == null ? service.duration() : duration; return new GarageServices().garageId(AggregateReference.to(this.id())) .serviceId(AggregateReference.to(service.id())).duration(duration); } }