Skip to content

Commit

Permalink
fixed logic to save and retrieve cars from a redis based cache by usi…
Browse files Browse the repository at this point in the history
…ng the vin of the car as the id of each car entity rather than the id field of the car entity as received from the external black box api, renamed all classes, methods and variables with the content vehicle in them to contain the keyword car instead to be in line with the requirement, renamed vehicle service to car service along with changing the keyword vehicle to car within all classes, methods and variables within previously called vehicle-service, fixed logic in car service to query cars by their vin when the vin is the id of each car entity in the redis store
  • Loading branch information
ANIRBANSTIFLER committed Mar 15, 2021
1 parent b2fa0d3 commit d374ef0
Show file tree
Hide file tree
Showing 50 changed files with 355 additions and 369 deletions.
5 changes: 5 additions & 0 deletions car-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM adoptopenjdk:11-jre-hotspot-focal
MAINTAINER Anirban Das <anirbandas18@live.com>
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"]
10 changes: 3 additions & 7 deletions vehicle-service/pom.xml → car-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>vehicle-service</artifactId>
<artifactId>car-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>vehicle-service</name>
<name>car-service</name>

<properties>
<java.version>11</java.version>
</properties>

<build>
<finalName>vehicle-app</finalName>
<finalName>car-app</finalName>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
Expand Down Expand Up @@ -54,10 +54,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.teenthofabud.codingchallenge.sharenow.vehicle;
package com.teenthofabud.codingchallenge.sharenow.car;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class VehicleServiceApplication {
public class CarServiceApplication {

public static void main(String[] args) {
SpringApplication.run(VehicleServiceApplication.class, args);
SpringApplication.run(CarServiceApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,7 +11,7 @@

@Configuration
@EnableRedisRepositories
public class VehicleServiceConfiguration {
public class CarServiceConfiguration {

@Bean
public LocaleResolver localeResolver() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CarDetailsVO> 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<CarVO> voList = this.service.retrieveAllVehicles();
ResponseEntity<List<CarVO>> response = ResponseEntity.ok(voList);
LOGGER.info("Responding with all available vehicles");
return response;
}

}
Original file line number Diff line number Diff line change
@@ -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<CarEntity, CarDetailsVO> {

public CarDetailsVO convert(CarEntity entity);

}
Original file line number Diff line number Diff line change
@@ -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<CarEntity, CarVO> {

public CarVO convert(CarEntity entity);

}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,8 +20,8 @@ public class RestErrorHandler {
@Autowired
private MessageSource messageSource;

@ExceptionHandler(VehicleServiceException.class)
public ResponseEntity<ErrorVO> handleVehicleServiceException(VehicleServiceException vsex) {
@ExceptionHandler(CarServiceException.class)
public ResponseEntity<ErrorVO> handleVehicleServiceException(CarServiceException vsex) {
LOGGER.error("Error encountered: {}", vsex);
ErrorVO vo = new ErrorVO();
String msg = messageSource.getMessage(vsex.getError().getErrorCode(), null, Locale.US);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,7 +18,7 @@
@RedisHash("Vehicle")
@TypeAlias("Vehicle")
@NoArgsConstructor
public class VehicleEntity implements Serializable {
public class CarEntity implements Serializable {

@Id
private int id;
Expand All @@ -35,7 +35,7 @@ public String getCacheKey() {
return "Vehicle:" + id;
}

public VehicleEntity(String vin) {
public CarEntity(String vin) {
this.vin = vin;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CarEntity, String> {

public List<CarEntity> findByVin(String vin);

}
Original file line number Diff line number Diff line change
@@ -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<CarVO> retrieveAllVehicles();

public CarDetailsVO retrieveVehicleDetailsByVin(String vin) throws CarServiceException;

}
Loading

0 comments on commit d374ef0

Please sign in to comment.