Commit 9be2b37b authored by Thomas's avatar Thomas
Browse files

Final functionality commit

parent 39ccf997
# 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
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) {
......
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 setModelYear(Integer modelYear) {
this.modelYear = modelYear;
public void setDriverId(Long driverId) {
this.driverId = driverId;
}
public String getLicensePlate() {
return licensePlate;
public Long getVehicleId() {
return vehicleId;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
public void setVehicleId(Long vehicleId) {
this.vehicleId = vehicleId;
}
@Override
......@@ -64,36 +31,27 @@ public class DriverVehicleDTO {
if (o == null || getClass() != o.getClass()) return false;
DriverVehicleDTO that = (DriverVehicleDTO) o;
DriverVehicleChangeRequest that = (DriverVehicleChangeRequest) 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();
}
}
......@@ -9,7 +9,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Entity(name = "driver")
public class Driver {
@Id
......
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> driverSearchResult = driverRepo.findById(driverId);
Driver toReturn = null;
if (driver.isPresent()) {
toReturn = driver.get();
if (driverSearchResult.isPresent()) {
toReturn = driverSearchResult.get();
}
return toReturn;
......
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);
......
INSERT INTO Driver (id, first_name, last_name, vehicle_id) VALUES
INSERT INTO driver (id, first_name, last_name, vehicle_id) VALUES
(0, 'Gigi', 'Wentworth (877-CASH-NOW)', 0),
(1, 'Fu', 'Duh', 1),
(2, 'Joe', 'Schmo', 2);
CREATE TABLE IF NOT EXISTS Driver (
CREATE TABLE IF NOT EXISTS driver (
id BIGINT AUTO_INCREMENT,
first_name CHAR(24),
last_name CHAR(24),
......
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;
/**
......
......@@ -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);
}
......@@ -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);
}
}
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');
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)
);
......@@ -9,7 +9,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Entity(name = "registration")
public class Registration {
@Id
......
......@@ -6,7 +6,7 @@ import org.apache.commons.lang3.builder.ToStringBuilder;
import javax.persistence.*;
@Entity
@Entity(name = "vehicle")
public class Vehicle {
@Id
......
INSERT INTO Vehicle (id, make, model, model_year, registration_id) VALUES
INSERT INTO vehicle (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 Registration (id, license_plate, licensed_to) VALUES
INSERT INTO registration (id, license_plate, licensed_to) VALUES
(0, 'MPASTA', 'Gianni Pasta Emporium'),
(1, 'HANYOU', 'Your Local Chinese'),
(2, 'HAMMER', 'Joe Construction Corp');
CREATE TABLE IF NOT EXISTS Vehicle (
CREATE TABLE IF NOT EXISTS vehicle (
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 Registration (
CREATE TABLE IF NOT EXISTS registration (
id BIGINT AUTO_INCREMENT,
license_plate CHAR(16),
license_plate CHAR(10),
licensed_to CHAR(128),
PRIMARY KEY (id)
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment