Skip to content

Commit

Permalink
Merge pull request #81 from TLI-Group-1/code-cleanup
Browse files Browse the repository at this point in the history
cleaned up code
  • Loading branch information
sammdu authored Dec 7, 2021
2 parents 609d33d + b2c071d commit b5efbab
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 287 deletions.
270 changes: 135 additions & 135 deletions src/main/java/tech/autodirect/api/ApiEndpoints.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,142 +40,142 @@
@SpringBootApplication
// specify hosts allowed to access the AutoDirect API
@CrossOrigin(origins = {
"http://0.0.0.0",
"http://localhost",
"http://localhost:8080",
"https://autodirect.tech",
"https://api.autodirect.tech"
"http://0.0.0.0",
"http://localhost",
"http://localhost:8080",
"https://autodirect.tech",
"https://api.autodirect.tech"
})
@RestController
public class ApiEndpoints extends SpringBootServletInitializer {
// Name of the database to access
private final String dbName = "autodirect";

// Initialize Frameworks & Drivers
// TODO: Add explanation
private TableCarsInterface tableCars;
private TableUsersInterface tableUsers;
private TableOffersInterface tableOffers;
private SensoApiInterface sensoApi;
private BankApiInterface bankApi;

// Initialize Services (Use Cases)
private SvcClaimOffer svcClaimOffer;
private SvcGetClaimedOffers svcGetClaimedOffers;
private SvcGetOfferDetails svcGetOfferDetails;
private SvcMockBankApi svcMockBankApi;
private SvcSearch svcSearch;
private SvcUnclaimOffer svcUnclaimOffer;
private SvcUpdatePrincipal svcUpdatePrincipal;
private SvcUserLogin svcUserLogin;

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

public ApiEndpoints() {
try {
// Instantiate Frameworks & Drivers
tableCars = new TableCars(dbName);
tableUsers = new TableUsers(dbName);
tableOffers = new TableOffers(dbName);
sensoApi = new SensoApi();
bankApi = new BankApi();

// Instantiate Services (Use Cases)
svcClaimOffer = new SvcClaimOffer();
svcGetClaimedOffers = new SvcGetClaimedOffers();
svcGetOfferDetails = new SvcGetOfferDetails();
svcMockBankApi = new SvcMockBankApi();
svcSearch = new SvcSearch(tableCars, tableUsers, sensoApi);
svcUnclaimOffer = new SvcUnclaimOffer();
svcUpdatePrincipal = new SvcUpdatePrincipal();
svcUserLogin = new SvcUserLogin();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}

}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ApiEndpoints.class);
}

@GetMapping("/search")
public Object search(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "downpayment") String downPayment,
@RequestParam(name = "budget_mo") String budgetMo,
@RequestParam(name = "sort_by") String sortBy,
@RequestParam(name = "sort_asc") String sortAsc
) {
try {
return svcSearch.searchCars(userId, downPayment, budgetMo, sortBy, sortAsc);
} catch (IOException | InterruptedException | SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/login")
public Object login(@RequestParam(name = "user_id") String userId) {
try {
return svcUserLogin.loginUser(tableUsers, bankApi, userId);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/claimOffer")
public Object claimOffer(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "offer_id") String offerId
) {
try {
svcClaimOffer.claimOffer(tableOffers, userId, offerId);
return "";
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/unclaimOffer")
public Object unclaimOffer(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "offer_id") String offerId
) {
try {
svcUnclaimOffer.unclaimOffer(tableOffers, userId, offerId);
return "";
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/getClaimedOffers")
public Object getClaimedOffers(@RequestParam(name = "user_id") String userId) {
try {
return svcGetClaimedOffers.getClaimedOffers(tableOffers, userId);
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/getOfferDetails")
public Object getOfferDetails(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "offer_id") String offerId
) {
try {
return svcGetOfferDetails.getOfferDetails(tableOffers, userId, offerId);
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}
// Name of the database to access
private final String dbName = "autodirect";

// Initialize Frameworks & Drivers
// TODO: Add explanation
private TableCarsInterface tableCars;
private TableUsersInterface tableUsers;
private TableOffersInterface tableOffers;
private SensoApiInterface sensoApi;
private BankApiInterface bankApi;

// Initialize Services (Use Cases)
private SvcClaimOffer svcClaimOffer;
private SvcGetClaimedOffers svcGetClaimedOffers;
private SvcGetOfferDetails svcGetOfferDetails;
private SvcMockBankApi svcMockBankApi;
private SvcSearch svcSearch;
private SvcUnclaimOffer svcUnclaimOffer;
private SvcUpdatePrincipal svcUpdatePrincipal;
private SvcUserLogin svcUserLogin;

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

public ApiEndpoints() {
try {
// Instantiate Frameworks & Drivers
tableCars = new TableCars(dbName);
tableUsers = new TableUsers(dbName);
tableOffers = new TableOffers(dbName);
sensoApi = new SensoApi();
bankApi = new BankApi();

// Instantiate Services (Use Cases)
svcClaimOffer = new SvcClaimOffer();
svcGetClaimedOffers = new SvcGetClaimedOffers();
svcGetOfferDetails = new SvcGetOfferDetails();
svcMockBankApi = new SvcMockBankApi();
svcSearch = new SvcSearch(tableCars, tableUsers, sensoApi);
svcUnclaimOffer = new SvcUnclaimOffer();
svcUpdatePrincipal = new SvcUpdatePrincipal();
svcUserLogin = new SvcUserLogin();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}

}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ApiEndpoints.class);
}

@GetMapping("/search")
public Object search(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "downpayment") String downPayment,
@RequestParam(name = "budget_mo") String budgetMo,
@RequestParam(name = "sort_by") String sortBy,
@RequestParam(name = "sort_asc") String sortAsc
) {
try {
return svcSearch.searchCars(userId, downPayment, budgetMo, sortBy, sortAsc);
} catch (IOException | InterruptedException | SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/login")
public Object login(@RequestParam(name = "user_id") String userId) {
try {
return svcUserLogin.loginUser(tableUsers, bankApi, userId);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/claimOffer")
public Object claimOffer(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "offer_id") String offerId
) {
try {
svcClaimOffer.claimOffer(tableOffers, userId, offerId);
return "";
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/unclaimOffer")
public Object unclaimOffer(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "offer_id") String offerId
) {
try {
svcUnclaimOffer.unclaimOffer(tableOffers, userId, offerId);
return "";
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/getClaimedOffers")
public Object getClaimedOffers(@RequestParam(name = "user_id") String userId) {
try {
return svcGetClaimedOffers.getClaimedOffers(tableOffers, userId);
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}

@GetMapping("/getOfferDetails")
public Object getOfferDetails(
@RequestParam(name = "user_id") String userId,
@RequestParam(name = "offer_id") String offerId
) {
try {
return svcGetOfferDetails.getOfferDetails(tableOffers, userId, offerId);
} catch (SQLException e) {
e.printStackTrace();
return "Server Error!";
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/tech/autodirect/api/database/Conn.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

public class Conn {
// list of environment variables to fetch
private static String[] environmentVariables = {
private static final String[] environmentVariables = {
"AUTODIRECT_DB_HOST",
"AUTODIRECT_DB_PORT",
"AUTODIRECT_DB_SSL",
"AUTODIRECT_DB_USER",
"AUTODIRECT_DB_PASS"
};
// database parameters
private static HashMap<String, String> dbParams = new HashMap<>();
private static final HashMap<String, String> dbParams = new HashMap<>();

public static Connection getConn(String dbName)
throws MissingEnvironmentVariableException, SQLException, ClassNotFoundException {
Expand Down
Loading

0 comments on commit b5efbab

Please sign in to comment.