Modeling first attempt for DB
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package de.etecture.ga.config;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.convert.ReadingConverter;
|
||||
import org.springframework.data.convert.WritingConverter;
|
||||
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
@Component
|
||||
public class DataBaseConfiguration extends AbstractJdbcConfiguration {
|
||||
|
||||
|
||||
@Override
|
||||
protected List<?> userConverters() {
|
||||
return Arrays.asList(new DurationToLongConverter(), new LongToDurationConverter());
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
public class DurationToLongConverter implements Converter<Duration, Long> {
|
||||
|
||||
@Override
|
||||
public Long convert(Duration duration) {
|
||||
return duration.toNanos();
|
||||
}
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
public class LongToDurationConverter implements Converter<Long, Duration> {
|
||||
|
||||
@Override
|
||||
public Duration convert(Long duration) {
|
||||
return Duration.of(duration, ChronoUnit.NANOS);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,8 +13,6 @@ public class Appointment {
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private Garage garage;
|
||||
|
||||
private String serviceCode;
|
||||
|
||||
private String serviceName;
|
||||
|
||||
@@ -2,8 +2,10 @@ package de.etecture.ga.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -12,18 +14,35 @@ public class Garage {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
private List<Appointment> appointments;
|
||||
|
||||
|
||||
private Set<GarageServices> garageServices;
|
||||
|
||||
public Garage addAppointment(Appointment appointment) {
|
||||
|
||||
if(this.appointments == null) {
|
||||
|
||||
if (this.appointments == null) {
|
||||
this.appointments = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
this.appointments.add(appointment);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void addService(MDService service) {
|
||||
garageServices.add(createGarageService(service));
|
||||
}
|
||||
|
||||
private GarageServices createGarageService(MDService service) {
|
||||
|
||||
Assert.notNull(service, "Service must not be null");
|
||||
Assert.notNull(service.getId(), "Service id, must not be null");
|
||||
|
||||
GarageServices garageService = new GarageServices();
|
||||
garageService.setMdService(service.getId());
|
||||
|
||||
return garageService;
|
||||
}
|
||||
}
|
||||
|
||||
9
src/main/java/de/etecture/ga/model/GarageServices.java
Normal file
9
src/main/java/de/etecture/ga/model/GarageServices.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package de.etecture.ga.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class GarageServices {
|
||||
|
||||
private Long mdService;
|
||||
}
|
||||
@@ -17,4 +17,5 @@ public class MDService {
|
||||
private String name;
|
||||
|
||||
private Duration duration;
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class GarageImportService {
|
||||
garage.setName(getGarageNameFromFile(file));
|
||||
|
||||
List<CSVData> appointments = loadObjectsFromFile(file);
|
||||
appointments.stream().map(data -> this.fromCSVData(data, garage)).filter(Objects::nonNull)
|
||||
appointments.stream().map(this::fromCSVData).filter(Objects::nonNull)
|
||||
.forEach(garage::addAppointment);
|
||||
|
||||
return Optional.of(garage);
|
||||
@@ -87,13 +87,12 @@ public class GarageImportService {
|
||||
}
|
||||
}
|
||||
|
||||
private Appointment fromCSVData(CSVData data, Garage forGarage) {
|
||||
private Appointment fromCSVData(CSVData data) {
|
||||
|
||||
if (data == null)
|
||||
return null;
|
||||
|
||||
Appointment appointment = new Appointment();
|
||||
appointment.setGarage(forGarage);
|
||||
appointment.setAppointmentTime(data.APP_DATE);
|
||||
appointment.setServiceCode(data.SERVICE);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user