Skip to content

Commit

Permalink
Merge pull request #96 from TLI-Group-1/getClaimedOffers-and-getoffer…
Browse files Browse the repository at this point in the history
…Details-return-carAndOfferInfo

getClaimedOffers and getOfferDetails endpoints both return car info as well as offer info
  • Loading branch information
sammdu authored Dec 9, 2021
2 parents 3e1f599 + 4658ca5 commit 53d198f
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 111 deletions.
5 changes: 2 additions & 3 deletions src/main/java/tech/autodirect/api/ApiEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class ApiEndpoints extends SpringBootServletInitializer {
private final String dbName = "autodirect";

// Initialize Frameworks & Drivers
// TODO: Add explanation, change to more specific?
private TableCarsInterface tableCars;
private TableUsersInterface tableUsers;
private TableOffersInterface tableOffers;
Expand Down Expand Up @@ -175,7 +174,7 @@ public Object unclaimOffer(
@GetMapping("/getClaimedOffers")
public Object getClaimedOffers(@RequestParam(name = "user_id") String userId) {
try {
return svcGetClaimedOffers.getClaimedOffers(tableOffers, userId);
return svcGetClaimedOffers.getClaimedOffers(tableCars, tableOffers, userId);
} catch (SQLException e) {
e.printStackTrace();
throw SERVER_ERROR;
Expand All @@ -188,7 +187,7 @@ public Object getOfferDetails(
@RequestParam(name = "offer_id") String offerId
) {
try {
return svcGetOfferDetails.getOfferDetails(tableOffers, userId, offerId);
return svcGetOfferDetails.getOfferDetails(tableCars, tableOffers, userId, offerId);
} catch (SQLException e) {
e.printStackTrace();
throw SERVER_ERROR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static void exitWithHelp(int exitCode) {
private static void createCarsTable(Connection conn) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(
"CREATE TABLE IF NOT EXISTS public.cars (" +
"id serial NOT NULL PRIMARY KEY, " +
"car_id serial NOT NULL PRIMARY KEY, " +
"brand varchar(50) NOT NULL, " +
"model varchar(50) NOT NULL, " +
"year integer NOT NULL, " +
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/tech/autodirect/api/database/TableCars.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
limitations under the License.
*/

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import tech.autodirect.api.interfaces.TableCarsInterface;

Expand All @@ -26,7 +25,6 @@
import java.util.Map;

public class TableCars extends Table implements TableCarsInterface {
private final String dbName;
private final Connection dbConn;
private final String schemaName = "public";
private final String tableName = "cars";
Expand All @@ -37,7 +35,6 @@ public class TableCars extends Table implements TableCarsInterface {
* @param dbName : name of the database to connect to
*/
public TableCars(String dbName) throws SQLException, ClassNotFoundException {
this.dbName = dbName;
this.dbConn = Conn.getConn(dbName);
}

Expand All @@ -47,11 +44,11 @@ public List<Map<String, Object>> getAllCars() throws SQLException {
}

@Override
public Map<String, Object> getCarById(String carId) throws SQLException, ResponseStatusException {
public Map<String, Object> getCarById(int carId) throws SQLException, ResponseStatusException {
return getEntryById(carId, schemaName, tableName, dbConn, "car");
}

public boolean checkCarExists(String carId) throws SQLException {
public boolean checkCarExists(int carId) throws SQLException {
return checkEntryExists(carId, schemaName, tableName, dbConn, "car");
}
}
10 changes: 5 additions & 5 deletions src/main/java/tech/autodirect/api/entities/EntCar.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* This entity represents a car in our program.
*/
public class EntCar {
private int id;
private int carId;
private String brand;
private String model;
private int year;
Expand All @@ -40,16 +40,16 @@ public class EntCar {
* @param entry : A Map containing representing a car entry in the database.
*/
public void loadFromMap(Map<String, Object> entry) throws SQLException {
id = (int) entry.get("id");
carId = (int) entry.get("car_id");
brand = (String) entry.get("brand");
model = (String) entry.get("model");
year = (int) entry.get("year");
price = ((BigDecimal) entry.get("price")).doubleValue();
kms = UnitConv.mileToKm(((Float) entry.get("mileage")).doubleValue());
}

public int getId() {
return id;
public int getCarId() {
return carId;
}

public String getBrand() {
Expand Down Expand Up @@ -77,7 +77,7 @@ public boolean equals(Object o) {
if (o instanceof EntCar) {
EntCar car = (EntCar) o;

return getId() == car.getId() &&
return getCarId() == car.getCarId() &&
Objects.equals(getBrand(), car.getBrand()) &&
getKms() == car.getKms() &&
Objects.equals(getModel(), car.getModel()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public interface TableCarsInterface {

/**
* Gets all cars from the cars table in the database, using full-text keyword search (TODO).
* Gets all cars from the cars table in the database.
*
* @return A List of Maps where each Map is a single entry in the JDBC query result.
*/
Expand All @@ -34,10 +34,10 @@ public interface TableCarsInterface {
*
* @return A Map representing a car entry in the database.
*/
Map<String, Object> getCarById(String carId) throws SQLException;
Map<String, Object> getCarById(int carId) throws SQLException;

/**
* Check if car exists in database.
*/
boolean checkCarExists(String carId) throws SQLException;
boolean checkCarExists(int carId) throws SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;
import tech.autodirect.api.database.TableCars;
import tech.autodirect.api.entities.EntCar;
import tech.autodirect.api.entities.EntOffer;
import tech.autodirect.api.interfaces.TableCarsInterface;
import tech.autodirect.api.interfaces.TableOffersInterface;
import tech.autodirect.api.utils.MergeCarAndOffer;

import java.sql.SQLException;
import java.util.ArrayList;
Expand All @@ -34,7 +38,8 @@ public class SvcGetClaimedOffers {
/**
* Get all claimed offers for the specified user.
*/
public List<EntOffer> getClaimedOffers(
public List<Map<String, Object>> getClaimedOffers(
TableCarsInterface tableCars,
TableOffersInterface tableOffers,
String userId
) throws SQLException, ResponseStatusException {
Expand All @@ -44,16 +49,25 @@ public List<EntOffer> getClaimedOffers(
);
}

// Set user in offers table and get all offers
tableOffers.setUser(userId);
List<Map<String, Object>> offersList = tableOffers.getAllOffers();

List<EntOffer> offers = new ArrayList<>();
List<Map<String, Object>> carAndOfferInfoMaps = new ArrayList<>();
for (Map<String, Object> offerMap : offersList) {
// Get offer entity
EntOffer offer = new EntOffer();
offer.loadFromMap(offerMap);
offers.add(offer);
}

return offers;
// Get car entity
Map<String, Object> carMap = tableCars.getCarById(offer.getCarId());
EntCar car = new EntCar();
car.loadFromMap(carMap);

// Merge car and offer entities into a map that has both car and offer info to return to frontend
Map<String, Object> carAndOfferInfo = MergeCarAndOffer.mergeCarAndOffer(car, offer);
carAndOfferInfoMaps.add(carAndOfferInfo);
}
return carAndOfferInfoMaps;
}
}
17 changes: 15 additions & 2 deletions src/main/java/tech/autodirect/api/services/SvcGetOfferDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
*/

import org.springframework.http.HttpStatus;
import org.springframework.validation.ObjectError;
import org.springframework.web.server.ResponseStatusException;
import tech.autodirect.api.entities.EntCar;
import tech.autodirect.api.entities.EntOffer;
import tech.autodirect.api.interfaces.TableCarsInterface;
import tech.autodirect.api.interfaces.TableOffersInterface;
import tech.autodirect.api.utils.MergeCarAndOffer;

import java.sql.SQLException;
import java.util.Map;
Expand All @@ -32,7 +36,8 @@ public class SvcGetOfferDetails {
/**
* Get details for an offer (as an EntOffer object).
*/
public EntOffer getOfferDetails(
public Map<String, Object> getOfferDetails(
TableCarsInterface tableCars,
TableOffersInterface tableOffers,
String userId,
String offerId
Expand All @@ -43,12 +48,20 @@ public EntOffer getOfferDetails(
);
}

// Set the user in the offers table
tableOffers.setUser(userId);

// Get offer entity
Map<String, Object> offerMap = tableOffers.getOfferByOfferId(Integer.parseInt(offerId));
EntOffer offer = new EntOffer();
offer.loadFromMap(offerMap);

return offer;
// Get car entity
Map<String, Object> carMap = tableCars.getCarById(offer.getCarId());
EntCar car = new EntCar();
car.loadFromMap(carMap);

// Merge car and offer entities into a map that has both car and offer info to return to frontend
return MergeCarAndOffer.mergeCarAndOffer(car, offer);
}
}
53 changes: 13 additions & 40 deletions src/main/java/tech/autodirect/api/services/SvcSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import tech.autodirect.api.interfaces.TableCarsInterface;
import tech.autodirect.api.interfaces.TableOffersInterface;
import tech.autodirect.api.interfaces.TableUsersInterface;
import tech.autodirect.api.utils.MergeCarAndOffer;
import tech.autodirect.api.utils.ParseChecker;

import java.io.IOException;
Expand All @@ -40,7 +41,8 @@ public class SvcSearch {
private final TableUsersInterface tableUsers;
private final TableOffersInterface tableOffers;
private final SensoApiInterface sensoApi;
private final List<String> valuesOfSortBy = Arrays.asList("price", "payment_mo", "apr", "total_sum", "term_length");
private final List<String> valuesOfSortBy
= Arrays.asList("price", "payment_mo", "interest_rate", "total_sum", "term_mo");
private final List<String> valuesOfSortAsc = Arrays.asList("true", "false");

public SvcSearch(
Expand Down Expand Up @@ -139,8 +141,6 @@ private List<Map<String, Object>> searchAllCars(
}

List<Map<String, Object>> carsMapsAll = this.tableCars.getAllCars();
// TODO: Tell Samm that pre-login should only have price filtering (hardcode "price" so not care about
// sortBy when pre-login?)
return sortCars(carsMapsAll, sortBy, sortAsc);
}

Expand Down Expand Up @@ -176,7 +176,7 @@ private List<Map<String, Object>> searchOnlyCarsWithOffer(
/**
* Resets user's offers table, queries Senso Api for new loan offer information using search params,
* adds approved offers to the user's offers table, and return maps that contain car and offers information
* (made using mergeCarOffer()) using the approved offers that were added to the user's offers table.
* (made using MergeCarAndOffer.mergeCarAndOffer()) using the approved offers that were added to the user's offers table.
*
* Run this method if user's search params are different to previous search (what's currently in the database).
* Thus, we need to reset the offers table and check loan offer approval for each car with the senso /rate api
Expand Down Expand Up @@ -217,7 +217,7 @@ private List<Map<String, Object>> searchCarsWithOfferNewParams(
// carMap in the loop.
if (offer != null) {
// Merge car and offer to create a carAndOfferInfoMap
Map<String, Object> carAndOfferInfoMap = mergeCarAndOffer(car, offer);
Map<String, Object> carAndOfferInfoMap = MergeCarAndOffer.mergeCarAndOffer(car, offer);
carAndOfferInfoMaps.add(carAndOfferInfoMap);
}
}
Expand All @@ -226,10 +226,10 @@ private List<Map<String, Object>> searchCarsWithOfferNewParams(
}

/**
* Returns maps that contain car and offers information (made using mergeCarOffer()) using the offers that
* are currently in the user's offers table (does not reset the offers table or re-call senso Api to
* check whether loan offers are approved, since these are already assumed to be approved in a previous
* search query with the same search params).
* Returns maps that contain car and offers information (made using MergeCarAndOffer.mergeCarAndOffer())
* using the offers that are currently in the user's offers table (does not reset the offers table or re-call
* senso Api to check whether loan offers are approved, since these are already assumed to be approved
* in a previous search query with the same search params).
*/
private List<Map<String, Object>> searchCarsWithOfferOldParams(
EntUser user,
Expand All @@ -248,12 +248,12 @@ private List<Map<String, Object>> searchCarsWithOfferOldParams(
EntOffer offer = new EntOffer();
offer.loadFromMap(offerMap);

Map<String, Object> carMap = tableCars.getCarById(Integer.toString(offer.getCarId()));
Map<String, Object> carMap = tableCars.getCarById(offer.getCarId());
EntCar car = new EntCar();
car.loadFromMap(carMap);

// Merge car and offer to create a carAndOfferInfoMap
Map<String, Object> carAndOfferInfoMap = mergeCarAndOffer(car, offer);
Map<String, Object> carAndOfferInfoMap = MergeCarAndOffer.mergeCarAndOffer(car, offer);
carAndOfferInfoMaps.add(carAndOfferInfoMap);
}
// Return a sorted version of carAndOfferInfoMaps according to the sort settings
Expand All @@ -276,7 +276,7 @@ private EntOffer createOfferFromUserAndCar (

// Query senso Api for this car and user information
Map<String, Object> queryResult = this.sensoApi.getLoanOffer(
Double.toString(car.getPrice()), // loanAmount (TODO: verify correct)
Double.toString(car.getPrice()), // loanAmount
Integer.toString(user.getCreditScore()), // creditScore
Double.toString(budgetMo), // budget
car.getBrand(), // vehicleMake
Expand All @@ -291,7 +291,7 @@ private EntOffer createOfferFromUserAndCar (
// offer entity. Otherwise, there was no loan offer for this car and the current search params, so return null.
if (queryResult.get("status").equals(200)) {
Map queryBody = (Map) queryResult.get("body");
int carId = car.getId();
int carId = car.getCarId();
double loanAmount = (double) queryBody.get("amount");
double capitalSum = (double) queryBody.get("capitalSum");
double interestSum = (double) queryBody.get("interestSum");
Expand Down Expand Up @@ -326,33 +326,6 @@ private EntOffer createOfferFromUserAndCar (
}
}

/**
* Merge car and offer entities into a single map.
*/
private Map<String, Object> mergeCarAndOffer(EntCar car, EntOffer offer) {
return new HashMap<>() {{
// Car info
put("car_id", car.getId());
put("brand", car.getBrand());
put("model", car.getModel());
put("year", car.getYear());
put("price", car.getPrice());
put("kms", car.getKms());
// Offer info
put("offer_id", offer.getOfferId());
put("loan_amount", offer.getLoanAmount());
put("capital_sum", offer.getCapitalSum());
put("interest_sum", offer.getInterestSum());
put("total_sum", offer.getTotalSum());
put("apr", offer.getInterestRate()); // TODO: why "apr"
put("term_length", offer.getTermMo()); // TODO: clean term_length/term_mo and all these terms
put("installments", offer.getInstallments());
put("claimed", offer.isClaimed());
// Computed stuff
put("payment_mo", offer.getTotalSum() / offer.getTermMo());
}};
}

/**
* Sort a list of maps according to key and sortAsc. We use the corresponding value of key for each map
* (which is assumed to be castable to double) to sort the list of maps. sortAsc defines whether we sort
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/tech/autodirect/api/utils/MergeCarAndOffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package tech.autodirect.api.utils;

import tech.autodirect.api.entities.EntCar;
import tech.autodirect.api.entities.EntOffer;

import java.util.HashMap;
import java.util.Map;

public class MergeCarAndOffer {
/**
* Merge car and offer entities into a single map.
*/
public static Map<String, Object> mergeCarAndOffer(EntCar car, EntOffer offer) {
return new HashMap<>() {{
// Car info
put("car_id", car.getCarId());
put("brand", car.getBrand());
put("model", car.getModel());
put("year", car.getYear());
put("price", car.getPrice());
put("kms", car.getKms());
// Offer info
put("offer_id", offer.getOfferId());
put("loan_amount", offer.getLoanAmount());
put("capital_sum", offer.getCapitalSum());
put("interest_sum", offer.getInterestSum());
put("total_sum", offer.getTotalSum());
put("interest_rate", offer.getInterestRate());
put("term_mo", offer.getTermMo());
put("installments", offer.getInstallments());
put("claimed", offer.isClaimed());
// Computed stuff
put("payment_mo", offer.getTotalSum() / offer.getTermMo());
}};
}
}
Loading

0 comments on commit 53d198f

Please sign in to comment.