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);
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ spring.datasource.url=jdbc:h2:mem:etecture
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.datasource.platform=h2
|
||||
|
||||
# init DB by script
|
||||
spring.sql.init.mode=always
|
||||
41
src/main/resources/schema.sql
Normal file
41
src/main/resources/schema.sql
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
CREATE TABLE GARAGE (
|
||||
ID BIGINT NOT NULL AUTO_INCREMENT UNIQUE,
|
||||
NAME VARCHAR(200) DEFAULT NULL UNIQUE,
|
||||
|
||||
PRIMARY KEY (ID)
|
||||
);
|
||||
|
||||
|
||||
CREATE TABLE APPOINTMENT (
|
||||
ID BIGINT NOT NULL AUTO_INCREMENT UNIQUE,
|
||||
GARAGE BIGINT NOT NULL,
|
||||
SERVICE_CODE VARCHAR(5) DEFAULT NULL,
|
||||
SERVICE_NAME VARCHAR(50) DEFAULT NULL,
|
||||
APPOINTMENT_TIME DATE DEFAULT NULL,
|
||||
DURATION BIGINT NOT NULL DEFAULT 0,
|
||||
|
||||
CONSTRAINT GARAGE_APPOINTMENT_IDX UNIQUE (GARAGE,APPOINTMENT_TIME),
|
||||
|
||||
PRIMARY KEY (ID),
|
||||
FOREIGN KEY (GARAGE) REFERENCES GARAGE(ID)
|
||||
);
|
||||
|
||||
CREATE TABLE MD_SERVICE (
|
||||
ID BIGINT NOT NULL AUTO_INCREMENT UNIQUE,
|
||||
CODE VARCHAR(5) NOT NULL UNIQUE,
|
||||
NAME VARCHAR(50) DEFAULT NULL,
|
||||
DURATION INT NOT NULL,
|
||||
|
||||
PRIMARY KEY (ID)
|
||||
);
|
||||
|
||||
CREATE TABLE GARAGE_SERVICES_MAP (
|
||||
GARAGE BIGINT NOT NULL,
|
||||
MD_SERVICE BIGINT NOT NULL,
|
||||
|
||||
CONSTRAINT GARAGE_SERVICE_IDX UNIQUE (GARAGE,MD_SERVICE),
|
||||
|
||||
FOREIGN KEY (GARAGE) REFERENCES GARAGE(ID),
|
||||
FOREIGN KEY (MD_SERVICE) REFERENCES MD_SERVICE(ID)
|
||||
);
|
||||
Reference in New Issue
Block a user