diff --git a/car-service/Dockerfile b/car-service/Dockerfile new file mode 100644 index 0000000..6f92c37 --- /dev/null +++ b/car-service/Dockerfile @@ -0,0 +1,5 @@ +FROM adoptopenjdk:11-jre-hotspot-focal +MAINTAINER Anirban Das +RUN mkdip ~p /opt/sharenow-coding-challenge +COPY target/vehicle-car.jar /opt/sharenow-coding-challenge/car-app.jar +CMD ["java","-jar","/opt/sharenow-coding-challenge/car-app.jar"] \ No newline at end of file diff --git a/vehicle-service/pom.xml b/car-service/pom.xml similarity index 88% rename from vehicle-service/pom.xml rename to car-service/pom.xml index 966bf97..ae2ca07 100644 --- a/vehicle-service/pom.xml +++ b/car-service/pom.xml @@ -9,17 +9,17 @@ 1.0-SNAPSHOT - vehicle-service + car-service 1.0.0-SNAPSHOT jar - vehicle-service + car-service 11 - vehicle-app + car-app com.spotify @@ -54,10 +54,6 @@ org.springframework.boot spring-boot-starter-data-redis - - org.springframework.cloud - spring-cloud-starter-openfeign - org.springframework.cloud spring-cloud-starter-sleuth diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/VehicleServiceApplication.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/CarServiceApplication.java similarity index 62% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/VehicleServiceApplication.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/CarServiceApplication.java index 46e4339..ae302f6 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/VehicleServiceApplication.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/CarServiceApplication.java @@ -1,4 +1,4 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle; +package com.teenthofabud.codingchallenge.sharenow.car; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -6,10 +6,10 @@ @SpringBootApplication @EnableEurekaClient -public class VehicleServiceApplication { +public class CarServiceApplication { public static void main(String[] args) { - SpringApplication.run(VehicleServiceApplication.class, args); + SpringApplication.run(CarServiceApplication.class, args); } } diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/configuration/VehicleServiceConfiguration.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/configuration/CarServiceConfiguration.java similarity index 88% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/configuration/VehicleServiceConfiguration.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/configuration/CarServiceConfiguration.java index ffc02ac..3e70931 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/configuration/VehicleServiceConfiguration.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/configuration/CarServiceConfiguration.java @@ -1,4 +1,4 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.configuration; +package com.teenthofabud.codingchallenge.sharenow.car.configuration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -11,7 +11,7 @@ @Configuration @EnableRedisRepositories -public class VehicleServiceConfiguration { +public class CarServiceConfiguration { @Bean public LocaleResolver localeResolver() { diff --git a/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/controller/CarSearchController.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/controller/CarSearchController.java new file mode 100644 index 0000000..d8c72f3 --- /dev/null +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/controller/CarSearchController.java @@ -0,0 +1,45 @@ +package com.teenthofabud.codingchallenge.sharenow.car.controller; + +import com.teenthofabud.codingchallenge.sharenow.car.model.error.CarServiceException; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarDetailsVO; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarVO; +import com.teenthofabud.codingchallenge.sharenow.car.service.CarService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("search") +public class CarSearchController { + + private static final Logger LOGGER = LoggerFactory.getLogger(CarSearchController.class); + + @Autowired + private CarService service; + + @GetMapping("vin/{vin}") + public ResponseEntity getVehicleByVin(@PathVariable String vin) throws CarServiceException { + LOGGER.info("Requesting vehicle with vin: {}", vin); + CarDetailsVO vo = this.service.retrieveVehicleDetailsByVin(vin); + ResponseEntity response = ResponseEntity.ok(vo); + LOGGER.info("Responding with vehicle of vin: {}", vin); + return response; + } + + @GetMapping + public ResponseEntity getAllVehicles() throws CarServiceException { + LOGGER.info("Requesting all vehicles"); + List voList = this.service.retrieveAllVehicles(); + ResponseEntity> response = ResponseEntity.ok(voList); + LOGGER.info("Responding with all available vehicles"); + return response; + } + +} diff --git a/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/CarEntity2DetailedVOConverter.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/CarEntity2DetailedVOConverter.java new file mode 100644 index 0000000..a95f1b3 --- /dev/null +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/CarEntity2DetailedVOConverter.java @@ -0,0 +1,12 @@ +package com.teenthofabud.codingchallenge.sharenow.car.converter; + +import com.teenthofabud.codingchallenge.sharenow.car.model.entity.CarEntity; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarDetailsVO; +import org.springframework.core.convert.converter.Converter; + +@FunctionalInterface +public interface CarEntity2DetailedVOConverter extends Converter { + + public CarDetailsVO convert(CarEntity entity); + +} diff --git a/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/CarEntity2VOConverter.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/CarEntity2VOConverter.java new file mode 100644 index 0000000..ca79840 --- /dev/null +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/CarEntity2VOConverter.java @@ -0,0 +1,12 @@ +package com.teenthofabud.codingchallenge.sharenow.car.converter; + +import com.teenthofabud.codingchallenge.sharenow.car.model.entity.CarEntity; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarVO; +import org.springframework.core.convert.converter.Converter; + +@FunctionalInterface +public interface CarEntity2VOConverter extends Converter { + + public CarVO convert(CarEntity entity); + +} diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/PositionEntity2VOConverter.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/PositionEntity2VOConverter.java similarity index 50% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/PositionEntity2VOConverter.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/PositionEntity2VOConverter.java index 381b0f3..78c4977 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/PositionEntity2VOConverter.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/converter/PositionEntity2VOConverter.java @@ -1,7 +1,7 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.converter; +package com.teenthofabud.codingchallenge.sharenow.car.converter; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.entity.PositionEntity; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.PositionVO; +import com.teenthofabud.codingchallenge.sharenow.car.model.entity.PositionEntity; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.PositionVO; import org.springframework.core.convert.converter.Converter; @FunctionalInterface diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/filter/RestErrorHandler.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/filter/RestErrorHandler.java similarity index 77% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/filter/RestErrorHandler.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/filter/RestErrorHandler.java index a98fee2..04ab798 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/filter/RestErrorHandler.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/filter/RestErrorHandler.java @@ -1,7 +1,7 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.filter; +package com.teenthofabud.codingchallenge.sharenow.car.filter; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.error.VehicleServiceException; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.ErrorVO; +import com.teenthofabud.codingchallenge.sharenow.car.model.error.CarServiceException; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.ErrorVO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -20,8 +20,8 @@ public class RestErrorHandler { @Autowired private MessageSource messageSource; - @ExceptionHandler(VehicleServiceException.class) - public ResponseEntity handleVehicleServiceException(VehicleServiceException vsex) { + @ExceptionHandler(CarServiceException.class) + public ResponseEntity handleVehicleServiceException(CarServiceException vsex) { LOGGER.error("Error encountered: {}", vsex); ErrorVO vo = new ErrorVO(); String msg = messageSource.getMessage(vsex.getError().getErrorCode(), null, Locale.US); diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/entity/VehicleEntity.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/entity/CarEntity.java similarity index 83% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/entity/VehicleEntity.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/entity/CarEntity.java index 1df2f22..00a517d 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/entity/VehicleEntity.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/entity/CarEntity.java @@ -1,4 +1,4 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.entity; +package com.teenthofabud.codingchallenge.sharenow.car.model.entity; import lombok.Getter; import lombok.NoArgsConstructor; @@ -18,7 +18,7 @@ @RedisHash("Vehicle") @TypeAlias("Vehicle") @NoArgsConstructor -public class VehicleEntity implements Serializable { +public class CarEntity implements Serializable { @Id private int id; @@ -35,7 +35,7 @@ public String getCacheKey() { return "Vehicle:" + id; } - public VehicleEntity(String vin) { + public CarEntity(String vin) { this.vin = vin; } diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/entity/PositionEntity.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/entity/PositionEntity.java similarity index 80% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/entity/PositionEntity.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/entity/PositionEntity.java index 3f30267..95d92f3 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/entity/PositionEntity.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/entity/PositionEntity.java @@ -1,4 +1,4 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.entity; +package com.teenthofabud.codingchallenge.sharenow.car.model.entity; import lombok.Getter; import lombok.Setter; diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/error/VehicleErrorCode.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/error/CarErrorCode.java similarity index 50% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/error/VehicleErrorCode.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/error/CarErrorCode.java index 32e9c18..1e2fd14 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/error/VehicleErrorCode.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/error/CarErrorCode.java @@ -1,20 +1,20 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.error; +package com.teenthofabud.codingchallenge.sharenow.car.model.error; import lombok.Getter; import lombok.ToString; @Getter @ToString -public enum VehicleErrorCode { +public enum CarErrorCode { - NOT_FOUND("SNCC-VS-001", 404), - INVALID_PARAMETER("SNCC-VS-002", 400); + NOT_FOUND("SNCC-CS-001", 404), + INVALID_PARAMETER("SNCC-CS-002", 400); @ToString.Include private int statusCode; @ToString.Include private String errorCode; - private VehicleErrorCode(String errorCode, int statusCode) { + private CarErrorCode(String errorCode, int statusCode) { this.errorCode = errorCode; this.statusCode = statusCode; } diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/error/VehicleServiceException.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/error/CarServiceException.java similarity index 51% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/error/VehicleServiceException.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/error/CarServiceException.java index 808c832..3f91ca5 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/error/VehicleServiceException.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/error/CarServiceException.java @@ -1,28 +1,28 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.error; +package com.teenthofabud.codingchallenge.sharenow.car.model.error; import lombok.Getter; import lombok.Setter; @Getter @Setter -public class VehicleServiceException extends Exception { +public class CarServiceException extends Exception { - private VehicleErrorCode error; + private CarErrorCode error; private String message; private Object[] params; - public VehicleServiceException(String message) { + public CarServiceException(String message) { super(message); this.message = message; } - public VehicleServiceException(String message, Object[] params) { + public CarServiceException(String message, Object[] params) { super(message); this.message = message; this.params = params; } - public VehicleServiceException(String message, VehicleErrorCode error, Object[] params) { + public CarServiceException(String message, CarErrorCode error, Object[] params) { super(message); this.message = message; this.error = error; diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/VehicleDetailsVO.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/CarDetailsVO.java similarity index 71% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/VehicleDetailsVO.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/CarDetailsVO.java index 774e57f..e6e27b1 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/VehicleDetailsVO.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/CarDetailsVO.java @@ -1,11 +1,11 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo; +package com.teenthofabud.codingchallenge.sharenow.car.model.vo; import lombok.Getter; import lombok.Setter; @Getter @Setter -public class VehicleDetailsVO { +public class CarDetailsVO { private int id; private int locationId; diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/VehicleVO.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/CarVO.java similarity index 64% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/VehicleVO.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/CarVO.java index 3c827a4..a446815 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/VehicleVO.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/CarVO.java @@ -1,11 +1,11 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo; +package com.teenthofabud.codingchallenge.sharenow.car.model.vo; import lombok.Getter; import lombok.Setter; @Getter @Setter -public class VehicleVO { +public class CarVO { private int id; private String vin; diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/ErrorVO.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/ErrorVO.java similarity index 79% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/ErrorVO.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/ErrorVO.java index 1ed4713..2d5df47 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/ErrorVO.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/ErrorVO.java @@ -1,4 +1,4 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo; +package com.teenthofabud.codingchallenge.sharenow.car.model.vo; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/PositionVO.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/PositionVO.java similarity index 68% rename from vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/PositionVO.java rename to car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/PositionVO.java index 25e1adc..963c4ef 100644 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/model/vo/PositionVO.java +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/model/vo/PositionVO.java @@ -1,4 +1,4 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo; +package com.teenthofabud.codingchallenge.sharenow.car.model.vo; import lombok.Getter; import lombok.Setter; diff --git a/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/repository/CarRepository.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/repository/CarRepository.java new file mode 100644 index 0000000..4a7f8cc --- /dev/null +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/repository/CarRepository.java @@ -0,0 +1,14 @@ +package com.teenthofabud.codingchallenge.sharenow.car.repository; + +import com.teenthofabud.codingchallenge.sharenow.car.model.entity.CarEntity; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface CarRepository extends CrudRepository { + + public List findByVin(String vin); + +} diff --git a/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/service/CarService.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/service/CarService.java new file mode 100644 index 0000000..7fcbea9 --- /dev/null +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/service/CarService.java @@ -0,0 +1,17 @@ +package com.teenthofabud.codingchallenge.sharenow.car.service; + +import com.teenthofabud.codingchallenge.sharenow.car.model.error.CarServiceException; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarDetailsVO; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarVO; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public interface CarService { + + public List retrieveAllVehicles(); + + public CarDetailsVO retrieveVehicleDetailsByVin(String vin) throws CarServiceException; + +} diff --git a/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/service/impl/CarServiceImpl.java b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/service/impl/CarServiceImpl.java new file mode 100644 index 0000000..d8ed010 --- /dev/null +++ b/car-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/car/service/impl/CarServiceImpl.java @@ -0,0 +1,113 @@ +package com.teenthofabud.codingchallenge.sharenow.car.service.impl; + +import com.teenthofabud.codingchallenge.sharenow.car.converter.PositionEntity2VOConverter; +import com.teenthofabud.codingchallenge.sharenow.car.converter.CarEntity2DetailedVOConverter; +import com.teenthofabud.codingchallenge.sharenow.car.converter.CarEntity2VOConverter; +import com.teenthofabud.codingchallenge.sharenow.car.model.entity.CarEntity; +import com.teenthofabud.codingchallenge.sharenow.car.model.error.CarErrorCode; +import com.teenthofabud.codingchallenge.sharenow.car.model.error.CarServiceException; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.PositionVO; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarDetailsVO; +import com.teenthofabud.codingchallenge.sharenow.car.model.vo.CarVO; +import com.teenthofabud.codingchallenge.sharenow.car.repository.CarRepository; +import com.teenthofabud.codingchallenge.sharenow.car.service.CarService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import javax.annotation.PostConstruct; +import java.util.*; + +@Component +public class CarServiceImpl implements CarService { + + private static final Logger LOGGER = LoggerFactory.getLogger(CarServiceImpl.class); + + @Autowired + private CarRepository repository; + + private PositionEntity2VOConverter positionConverter; + + private CarEntity2VOConverter simpleVOConverter; + + private CarEntity2DetailedVOConverter complexVOConverter; + + private Comparator cmpVehicleByVin; + + @PostConstruct + private void init() { + this.positionConverter = (entity) -> { + PositionVO vo = new PositionVO(); + if(entity != null) { + vo.setLatitude(entity.getLatitude()); + vo.setLongitude(entity.getLongitude()); + } + return vo; + }; + this.simpleVOConverter = (entity) -> { + CarVO vo = new CarVO(); + if(entity != null) { + vo.setId(entity.getId()); + vo.setNumberPlate(entity.getNumberPlate()); + vo.setVin(entity.getVin()); + vo.setLocationId(entity.getLocationId()); + } + return vo; + }; + this.complexVOConverter = (entity) -> { + CarDetailsVO vo = new CarDetailsVO(); + if(entity != null) { + vo.setId(entity.getId()); + vo.setNumberPlate(entity.getNumberPlate()); + vo.setVin(entity.getVin()); + vo.setFuel(entity.getFuel()); + vo.setLocationId(entity.getLocationId()); + vo.setModel(entity.getModel()); + vo.setPosition(positionConverter.convert(entity.getPosition())); + } + return vo; + }; + this.cmpVehicleByVin = (v1, v2) -> { + return v1.getVin().compareTo(v2.getVin()); + }; + } + + @Override + public List retrieveAllVehicles() { + List carVOList = new ArrayList<>(); + Iterable entityItr = this.repository.findAll(); + for(CarEntity entity : entityItr) { + CarVO vo = this.simpleVOConverter.convert(entity); + carVOList.add(vo); + } + LOGGER.info("Found {} vehicles", carVOList.size()); + return carVOList; + } + + @Override + public CarDetailsVO retrieveVehicleDetailsByVin(String vin) throws CarServiceException { + if(StringUtils.hasText(vin)) { + Iterable entityItr = this.repository.findAll(); + List entityList = new ArrayList<>(); + for(CarEntity entity : entityItr) { + entityList.add(entity); + } + Collections.sort(entityList, cmpVehicleByVin); + int idx = Collections.binarySearch(entityList, new CarEntity(vin), cmpVehicleByVin); + if(idx >= 0) { + CarEntity entity = entityList.get(idx); + CarDetailsVO vo = this.complexVOConverter.convert(entity); + LOGGER.info("Found vehicle by vin: {}", vin); + return vo; + } else { + LOGGER.error("No vehicle found with vin: {}", vin); + throw new CarServiceException("No vehicle found that matches with vin", CarErrorCode.NOT_FOUND, new Object[] {"vin", vin}); + } + } else { + LOGGER.error("Invalid vin: {}", vin); + throw new CarServiceException("Invalid vin", CarErrorCode.INVALID_PARAMETER, new Object[] {"vin"}); + } + } +} diff --git a/car-service/src/main/resources/application.properties b/car-service/src/main/resources/application.properties new file mode 100644 index 0000000..fc1b0db --- /dev/null +++ b/car-service/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.application.name=car-service + +server.servlet.context-path=/car +server.port=18080 + +spring.redis.host=localhost +spring.redis.port=6379 \ No newline at end of file diff --git a/car-service/src/main/resources/messages.properties b/car-service/src/main/resources/messages.properties new file mode 100644 index 0000000..b4ecec2 --- /dev/null +++ b/car-service/src/main/resources/messages.properties @@ -0,0 +1,2 @@ +SNCC-CS-001=%s: %s not available +SNCC-CS-002=%s is not valid \ No newline at end of file diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/CarCleanupService.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/CarCleanupService.java new file mode 100644 index 0000000..d522c70 --- /dev/null +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/CarCleanupService.java @@ -0,0 +1,10 @@ +package com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service; + +import org.springframework.stereotype.Service; + +@Service +public interface CarCleanupService { + + public void clearStaleCarsForConfiguredCity(); + +} diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/VehicleStaleness.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/CarStaleness.java similarity index 59% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/VehicleStaleness.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/CarStaleness.java index 0e5f691..a691c0f 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/VehicleStaleness.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/CarStaleness.java @@ -1,10 +1,10 @@ package com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service; -import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.VehicleEntity; +import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.CarEntity; @FunctionalInterface -public interface VehicleStaleness { +public interface CarStaleness { - public boolean isVehicleStale(VehicleEntity entity); + public boolean isCarStale(CarEntity entity); } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/VehicleCleanupService.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/VehicleCleanupService.java deleted file mode 100644 index 07de2e4..0000000 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/VehicleCleanupService.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service; - -public interface VehicleCleanupService { - - public void clearStaleVehiclesForConfiguredCity(); - -} diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/impl/Car2GoVehicleCleanupServiceImpl.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/impl/Car2GoCarCleanupServiceImpl.java similarity index 69% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/impl/Car2GoVehicleCleanupServiceImpl.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/impl/Car2GoCarCleanupServiceImpl.java index c9a3b66..251155b 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/impl/Car2GoVehicleCleanupServiceImpl.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/cleanup/service/impl/Car2GoCarCleanupServiceImpl.java @@ -1,12 +1,11 @@ package com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service.impl; import com.teenthofabud.codingchallenge.sharenow.polling.PollingMonitor; -import com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service.VehicleCleanupService; -import com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service.VehicleStaleness; -import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.VehicleEntity; -import com.teenthofabud.codingchallenge.sharenow.polling.repository.VehicleRepository; +import com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service.CarCleanupService; +import com.teenthofabud.codingchallenge.sharenow.polling.cleanup.service.CarStaleness; +import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.CarEntity; +import com.teenthofabud.codingchallenge.sharenow.polling.repository.CarRepository; import org.redisson.api.RKeys; -import org.redisson.api.RMapCache; import org.redisson.api.RedissonClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,9 +19,9 @@ @Component -public class Car2GoVehicleCleanupServiceImpl implements VehicleCleanupService { +public class Car2GoCarCleanupServiceImpl implements CarCleanupService { - private static final Logger LOGGER = LoggerFactory.getLogger(Car2GoVehicleCleanupServiceImpl.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Car2GoCarCleanupServiceImpl.class); @Value("${ps.cleanup.staleness.interval:60}") private int stalenessIntervalInSeconds; @@ -34,12 +33,12 @@ public class Car2GoVehicleCleanupServiceImpl implements VehicleCleanupService { private RedissonClient redisson; @Autowired - private VehicleRepository repository; + private CarRepository repository; @Autowired private PollingMonitor monitor; - private VehicleStaleness staleDetector; + private CarStaleness staleDetector; @PostConstruct private void init() { @@ -58,21 +57,21 @@ private void init() { @Override @Scheduled(cron = "${ps.cleanup.cron.expression:*/90 * * * *}") - public void clearStaleVehiclesForConfiguredCity() { + public void clearStaleCarsForConfiguredCity() { synchronized (monitor) { - Iterable itr = this.repository.findAll(); + Iterable itr = this.repository.findAll(); RKeys keys = redisson.getKeys(); int count = 0; - LOGGER.info("Starting transaction for evicting stale vehicles by least recently updated policy"); - for(VehicleEntity entity : itr) { - if(this.staleDetector.isVehicleStale(entity)) { - String vehicleKey = entity.getCacheKey(); - keys.expire(vehicleKey, ttlEviction, TimeUnit.SECONDS); - LOGGER.info("Preparing vehicle with id {} for eviction by assigning the lowest ttl", entity.getId()); + LOGGER.info("Starting transaction for evicting stale cars by least recently updated policy"); + for(CarEntity entity : itr) { + if(this.staleDetector.isCarStale(entity)) { + String carKey = entity.getCacheKey(); + keys.expire(carKey, ttlEviction, TimeUnit.SECONDS); + LOGGER.info("Preparing cars with id {} for eviction by assigning the lowest ttl", entity.getId()); count++; } } - LOGGER.info("Completed transaction for evicting {} stale vehicles", count); + LOGGER.info("Completed transaction for evicting {} stale cars", count); } } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/dto/VehicleDTO.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/dto/CarDTO.java similarity index 92% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/dto/VehicleDTO.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/dto/CarDTO.java index ac9d9db..81deb7b 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/dto/VehicleDTO.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/dto/CarDTO.java @@ -5,7 +5,7 @@ @Getter @Setter -public class VehicleDTO { +public class CarDTO { private int id; private int locationId; diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/entity/VehicleEntity.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/entity/CarEntity.java similarity index 84% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/entity/VehicleEntity.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/entity/CarEntity.java index 8ca6b3d..bb59a40 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/entity/VehicleEntity.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/model/entity/CarEntity.java @@ -14,13 +14,14 @@ @Getter @Setter @ToString -@RedisHash("Vehicle") -@TypeAlias("Vehicle") -public class VehicleEntity implements Serializable { +@RedisHash("Car") +@TypeAlias("Car") +public class CarEntity implements Serializable { - @Id + @Indexed private int id; private int locationId; + @Id @Indexed private String vin; private String numberPlate; @@ -30,7 +31,7 @@ public class VehicleEntity implements Serializable { private Date updatedAt; public String getCacheKey() { - return "Vehicle:" + id; + return "Car:" + vin; } } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/configuration/VehicleRefreshConfiguration.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/configuration/CarRefreshConfiguration.java similarity index 94% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/configuration/VehicleRefreshConfiguration.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/configuration/CarRefreshConfiguration.java index 4666908..d5ca0de 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/configuration/VehicleRefreshConfiguration.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/configuration/CarRefreshConfiguration.java @@ -7,7 +7,7 @@ import org.springframework.stereotype.Component; @Component -public class VehicleRefreshConfiguration { +public class CarRefreshConfiguration { @Autowired private RedisConnectionFactory connectionFactory; diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/VehicleDTO2EntityConveter.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/CarDTO2EntityConverter.java similarity index 69% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/VehicleDTO2EntityConveter.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/CarDTO2EntityConverter.java index 4076f51..1358f25 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/VehicleDTO2EntityConveter.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/CarDTO2EntityConverter.java @@ -1,9 +1,9 @@ package com.teenthofabud.codingchallenge.sharenow.polling.refresh.converter; -import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.VehicleDTO; -import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.VehicleEntity; +import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.CarDTO; +import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.CarEntity; import org.springframework.core.convert.converter.Converter; @FunctionalInterface -public interface VehicleDTO2EntityConveter extends Converter { +public interface CarDTO2EntityConverter extends Converter { } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/PositionDTO2EntityConveter.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/PositionDTO2EntityConverter.java similarity index 77% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/PositionDTO2EntityConveter.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/PositionDTO2EntityConverter.java index 29faf39..12c8d75 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/PositionDTO2EntityConveter.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/converter/PositionDTO2EntityConverter.java @@ -5,5 +5,5 @@ import org.springframework.core.convert.converter.Converter; @FunctionalInterface -public interface PositionDTO2EntityConveter extends Converter { +public interface PositionDTO2EntityConverter extends Converter { } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleInput.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarInput.java similarity index 65% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleInput.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarInput.java index 2359e84..2f68f84 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleInput.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarInput.java @@ -1,13 +1,13 @@ package com.teenthofabud.codingchallenge.sharenow.polling.refresh.service; -import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.VehicleDTO; +import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.CarDTO; import java.util.List; @FunctionalInterface -public interface VehicleInput { +public interface CarInput { - public List readVehicles(T source); + public List readCars(T source); } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleOutput.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarOutput.java similarity index 61% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleOutput.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarOutput.java index 3156b5d..a24edd1 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleOutput.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarOutput.java @@ -1,12 +1,12 @@ package com.teenthofabud.codingchallenge.sharenow.polling.refresh.service; -import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.VehicleEntity; +import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.CarEntity; import java.util.List; @FunctionalInterface -public interface VehicleOutput { +public interface CarOutput { - public boolean writeVehicles(List entityList); + public boolean writeCars(List entityList); } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleProcessor.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarProcessor.java similarity index 63% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleProcessor.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarProcessor.java index a7a9cb1..f2b40a4 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleProcessor.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarProcessor.java @@ -1,13 +1,13 @@ package com.teenthofabud.codingchallenge.sharenow.polling.refresh.service; -import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.VehicleDTO; -import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.VehicleEntity; +import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.CarDTO; +import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.CarEntity; import java.util.List; @FunctionalInterface -public interface VehicleProcessor { +public interface CarProcessor { - public List processVehicles(List dtoList); + public List processCars(List dtoList); } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleRefreshService.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarRefreshService.java similarity index 58% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleRefreshService.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarRefreshService.java index a6f2f4b..55a30b7 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleRefreshService.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/CarRefreshService.java @@ -3,8 +3,8 @@ import org.springframework.stereotype.Service; @Service -public interface VehicleRefreshService { +public interface CarRefreshService { - public void collectLiveVehiclesForConfiguredCity(); + public void collectLiveCarsForConfiguredCity(); } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleService.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleService.java deleted file mode 100644 index 41391ee..0000000 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/VehicleService.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.polling.refresh.service; - -import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.VehicleDTO; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Service -public interface VehicleService { - - public List retrieveAllVehicles(); - - public VehicleDTO retrieveVehicleDetailsByVIN(String vin); - -} diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/impl/Car2GoVehicleRefreshServiceImpl.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/impl/Car2GoCarRefreshServiceImpl.java similarity index 72% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/impl/Car2GoVehicleRefreshServiceImpl.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/impl/Car2GoCarRefreshServiceImpl.java index a94d523..06c6c25 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/impl/Car2GoVehicleRefreshServiceImpl.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/refresh/service/impl/Car2GoCarRefreshServiceImpl.java @@ -2,17 +2,17 @@ import com.teenthofabud.codingchallenge.sharenow.polling.PollingMonitor; -import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.VehicleDTO; +import com.teenthofabud.codingchallenge.sharenow.polling.model.dto.CarDTO; import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.PositionEntity; -import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.VehicleEntity; -import com.teenthofabud.codingchallenge.sharenow.polling.refresh.converter.PositionDTO2EntityConveter; -import com.teenthofabud.codingchallenge.sharenow.polling.refresh.converter.VehicleDTO2EntityConveter; +import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.CarEntity; +import com.teenthofabud.codingchallenge.sharenow.polling.refresh.converter.PositionDTO2EntityConverter; +import com.teenthofabud.codingchallenge.sharenow.polling.refresh.converter.CarDTO2EntityConverter; import com.teenthofabud.codingchallenge.sharenow.polling.filter.RestClientErrorHandler; -import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.VehicleInput; -import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.VehicleOutput; -import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.VehicleProcessor; -import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.VehicleRefreshService; -import com.teenthofabud.codingchallenge.sharenow.polling.repository.VehicleRepository; +import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.CarInput; +import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.CarOutput; +import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.CarProcessor; +import com.teenthofabud.codingchallenge.sharenow.polling.refresh.service.CarRefreshService; +import com.teenthofabud.codingchallenge.sharenow.polling.repository.CarRepository; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -32,23 +32,23 @@ import java.util.Map; @Component -public class Car2GoVehicleRefreshServiceImpl implements VehicleRefreshService { +public class Car2GoCarRefreshServiceImpl implements CarRefreshService { - private static final Logger LOGGER = LoggerFactory.getLogger(Car2GoVehicleRefreshServiceImpl.class); + private static final Logger LOGGER = LoggerFactory.getLogger(Car2GoCarRefreshServiceImpl.class); private static String REQUEST_URL; private RestTemplate car2goClient; - private PositionDTO2EntityConveter positionDto2Entity; + private PositionDTO2EntityConverter positionDto2Entity; - private VehicleDTO2EntityConveter vehicleDto2Entity; + private CarDTO2EntityConverter vehicleDto2Entity; - private VehicleInput input; + private CarInput input; - private VehicleProcessor processor; + private CarProcessor processor; - private VehicleOutput output; + private CarOutput output; private int counter; @@ -65,7 +65,7 @@ public class Car2GoVehicleRefreshServiceImpl implements VehicleRefreshService { private String car2goVehiclesByLocationURI; @Autowired - private VehicleRepository repository; + private CarRepository repository; @Autowired private RestClientErrorHandler errorHandler; @@ -81,14 +81,14 @@ private void init() { this.car2goClient.setErrorHandler(this.errorHandler); this.counter = 0; this.input = (sourceUrl) -> { - List dtoList = new ArrayList<>(); + List dtoList = new ArrayList<>(); String variableName = "locationName"; Map uriVariables = Collections.singletonMap(variableName, searchLocation); - ResponseEntity> responseEntity = + ResponseEntity> responseEntity = car2goClient.exchange(sourceUrl, HttpMethod.GET, null, - new ParameterizedTypeReference>() {}, uriVariables); + new ParameterizedTypeReference>() {}, uriVariables); dtoList = responseEntity.getBody(); - LOGGER.info("Queried vehicle count: {}", dtoList.size()); + LOGGER.info("Queried car count: {}", dtoList.size()); return dtoList; }; this.positionDto2Entity = (dto) -> { @@ -98,7 +98,7 @@ private void init() { return entity; }; this.vehicleDto2Entity = (dto) -> { - VehicleEntity entity = new VehicleEntity(); + CarEntity entity = new CarEntity(); entity.setFuel(dto.getFuel()); entity.setId(dto.getId()); entity.setLocationId(dto.getLocationId()); @@ -110,22 +110,22 @@ private void init() { return entity; }; this.processor = (dtoList) -> { - List entityList = new ArrayList<>(); - for(VehicleDTO dto : dtoList) { - VehicleEntity entity = this.vehicleDto2Entity.convert(dto); + List entityList = new ArrayList<>(); + for(CarDTO dto : dtoList) { + CarEntity entity = this.vehicleDto2Entity.convert(dto); entityList.add(entity); } - LOGGER.info("Processed vehicle count: {}", entityList.size()); + LOGGER.info("Processed car count: {}", entityList.size()); return entityList; }; this.output = (entityList) -> { int count = 0; - for(VehicleEntity entity : entityList) { + for(CarEntity entity : entityList) { if(this.repository.save(entity) != null) { count++; } } - LOGGER.info("Updated vehicle count: {}", count); + LOGGER.info("Updated car count: {}", count); return count == entityList.size(); }; } @@ -133,12 +133,12 @@ private void init() { @Override @Scheduled(cron = "${ps.refresh.cron.expression:*/5 * * * *}") - public void collectLiveVehiclesForConfiguredCity() { + public void collectLiveCarsForConfiguredCity() { synchronized (monitor) { - List vehicleDTOs = this.input.readVehicles(REQUEST_URL); - List vehicleEntities = this.processor.processVehicles(vehicleDTOs); - boolean status = this.output.writeVehicles(vehicleEntities); - LOGGER.info("Vehicle refresh status: {}", status); + List dtoList = this.input.readCars(REQUEST_URL); + List entityList = this.processor.processCars(dtoList); + boolean status = this.output.writeCars(entityList); + LOGGER.info("car refresh status: {}", status); } } diff --git a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/repository/VehicleRepository.java b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/repository/CarRepository.java similarity index 71% rename from polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/repository/VehicleRepository.java rename to polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/repository/CarRepository.java index 64457e9..fdd033b 100644 --- a/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/repository/VehicleRepository.java +++ b/polling-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/polling/repository/CarRepository.java @@ -1,9 +1,9 @@ package com.teenthofabud.codingchallenge.sharenow.polling.repository; -import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.VehicleEntity; +import com.teenthofabud.codingchallenge.sharenow.polling.model.entity.CarEntity; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository -public interface VehicleRepository extends CrudRepository { +public interface CarRepository extends CrudRepository { } diff --git a/polling-service/src/main/resources/application.properties b/polling-service/src/main/resources/application.properties index 4d9e58f..74b194b 100644 --- a/polling-service/src/main/resources/application.properties +++ b/polling-service/src/main/resources/application.properties @@ -8,7 +8,7 @@ ps.refresh.search.location=Stuttgart ps.refresh.cron.expression=*/20 * * * * * ps.cleanup.cron.expression=*/90 * * * * * ps.cleanup.staleness.interval=20 -pps.cleanup.ttl.eviction=3 +ps.cleanup.ttl.eviction=3 car2go.base.url=http://192.168.0.119:3000 car2go.vehicles.by.location.uri=/vehicles/{locationName} \ No newline at end of file diff --git a/polling-service/src/test/java/com/teenthofabud/codingchallenge/sharenow/polling/VehicleRefreshServiceTests.java b/polling-service/src/test/java/com/teenthofabud/codingchallenge/sharenow/polling/CarRefreshServiceTests.java similarity index 95% rename from polling-service/src/test/java/com/teenthofabud/codingchallenge/sharenow/polling/VehicleRefreshServiceTests.java rename to polling-service/src/test/java/com/teenthofabud/codingchallenge/sharenow/polling/CarRefreshServiceTests.java index b6fde13..5a3386d 100644 --- a/polling-service/src/test/java/com/teenthofabud/codingchallenge/sharenow/polling/VehicleRefreshServiceTests.java +++ b/polling-service/src/test/java/com/teenthofabud/codingchallenge/sharenow/polling/CarRefreshServiceTests.java @@ -8,7 +8,7 @@ import org.springframework.web.client.RestTemplate; @SpringBootTest -public class VehicleRefreshServiceTests { +public class CarRefreshServiceTests { @MockBean private RestTemplate restClient; diff --git a/scripts/seeding/seed_geojson_dump.py b/scripts/seeding/seed_geojson_dump.py deleted file mode 100644 index e69de29..0000000 diff --git a/vehicle-service/Dockerfile b/vehicle-service/Dockerfile deleted file mode 100644 index 93c351d..0000000 --- a/vehicle-service/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM adoptopenjdk:11-jre-hotspot-focal -MAINTAINER Anirban Das -RUN mkdip ~p /opt/sharenow-coding-challenge -COPY target/vehicle-app.jar /opt/sharenow-coding-challenge/vehicle-app.jar -CMD ["java","-jar","/opt/sharenow-coding-challenge/vehicle-app.jar"] \ No newline at end of file diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/controller/VehicleSearchController.java b/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/controller/VehicleSearchController.java deleted file mode 100644 index 47bfc51..0000000 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/controller/VehicleSearchController.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.controller; - -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.error.VehicleErrorCode; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.error.VehicleServiceException; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleDetailsVO; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleVO; -import com.teenthofabud.codingchallenge.sharenow.vehicle.service.VehicleService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.util.StringUtils; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.List; - -@RestController -@RequestMapping("vehicle") -public class VehicleController { - - private static final Logger LOGGER = LoggerFactory.getLogger(VehicleController.class); - - @Autowired - private VehicleService service; - - @GetMapping("vin/{vin}") - public ResponseEntity getVehicleByVin(@PathVariable String vin) throws VehicleServiceException{ - if(StringUtils.hasText(vin)) { - VehicleDetailsVO vo = this.service.retrieveVehicleDetailsByVin(vin); - ResponseEntity response = ResponseEntity.ok(vo); - return response; - } else { - throw new VehicleServiceException("", VehicleErrorCode.INVALID_PARAMETER, new Object[] {"vin"}); - } - } - - @GetMapping - public ResponseEntity getAllVehicles() throws VehicleServiceException{ - List voList = this.service.retrieveAllVehicles(); - ResponseEntity> response = ResponseEntity.ok(voList); - return response; - } - -} diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/VehicleEntity2DetailedVOConverter.java b/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/VehicleEntity2DetailedVOConverter.java deleted file mode 100644 index 1ede1be..0000000 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/VehicleEntity2DetailedVOConverter.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.converter; - -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.entity.VehicleEntity; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleDetailsVO; -import org.springframework.core.convert.converter.Converter; - -@FunctionalInterface -public interface VehicleEntity2DetailedVOConverter extends Converter { - - public VehicleDetailsVO convert(VehicleEntity entity); - -} diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/VehicleEntity2VOConverter.java b/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/VehicleEntity2VOConverter.java deleted file mode 100644 index 48a2003..0000000 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/converter/VehicleEntity2VOConverter.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.converter; - -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.entity.VehicleEntity; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleVO; -import org.springframework.core.convert.converter.Converter; - -@FunctionalInterface -public interface VehicleEntity2VOConverter extends Converter { - - public VehicleVO convert(VehicleEntity entity); - -} diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/repository/VehicleRepository.java b/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/repository/VehicleRepository.java deleted file mode 100644 index e1088c8..0000000 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/repository/VehicleRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.repository; - -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.entity.VehicleEntity; -import org.springframework.data.repository.CrudRepository; -import org.springframework.stereotype.Repository; - -import java.util.List; - -@Repository -public interface VehicleRepository extends CrudRepository { - - public List findByVin(String vin); - -} diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/service/VehicleService.java b/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/service/VehicleService.java deleted file mode 100644 index 97335ad..0000000 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/service/VehicleService.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.service; - -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.error.VehicleServiceException; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleDetailsVO; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleVO; -import org.springframework.stereotype.Service; - -import java.util.List; - -@Service -public interface VehicleService { - - public List retrieveAllVehicles(); - - public VehicleDetailsVO retrieveVehicleDetailsByVin(String vin) throws VehicleServiceException; - -} diff --git a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/service/impl/VehicleServiceImpl.java b/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/service/impl/VehicleServiceImpl.java deleted file mode 100644 index 557fb5b..0000000 --- a/vehicle-service/src/main/java/com/teenthofabud/codingchallenge/sharenow/vehicle/service/impl/VehicleServiceImpl.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.teenthofabud.codingchallenge.sharenow.vehicle.service.impl; - -import com.teenthofabud.codingchallenge.sharenow.vehicle.converter.PositionEntity2VOConverter; -import com.teenthofabud.codingchallenge.sharenow.vehicle.converter.VehicleEntity2DetailedVOConverter; -import com.teenthofabud.codingchallenge.sharenow.vehicle.converter.VehicleEntity2VOConverter; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.entity.VehicleEntity; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.error.VehicleErrorCode; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.error.VehicleServiceException; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.PositionVO; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleDetailsVO; -import com.teenthofabud.codingchallenge.sharenow.vehicle.model.vo.VehicleVO; -import com.teenthofabud.codingchallenge.sharenow.vehicle.repository.VehicleRepository; -import com.teenthofabud.codingchallenge.sharenow.vehicle.service.VehicleService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; - -import javax.annotation.PostConstruct; -import java.util.*; - -@Component -public class VehicleServiceImpl implements VehicleService { - - private static final Logger LOGGER = LoggerFactory.getLogger(VehicleServiceImpl.class); - - @Autowired - private VehicleRepository repository; - - private PositionEntity2VOConverter positionConverter; - - private VehicleEntity2VOConverter simpleVOConverter; - - private VehicleEntity2DetailedVOConverter complexVOConverter; - - private Comparator cmpVehicleByVin; - - @PostConstruct - private void init() { - this.positionConverter = (entity) -> { - PositionVO vo = new PositionVO(); - if(entity != null) { - vo.setLatitude(entity.getLatitude()); - vo.setLongitude(entity.getLongitude()); - } - return vo; - }; - this.simpleVOConverter = (entity) -> { - VehicleVO vo = new VehicleVO(); - if(entity != null) { - vo.setId(entity.getId()); - vo.setNumberPlate(entity.getNumberPlate()); - vo.setVin(entity.getVin()); - vo.setLocationId(entity.getLocationId()); - } - return vo; - }; - this.complexVOConverter = (entity) -> { - VehicleDetailsVO vo = new VehicleDetailsVO(); - if(entity != null) { - vo.setId(entity.getId()); - vo.setNumberPlate(entity.getNumberPlate()); - vo.setVin(entity.getVin()); - vo.setFuel(entity.getFuel()); - vo.setLocationId(entity.getLocationId()); - vo.setModel(entity.getModel()); - vo.setPosition(positionConverter.convert(entity.getPosition())); - } - return vo; - }; - this.cmpVehicleByVin = (v1, v2) -> { - return v1.getVin().compareTo(v2.getVin()); - }; - } - - @Override - public List retrieveAllVehicles() { - List vehicleVOList = new ArrayList<>(); - Iterable entityItr = this.repository.findAll(); - for(VehicleEntity entity : entityItr) { - VehicleVO vo = this.simpleVOConverter.convert(entity); - vehicleVOList.add(vo); - } - return vehicleVOList; - } - - @Override - public VehicleDetailsVO retrieveVehicleDetailsByVin(String vin) throws VehicleServiceException { - if(StringUtils.hasText(vin)) { - Iterable entityItr = this.repository.findAll(); - List entityList = new ArrayList<>(); - for(VehicleEntity entity : entityItr) { - entityList.add(entity); - } - Collections.sort(entityList, cmpVehicleByVin); - int idx = Collections.binarySearch(entityList, new VehicleEntity(vin), cmpVehicleByVin); - //List entityList = this.repository.findByVin(vin); - //if(entityList != null && entityList.size() == 1) - if(idx >= 0) { - VehicleEntity entity = entityList.get(idx); - VehicleDetailsVO vo = this.complexVOConverter.convert(entity); - return vo; - } else { - throw new VehicleServiceException("", VehicleErrorCode.NOT_FOUND, new Object[] {"vin", vin}); - } - } else { - throw new VehicleServiceException("", VehicleErrorCode.INVALID_PARAMETER, new Object[] {"vin"}); - } - } -} diff --git a/vehicle-service/src/main/resources/application.properties b/vehicle-service/src/main/resources/application.properties deleted file mode 100644 index f93544d..0000000 --- a/vehicle-service/src/main/resources/application.properties +++ /dev/null @@ -1,5 +0,0 @@ -spring.application.name=vehicle-service -server.port=18080 - -spring.redis.host=localhost -spring.redis.port=6379 \ No newline at end of file diff --git a/vehicle-service/src/main/resources/messages.properties b/vehicle-service/src/main/resources/messages.properties deleted file mode 100644 index 96249d7..0000000 --- a/vehicle-service/src/main/resources/messages.properties +++ /dev/null @@ -1,2 +0,0 @@ -SNCC-VS-001=%s: %s not available -SNCC-VS-002=%s is not valid \ No newline at end of file