refactoring

This commit is contained in:
Matthias Engelien
2024-09-15 12:41:39 +02:00
parent c311564ecc
commit cbd6d373bb
5 changed files with 62 additions and 19 deletions

View File

@@ -71,7 +71,15 @@ public class GarageApiController implements WerkstattApi {
@Override
public ResponseEntity<List<Termin>> getTerminvorschlaege(String werkstattId, @NotNull @Valid String leistungsId,
@Valid String von, @Valid String bis) {
// TODO Auto-generated method stub
Assert.isTrue(NumberUtils.isParsable(werkstattId), "werkstattId ungültig");
long garageId = Long.parseLong(werkstattId);
Optional<Long> serviceId = NumberUtils.isParsable(leistungsId) ? Optional.of(Long.parseLong(leistungsId))
: Optional.empty();
Optional<Date> appointmentsFrom = Optional.ofNullable(parseDate(von));
Optional<Date> appointmentsTill = Optional.ofNullable(parseDate(bis));
return WerkstattApi.super.getTerminvorschlaege(werkstattId, leistungsId, von, bis);
}

View File

@@ -11,13 +11,16 @@ import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.data.relational.core.mapping.Column;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@Data
@Accessors(fluent = true, chain = true)
public class Appointment {
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Appointment implements Comparable<Appointment>{
@Id
@EqualsAndHashCode.Include
private Long id;
@Column("GARAGE_ID")
@@ -26,12 +29,15 @@ public class Appointment {
@Column("SERVICE_ID")
private AggregateReference<MDService, Long> serviceId;
@EqualsAndHashCode.Include
private String serviceCode;
private String serviceName;
@EqualsAndHashCode.Include
private Date appointmentTime;
@EqualsAndHashCode.Include
private Integer slot = 1;
private Duration duration = Duration.ZERO;
@@ -45,4 +51,12 @@ public class Appointment {
public LocalDateTime appointmentEnd() {
return this.appointmentStart().plus(this.duration());
}
@Override
public int compareTo(Appointment o) {
if(o == null || o.appointmentTime() == null) return 1;
return this.appointmentTime().compareTo(o.appointmentTime());
}
}

View File

@@ -2,6 +2,7 @@ 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;
@@ -40,6 +41,10 @@ public class Garage {
}
return this;
}
public List<Appointment> appointmentsSorted() {
return this.appointments.stream().sorted().toList();
}
public Garage addService(MDService service) {
garageServices.add(createGarageService(service, null));

View File

@@ -62,6 +62,7 @@ public class AppointmentService {
public Optional<Appointment> createAppointment(long garageId, long serviceId, Date from, Date till) {
// validate input
Optional<Garage> garage = garageService.getGarage(garageId);
if (garage.isEmpty())
throw new IllegalArgumentException("GarageId not valid");
@@ -73,8 +74,25 @@ public class AppointmentService {
if (service.isEmpty() || garageService.isEmpty())
throw new IllegalArgumentException("serviceId not valid");
// check appointment times
LocalDateTime validAppointmentTime = getValidAppointmentTime(from, till, garage);
// create appointment
if (validAppointmentTime != null) {
Appointment appointment = createAppointmentObj(garage, service, garageService, validAppointmentTime);
return Optional.of(repository.save(appointment));
} else {
return Optional.empty();
}
}
private LocalDateTime getValidAppointmentTime(Date from, Date till, Optional<Garage> garage) {
LocalDateTime fromDateTime = Instant.ofEpochMilli(from.getTime()).atZone(ZoneId.systemDefault())
.toLocalDateTime();
fromDateTime = roundUpToQuarter(fromDateTime);
LocalDateTime tillDateTime = Instant.ofEpochMilli(till.getTime()).atZone(ZoneId.systemDefault())
.toLocalDateTime();
@@ -87,17 +105,16 @@ public class AppointmentService {
fromDateTime = fromDateTime.plusMinutes(15);
}
if (validAppointmentTime != null) {
Appointment appointment = new Appointment().garageId(AggregateReference.to(garage.get().id()))
.appointmentTime(Date.from(validAppointmentTime.atZone(ZoneId.systemDefault()).toInstant()))
.serviceId(AggregateReference.to(service.get().id())).serviceCode(service.get().code())
.serviceName(service.get().name()).duration(garageService.get().duration());
return validAppointmentTime;
}
return Optional.of(repository.save(appointment));
private LocalDateTime roundUpToQuarter(LocalDateTime datetime) {
} else {
return Optional.empty();
}
if (datetime.getMinute() % 15 == 0)
return datetime;
int minutesToAdd = 15 - (datetime.getMinute() % 15);
return datetime.plusMinutes(minutesToAdd).truncatedTo(ChronoUnit.MINUTES);
}
private boolean isSlotAvailable(Garage garage, LocalDateTime time) {
@@ -110,12 +127,12 @@ public class AppointmentService {
return appointments < garage.maxAppointments();
}
private LocalDateTime roundUpToQuarter(LocalDateTime datetime) {
if (datetime.getMinute() % 15 == 0)
return datetime;
int minutesToAdd = 15 - (datetime.getMinute() % 15);
return datetime.plusMinutes(minutesToAdd).truncatedTo(ChronoUnit.MINUTES);
private Appointment createAppointmentObj(Optional<Garage> garage, Optional<MDService> service,
Optional<GarageServices> garageService, LocalDateTime validAppointmentTime) {
Appointment appointment = new Appointment().garageId(AggregateReference.to(garage.get().id()))
.appointmentTime(Date.from(validAppointmentTime.atZone(ZoneId.systemDefault()).toInstant()))
.serviceId(AggregateReference.to(service.get().id())).serviceCode(service.get().code())
.serviceName(service.get().name()).duration(garageService.get().duration());
return appointment;
}
}

View File

@@ -19,7 +19,6 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.etecture.ga.model.Appointment;
import de.etecture.ga.model.Garage;