Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
othmanel
MicroservicesLab
Commits
9be2b37b
Commit
9be2b37b
authored
Dec 19, 2018
by
Thomas
Browse files
Final functionality commit
parent
39ccf997
Changes
17
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
9be2b37b
# Microservice Lab
## Vehicle Microservice
Stores vehicle information such as a vehicle's type and its
registration information.
registration.
###### http://localhost:8080
## Driver Microservice
Stores driver information including the vehicle they currently
drive.
###### http://localhost:8081
Stores information of vehicle drivers and what vehicles they drive.
## Vehicle Use Microservice
## Usage Microservice
Stores vehicle location information based on specific points
in time.
\ No newline at end of file
in time.
###### http://localhost:8082
\ No newline at end of file
driver-microservice/src/main/java/edu/microserviceslab/drivermicroservice/controller/DriverController.java
View file @
9be2b37b
package
edu.microserviceslab.drivermicroservice.controller
;
import
edu.microserviceslab.drivermicroservice.common.proxies.VehicleRestProxy
;
import
edu.microserviceslab.drivermicroservice.dto.DriverVehicleChangeRequest
;
import
edu.microserviceslab.drivermicroservice.entity.Driver
;
import
edu.microserviceslab.drivermicroservice.service.interfaces.DriverService
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.apache.commons.lang3.ObjectUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.web.bind.annotation.*
;
import
java.util.List
;
...
...
@@ -23,12 +23,50 @@ public class DriverController {
this
.
vehicleRestProxy
=
vehicleRestProxy
;
}
@ResponseBody
@RequestMapping
(
path
=
"/add"
,
method
=
RequestMethod
.
POST
)
public
Driver
addDriver
(
@RequestBody
Driver
driver
)
{
if
(
driver
==
null
)
{
throw
new
IllegalStateException
(
"Please submit a driver to add."
);
}
if
(
driver
.
getVehicleId
()
==
null
)
{
throw
new
IllegalStateException
(
"The driver needs to have a vehicle assigned to him."
);
}
return
driverService
.
addDriver
(
driver
);
}
@ResponseBody
@RequestMapping
(
path
=
"/changeVehicle"
,
method
=
RequestMethod
.
POST
)
public
Driver
changeVehicle
(
@RequestBody
DriverVehicleChangeRequest
changeRequest
)
{
if
(
changeRequest
==
null
)
{
throw
new
IllegalStateException
(
"Please submit a vehicle change request."
);
}
if
(
changeRequest
.
getDriverId
()
==
null
)
{
throw
new
IllegalStateException
(
"The driver's ID number must be present in a vehicle change request."
);
}
if
(
changeRequest
.
getVehicleId
()
==
null
)
{
throw
new
IllegalStateException
(
"The vehicle's ID number must be present in a vehicle change request."
);
}
if
(
StringUtils
.
isBlank
(
vehicleRestProxy
.
getVehicleLicensePlate
(
changeRequest
.
getVehicleId
())))
{
throw
new
IllegalStateException
(
"The vehicle does not have a valid registration."
);
}
return
driverService
.
changeVehicle
(
changeRequest
);
}
@ResponseBody
@RequestMapping
(
"/list"
)
public
List
<
Driver
>
listAllDrivers
()
{
return
driverService
.
getAllDrivers
();
}
@ResponseBody
@RequestMapping
(
"/{driverId}"
)
public
Driver
getDriverById
(
@PathVariable
(
"driverId"
)
Long
driverId
)
{
return
driverService
.
getDriverById
(
driverId
);
}
@ResponseBody
@RequestMapping
(
"/{driverId}/licensePlate"
)
public
String
getDriverLicensePlate
(
@PathVariable
(
"driverId"
)
Long
driverId
)
{
...
...
driver-microservice/src/main/java/edu/microserviceslab/drivermicroservice/dto/DriverVehicle
DTO
.java
→
driver-microservice/src/main/java/edu/microserviceslab/drivermicroservice/dto/DriverVehicle
ChangeRequest
.java
View file @
9be2b37b
package
edu.microserviceslab.drivermicroservice.dto
;
import
com.fasterxml.jackson.annotation.JsonIgnoreProperties
;
import
org.apache.commons.lang3.builder.EqualsBuilder
;
import
org.apache.commons.lang3.builder.HashCodeBuilder
;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
public
class
DriverVehicleDTO
{
public
class
DriverVehicleChangeRequest
{
private
Long
id
;
private
Long
driverId
;
private
Long
vehicleId
;
private
String
make
;
private
String
model
;
private
Integer
modelYear
;
private
String
licensePlate
;
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
Long
id
)
{
this
.
id
=
id
;
}
public
String
getMake
()
{
return
make
;
}
public
void
setMake
(
String
make
)
{
this
.
make
=
make
;
}
public
String
getModel
()
{
return
model
;
}
public
void
setModel
(
String
model
)
{
this
.
model
=
model
;
}
public
Integer
getModelYear
()
{
return
modelYear
;
public
Long
getDriverId
()
{
return
driverId
;
}
public
void
set
ModelYear
(
Integer
modelYear
)
{
this
.
modelYear
=
modelYear
;
public
void
set
DriverId
(
Long
driverId
)
{
this
.
driverId
=
driverId
;
}
public
Stri
ng
get
LicensePlate
()
{
return
licensePlate
;
public
Lo
ng
get
VehicleId
()
{
return
vehicleId
;
}
public
void
set
LicensePlate
(
String
licensePlate
)
{
this
.
licensePlate
=
licensePlate
;
public
void
set
VehicleId
(
Long
vehicleId
)
{
this
.
vehicleId
=
vehicleId
;
}
@Override
...
...
@@ -64,36 +31,27 @@ public class DriverVehicleDTO {
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
return
false
;
DriverVehicle
DTO
that
=
(
DriverVehicle
DTO
)
o
;
DriverVehicle
ChangeRequest
that
=
(
DriverVehicle
ChangeRequest
)
o
;
return
new
EqualsBuilder
()
.
append
(
id
,
that
.
id
)
.
append
(
make
,
that
.
make
)
.
append
(
model
,
that
.
model
)
.
append
(
modelYear
,
that
.
modelYear
)
.
append
(
licensePlate
,
that
.
licensePlate
)
.
append
(
driverId
,
that
.
driverId
)
.
append
(
vehicleId
,
that
.
vehicleId
)
.
isEquals
();
}
@Override
public
int
hashCode
()
{
return
new
HashCodeBuilder
(
17
,
37
)
.
append
(
id
)
.
append
(
make
)
.
append
(
model
)
.
append
(
modelYear
)
.
append
(
licensePlate
)
.
append
(
driverId
)
.
append
(
vehicleId
)
.
toHashCode
();
}
@Override
public
String
toString
()
{
return
new
ToStringBuilder
(
this
)
.
append
(
"id"
,
id
)
.
append
(
"make"
,
make
)
.
append
(
"model"
,
model
)
.
append
(
"modelYear"
,
modelYear
)
.
append
(
"licensePlate"
,
licensePlate
)
.
append
(
"driverId"
,
driverId
)
.
append
(
"vehicleId"
,
vehicleId
)
.
toString
();
}
}
driver-microservice/src/main/java/edu/microserviceslab/drivermicroservice/entity/Driver.java
View file @
9be2b37b
...
...
@@ -9,7 +9,7 @@ import javax.persistence.Entity;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.Id
;
@Entity
@Entity
(
name
=
"driver"
)
public
class
Driver
{
@Id
...
...
driver-microservice/src/main/java/edu/microserviceslab/drivermicroservice/service/DriverServiceImpl.java
View file @
9be2b37b
package
edu.microserviceslab.drivermicroservice.service
;
import
edu.microserviceslab.drivermicroservice.dto.DriverVehicleChangeRequest
;
import
edu.microserviceslab.drivermicroservice.entity.Driver
;
import
edu.microserviceslab.drivermicroservice.repo.DriverRepo
;
import
edu.microserviceslab.drivermicroservice.service.interfaces.DriverService
;
...
...
@@ -17,6 +18,25 @@ public class DriverServiceImpl implements DriverService {
this
.
driverRepo
=
driverRepo
;
}
@Override
public
Driver
addDriver
(
Driver
driver
)
{
return
driverRepo
.
save
(
driver
);
}
@Override
public
Driver
changeVehicle
(
DriverVehicleChangeRequest
changeRequest
)
{
Optional
<
Driver
>
driverSearchResult
=
driverRepo
.
findById
(
changeRequest
.
getDriverId
());
Driver
toReturn
=
null
;
if
(
driverSearchResult
.
isPresent
())
{
toReturn
=
driverSearchResult
.
get
();
toReturn
.
setVehicleId
(
changeRequest
.
getVehicleId
());
toReturn
=
driverRepo
.
save
(
toReturn
);
}
return
toReturn
;
}
@Override
public
List
<
Driver
>
getAllDrivers
()
{
return
driverRepo
.
findAll
();
...
...
@@ -24,11 +44,11 @@ public class DriverServiceImpl implements DriverService {
@Override
public
Driver
getDriverById
(
Long
driverId
)
{
Optional
<
Driver
>
driver
=
driverRepo
.
findById
(
driverId
);
Optional
<
Driver
>
driver
SearchResult
=
driverRepo
.
findById
(
driverId
);
Driver
toReturn
=
null
;
if
(
driver
.
isPresent
())
{
toReturn
=
driver
.
get
();
if
(
driver
SearchResult
.
isPresent
())
{
toReturn
=
driver
SearchResult
.
get
();
}
return
toReturn
;
...
...
driver-microservice/src/main/java/edu/microserviceslab/drivermicroservice/service/interfaces/DriverService.java
View file @
9be2b37b
package
edu.microserviceslab.drivermicroservice.service.interfaces
;
import
edu.microserviceslab.drivermicroservice.dto.DriverVehicleChangeRequest
;
import
edu.microserviceslab.drivermicroservice.entity.Driver
;
import
java.util.List
;
public
interface
DriverService
{
Driver
addDriver
(
Driver
driver
);
Driver
changeVehicle
(
DriverVehicleChangeRequest
changeRequest
);
List
<
Driver
>
getAllDrivers
();
Driver
getDriverById
(
Long
driverId
);
...
...
driver-microservice/src/main/resources/data.sql
View file @
9be2b37b
INSERT
INTO
D
river
(
id
,
first_name
,
last_name
,
vehicle_id
)
VALUES
INSERT
INTO
d
river
(
id
,
first_name
,
last_name
,
vehicle_id
)
VALUES
(
0
,
'Gigi'
,
'Wentworth (877-CASH-NOW)'
,
0
),
(
1
,
'Fu'
,
'Duh'
,
1
),
(
2
,
'Joe'
,
'Schmo'
,
2
);
driver-microservice/src/main/resources/schema.sql
View file @
9be2b37b
CREATE
TABLE
IF
NOT
EXISTS
D
river
(
CREATE
TABLE
IF
NOT
EXISTS
d
river
(
id
BIGINT
AUTO_INCREMENT
,
first_name
CHAR
(
24
),
last_name
CHAR
(
24
),
...
...
usage-microservice/src/main/java/edu/microserviceslab/usagemicroservice/entity/UsageStatistic.java
View file @
9be2b37b
package
edu.microserviceslab.usagemicroservice.entity
;
import
com.fasterxml.jackson.annotation.JsonFormat
;
import
org.apache.commons.lang3.builder.EqualsBuilder
;
import
org.apache.commons.lang3.builder.HashCodeBuilder
;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
...
...
@@ -7,7 +8,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import
javax.persistence.*
;
import
java.util.Date
;
@Entity
@Entity
(
name
=
"usage_statistic"
)
public
class
UsageStatistic
{
@Id
...
...
@@ -19,6 +20,8 @@ public class UsageStatistic {
* Date and Time variable for when the usage statistic was added
*/
@Column
(
name
=
"created_date"
)
@Temporal
(
TemporalType
.
TIMESTAMP
)
@JsonFormat
(
pattern
=
"yyyy-MM-dd HH:mm:ss"
)
private
Date
createdDate
;
/**
...
...
usage-microservice/src/main/java/edu/microserviceslab/usagemicroservice/repo/UsageStatisticRepo.java
View file @
9be2b37b
...
...
@@ -3,9 +3,17 @@ package edu.microserviceslab.usagemicroservice.repo;
import
edu.microserviceslab.usagemicroservice.entity.UsageStatistic
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.Query
;
import
org.springframework.data.repository.query.Param
;
import
org.springframework.stereotype.Repository
;
import
java.util.List
;
@Repository
public
interface
UsageStatisticRepo
extends
JpaRepository
<
UsageStatistic
,
Long
>
{
@Query
(
"select u from usage_statistic u where u.driverId = :driverId"
)
List
<
UsageStatistic
>
findByDriverId
(
@Param
(
"driverId"
)
Long
driverId
);
@Query
(
"select u from usage_statistic u where u.vehicleId = :vehicleId"
)
List
<
UsageStatistic
>
findByVehicleId
(
@Param
(
"vehicleId"
)
Long
vehicleId
);
}
usage-microservice/src/main/java/edu/microserviceslab/usagemicroservice/service/UsageServiceImpl.java
View file @
9be2b37b
...
...
@@ -25,12 +25,10 @@ public class UsageServiceImpl implements UsageService {
}
public
List
<
UsageStatistic
>
getUsageStatisticsPerDriver
(
Long
driverId
)
{
// TODO finish custom query
return
usageStatisticRepo
.
findAll
();
return
usageStatisticRepo
.
findByDriverId
(
driverId
);
}
public
List
<
UsageStatistic
>
getUsageStatisticsPerVehicle
(
Long
vehicleId
)
{
// TODO finish custom query
return
usageStatisticRepo
.
findAll
();
return
usageStatisticRepo
.
findByVehicleId
(
vehicleId
);
}
}
usage-microservice/src/main/resources/data.sql
View file @
9be2b37b
INSERT
INTO
usage_statistic
(
id
,
created_date
,
speed
,
fuel_level
,
rotations_per_minute
,
latitude
,
longitude
,
driver_id
,
driver_fullname
,
vehicle_id
,
vehicle_license_plate
)
VALUES
(
0
,
'2018-12-19 06:22:00'
,
59
.
6
,
0
.
4
,
9
,
42
.
0284
,
-
93
.
6509
,
0
,
'Gigi Wentworth (877-CASH-NOW)'
,
0
,
'MPASTA'
);
usage-microservice/src/main/resources/schema.sql
View file @
9be2b37b
CREATE
TABLE
IF
NOT
EXISTS
usage_statistic
(
id
BIGINT
AUTO_INCREMENT
,
created_date
TIMESTAMP
,
speed
DOUBLE
,
fuel_level
DOUBLE
,
rotations_per_minute
BIGINT
,
latitude
DOUBLE
,
longitude
DOUBLE
,
driver_id
BIGINT
,
driver_fullname
CHAR
(
50
),
vehicle_id
BIGINT
,
vehicle_license_plate
CHAR
(
10
),
PRIMARY
KEY
(
id
)
);
vehicle-microservice/src/main/java/edu/microserviceslab/vehiclemicroservice/entity/Registration.java
View file @
9be2b37b
...
...
@@ -9,7 +9,7 @@ import javax.persistence.Entity;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.Id
;
@Entity
@Entity
(
name
=
"registration"
)
public
class
Registration
{
@Id
...
...
vehicle-microservice/src/main/java/edu/microserviceslab/vehiclemicroservice/entity/Vehicle.java
View file @
9be2b37b
...
...
@@ -6,7 +6,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import
javax.persistence.*
;
@Entity
@Entity
(
name
=
"vehicle"
)
public
class
Vehicle
{
@Id
...
...
vehicle-microservice/src/main/resources/data.sql
View file @
9be2b37b
INSERT
INTO
V
ehicle
(
id
,
make
,
model
,
model_year
,
registration_id
)
VALUES
INSERT
INTO
v
ehicle
(
id
,
make
,
model
,
model_year
,
registration_id
)
VALUES
(
0
,
'Ford'
,
'Taurus'
,
1996
,
0
),
(
1
,
'Ford'
,
'Fusion'
,
2011
,
1
),
(
2
,
'Ford'
,
'F150'
,
2003
,
2
);
INSERT
INTO
R
egistration
(
id
,
license_plate
,
licensed_to
)
VALUES
INSERT
INTO
r
egistration
(
id
,
license_plate
,
licensed_to
)
VALUES
(
0
,
'MPASTA'
,
'Gianni Pasta Emporium'
),
(
1
,
'HANYOU'
,
'Your Local Chinese'
),
(
2
,
'HAMMER'
,
'Joe Construction Corp'
);
vehicle-microservice/src/main/resources/schema.sql
View file @
9be2b37b
CREATE
TABLE
IF
NOT
EXISTS
V
ehicle
(
CREATE
TABLE
IF
NOT
EXISTS
v
ehicle
(
id
BIGINT
AUTO_INCREMENT
,
make
CHAR
(
20
),
model
CHAR
(
20
),
...
...
@@ -7,9 +7,9 @@ CREATE TABLE IF NOT EXISTS Vehicle (
PRIMARY
KEY
(
id
)
);
CREATE
TABLE
IF
NOT
EXISTS
R
egistration
(
CREATE
TABLE
IF
NOT
EXISTS
r
egistration
(
id
BIGINT
AUTO_INCREMENT
,
license_plate
CHAR
(
1
6
),
license_plate
CHAR
(
1
0
),
licensed_to
CHAR
(
128
),
PRIMARY
KEY
(
id
)
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment