Skip to content

Commit

Permalink
renamed controller in car and polygon service to search specific cont…
Browse files Browse the repository at this point in the history
…roller since the client facing apis of the services are read only in nature, implemented end to end flow of polygon service for all search functionalities
  • Loading branch information
ANIRBANSTIFLER committed Mar 15, 2021
1 parent d374ef0 commit 79dc25d
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Locale;

@Configuration
@EnableRedisRepositories
@EnableRedisRepositories(basePackages = "com.teenthofabud.codingchallenge.sharenow.car.repository")
public class CarServiceConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ public class CarSearchController {
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);
public ResponseEntity<?> getCarByVin(@PathVariable String vin) throws CarServiceException {
LOGGER.info("Requesting car with vin: {}", vin);
CarDetailsVO vo = this.service.retrieveCarDetailsByVin(vin);
ResponseEntity<CarDetailsVO> response = ResponseEntity.ok(vo);
LOGGER.info("Responding with vehicle of vin: {}", vin);
LOGGER.info("Responding with car of vin: {}", vin);
return response;
}

@GetMapping
public ResponseEntity<?> getAllVehicles() throws CarServiceException {
LOGGER.info("Requesting all vehicles");
List<CarVO> voList = this.service.retrieveAllVehicles();
public ResponseEntity<?> getAllCars() throws CarServiceException {
LOGGER.info("Requesting all cars");
List<CarVO> voList = this.service.retrieveAllCars();
ResponseEntity<List<CarVO>> response = ResponseEntity.ok(voList);
LOGGER.info("Responding with all available vehicles");
LOGGER.info("Responding with all available cars");
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
@Getter
@Setter
@ToString
@RedisHash("Vehicle")
@TypeAlias("Vehicle")
@RedisHash("Car")
@TypeAlias("Car")
@NoArgsConstructor
public class CarEntity implements Serializable {

@Id
@Indexed
private int id;
private int locationId;
@Id
@Indexed
private String vin;
private String numberPlate;
Expand All @@ -32,7 +33,7 @@ public class CarEntity implements Serializable {
private Date updatedAt;

public String getCacheKey() {
return "Vehicle:" + id;
return "Car:" + vin;
}

public CarEntity(String vin) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
@Service
public interface CarService {

public List<CarVO> retrieveAllVehicles();
public List<CarVO> retrieveAllCars();

public CarDetailsVO retrieveVehicleDetailsByVin(String vin) throws CarServiceException;
public CarDetailsVO retrieveCarDetailsByVin(String vin) throws CarServiceException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CarServiceImpl implements CarService {

private CarEntity2DetailedVOConverter complexVOConverter;

private Comparator<CarEntity> cmpVehicleByVin;
private Comparator<CarEntity> cmpCarByVin;

@PostConstruct
private void init() {
Expand Down Expand Up @@ -69,41 +69,41 @@ private void init() {
}
return vo;
};
this.cmpVehicleByVin = (v1, v2) -> {
return v1.getVin().compareTo(v2.getVin());
this.cmpCarByVin = (v1, v2) -> {
if(v1 != null && v2 != null) {
return v1.getVin().compareTo(v2.getVin());
} else {
return 0;
}
};
}

@Override
public List<CarVO> retrieveAllVehicles() {
public List<CarVO> retrieveAllCars() {
List<CarVO> carVOList = new ArrayList<>();
Iterable<CarEntity> entityItr = this.repository.findAll();
for(CarEntity entity : entityItr) {
CarVO vo = this.simpleVOConverter.convert(entity);
carVOList.add(vo);
if(entity != null && StringUtils.hasText(entity.getVin())) {
CarVO vo = this.simpleVOConverter.convert(entity);
carVOList.add(vo);
}
}
LOGGER.info("Found {} vehicles", carVOList.size());
LOGGER.info("Found {} cars", carVOList.size());
return carVOList;
}

@Override
public CarDetailsVO retrieveVehicleDetailsByVin(String vin) throws CarServiceException {
public CarDetailsVO retrieveCarDetailsByVin(String vin) throws CarServiceException {
if(StringUtils.hasText(vin)) {
Iterable<CarEntity> entityItr = this.repository.findAll();
List<CarEntity> 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);
Optional<CarEntity> optEntity = this.repository.findById(vin);
if(optEntity.isPresent()) {
CarEntity entity = optEntity.get();
CarDetailsVO vo = this.complexVOConverter.convert(entity);
LOGGER.info("Found vehicle by vin: {}", vin);
LOGGER.info("Found car 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});
LOGGER.error("No car found with vin: {}", vin);
throw new CarServiceException("No car found that matches with vin", CarErrorCode.NOT_FOUND, new Object[] {"vin", vin});
}
} else {
LOGGER.error("Invalid vin: {}", vin);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,81 @@
package com.teenthofabud.codingchallenge.sharenow.polygon.controller;

import com.teenthofabud.codingchallenge.sharenow.polygon.model.error.PolygonServiceException;
import com.teenthofabud.codingchallenge.sharenow.polygon.model.vo.StrategicPolygonDetailedVO;
import com.teenthofabud.codingchallenge.sharenow.polygon.model.vo.StrategicPolygonVO;
import com.teenthofabud.codingchallenge.sharenow.polygon.service.PolygonService;
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 PolygonSearchController {

private static final Logger LOGGER = LoggerFactory.getLogger(PolygonSearchController.class);

@Autowired
private PolygonService service;

@GetMapping
public ResponseEntity<?> getAllPolygons() {
LOGGER.info("Requesting all polygons");
List<StrategicPolygonVO> voList = this.service.retrieveAll();
ResponseEntity<List<StrategicPolygonVO>> response = ResponseEntity.ok(voList);
LOGGER.info("Responding with all available polygons");
return response;
}

@GetMapping("cityid/{cityId}")
public ResponseEntity<?> getAllPolygonsByCityId(@PathVariable String cityId) throws PolygonServiceException {
LOGGER.info("Requesting all polygons with cityId: {}", cityId);
List<StrategicPolygonVO> voList = this.service.retrieveByCityId(cityId);
ResponseEntity<List<StrategicPolygonVO>> response = ResponseEntity.ok(voList);
LOGGER.info("Responding with all available polygons with cityId: {}", cityId);
return response;
}

@GetMapping("type/{type}")
public ResponseEntity<?> getAllPolygonsByType(@PathVariable String type) throws PolygonServiceException {
LOGGER.info("Requesting all polygons of type: {}", type);
List<StrategicPolygonVO> voList = this.service.retrieveByType(type);
ResponseEntity<List<StrategicPolygonVO>> response = ResponseEntity.ok(voList);
LOGGER.info("Responding with all available polygons with type: {}", type);
return response;
}

@GetMapping("name/{name}")
public ResponseEntity<?> getPolygonByName(@PathVariable String name) throws PolygonServiceException {
LOGGER.info("Requesting all polygons with name: {}", name);
List<StrategicPolygonVO> voList = this.service.retrieveByName(name);
ResponseEntity<List<StrategicPolygonVO>> response = ResponseEntity.ok(voList);
LOGGER.info("Responding with all available polygons with name: {}", name);
return response;
}

@GetMapping("id/{id}")
public ResponseEntity<?> getPolygonById(@PathVariable String id) throws PolygonServiceException {
LOGGER.info("Requesting polygon with id: {}", id);
StrategicPolygonDetailedVO vo = this.service.retrieveById(id);
ResponseEntity<StrategicPolygonDetailedVO> response = ResponseEntity.ok(vo);
LOGGER.info("Responding with polygon of id: {}", id);
return response;
}

@GetMapping("legacyid/{legacyId}")
public ResponseEntity<?> getPolygonByCityId(@PathVariable String legacyId) throws PolygonServiceException {
LOGGER.info("Requesting polygon with legacyId: {}", legacyId);
StrategicPolygonDetailedVO vo = this.service.retrieveByLegacyId(legacyId);
ResponseEntity<StrategicPolygonDetailedVO> response = ResponseEntity.ok(vo);
LOGGER.info("Responding with polygon of legacyId: {}", legacyId);
return response;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.teenthofabud.codingchallenge.sharenow.polygon.model.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.teenthofabud.codingchallenge.sharenow.polygon.model.entity.GeoFeatureEntity;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Getter
@Setter
Expand All @@ -24,5 +26,6 @@ public class StrategicPolygonVO implements Serializable {
private String legacyId;
private String type;
private int version;
private List<GeoFeatureVO> geoFeatures;

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
@Repository
public interface PolygonRepository extends MongoRepository<StrategicPolygonEntity, String> {

public StrategicPolygonEntity findByName(String name);
public List<StrategicPolygonEntity> findAllByName(String name);

public StrategicPolygonEntity findByCityId(String cityId);
public List<StrategicPolygonEntity> findAllByCityId(String cityId);

public StrategicPolygonEntity findByLegacyId(String legacyId);

public List<StrategicPolygonEntity> findByType(String type);
public List<StrategicPolygonEntity> findAllByType(String type);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.teenthofabud.codingchallenge.sharenow.polygon.service;

import com.teenthofabud.codingchallenge.sharenow.polygon.model.entity.StrategicPolygonEntity;
import com.teenthofabud.codingchallenge.sharenow.polygon.model.error.PolygonServiceException;
import com.teenthofabud.codingchallenge.sharenow.polygon.model.vo.StrategicPolygonDetailedVO;
import com.teenthofabud.codingchallenge.sharenow.polygon.model.vo.StrategicPolygonVO;
import org.springframework.stereotype.Service;
Expand All @@ -12,14 +13,14 @@ public interface PolygonService {

public List<StrategicPolygonVO> retrieveAll();

public StrategicPolygonDetailedVO retrieveById(String id);
public StrategicPolygonDetailedVO retrieveById(String id) throws PolygonServiceException;

public StrategicPolygonDetailedVO retrieveByName(String name);
public StrategicPolygonDetailedVO retrieveByLegacyId(String legacyId) throws PolygonServiceException;

public StrategicPolygonDetailedVO retrieveByCityId(String cityId);
public List<StrategicPolygonVO> retrieveByName(String name) throws PolygonServiceException;

public StrategicPolygonDetailedVO retrieveByLegacyId(String legacyId);
public List<StrategicPolygonVO> retrieveByType(String type) throws PolygonServiceException;

public List<StrategicPolygonVO> retrieveByType(String type);
public List<StrategicPolygonVO> retrieveByCityId(String cityId) throws PolygonServiceException;

}
Loading

0 comments on commit 79dc25d

Please sign in to comment.