Skip to content

Commit

Permalink
Merge pull request #95 from TLI-Group-1/better-tables
Browse files Browse the repository at this point in the history
Better tables
  • Loading branch information
sammdu authored Dec 9, 2021
2 parents 0cc7a83 + 8eb0065 commit 3e1f599
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 154 deletions.
135 changes: 128 additions & 7 deletions src/main/java/tech/autodirect/api/database/Table.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package tech.autodirect.api.database;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.sql.*;
import java.util.*;

public class Table {
/**
Expand All @@ -30,4 +28,127 @@ public List<Map<String, Object>> resultSetToList(ResultSet rs) throws SQLExcepti
}
return list;
}

/**
* Get an entry with id from the database using dbConn, schemaName, and tableName.
*
* entName is the name of the entity (like "user" or "car").
*/
public List<Map<String, Object>> getAllEntries(
String schemaName,
String tableName,
Connection dbConn
) throws SQLException {
// Construct and execute a prepared SQL statement selecting all entries
PreparedStatement stmt = dbConn.prepareStatement(
"SELECT * FROM " + schemaName + "." + tableName
);
ResultSet rs = stmt.executeQuery();
List<Map<String, Object>> maps = resultSetToList(rs);
stmt.close();
return maps;
}

/**
* Get an entry with id from the database using dbConn, schemaName, and tableName.
*
* entName is the name of the entity (like "user" or "car").
*/
public Map<String, Object> getEntryById(
Object id,
String schemaName,
String tableName,
Connection dbConn,
String entName
) throws SQLException, ResponseStatusException {
if (!checkEntryExists(id, schemaName, tableName, dbConn, entName)) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, entName + " not found"
);
}

// construct a prepared SQL statement selecting the specified entry
PreparedStatement stmt = dbConn.prepareStatement(
"SELECT * FROM " + schemaName + "." + tableName + " WHERE " + entName + "_id = ?;"
);
stmt.setObject(1, id);

ResultSet rs = stmt.executeQuery();
List<Map<String, Object>> rsList = resultSetToList(rs);
stmt.close();
if (rsList.size() == 0) {
return Collections.emptyMap();
} else {
return rsList.get(0);
}
}

/**
* Remove an entry with id from the database using dbConn, schemaName, and tableName.
*
* entName is the name of the entity (like "user" or "car").
*/
public void removeEntryById(
Object id,
String schemaName,
String tableName,
Connection dbConn,
String entName
) throws SQLException, ResponseStatusException {
if (!checkEntryExists(id, schemaName, tableName, dbConn, entName)) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "user not found"
);
}

// construct a prepared SQL statement selecting the specified entry
PreparedStatement stmt = dbConn.prepareStatement(
"DELETE FROM " + schemaName + "." + tableName + " WHERE " + entName + "_id = ?;"
);
stmt.setObject(1, id);

// execute the above SQL statement
stmt.executeUpdate();
stmt.close();
}

/**
* Remove all entries in the database table given by dbConn, schemaName, and tableName.
*/
public void removeAllEntries(
String schemaName,
String tableName,
Connection dbConn
) throws SQLException {
PreparedStatement stmt = dbConn.prepareStatement(
"DELETE FROM " + schemaName + "." + tableName + ";"
);
stmt.executeUpdate();
stmt.close();
}

/**
* Check an entry with id exists in the database using dbConn, schemaName, and tableName.
*
* entName is the name of the entity (like "user" or "car").
*/
public boolean checkEntryExists(
Object id,
String schemaName,
String tableName,
Connection dbConn,
String entName
) throws SQLException {
// construct a prepared SQL statement selecting the specified entry
PreparedStatement stmt = dbConn.prepareStatement(
"SELECT 1 FROM " + schemaName + "." + tableName + " WHERE " + entName + "_id = ?;"
);
stmt.setObject(1, id);

// execute the above SQL statement and check whether the entry exists
ResultSet rs = stmt.executeQuery();
boolean userCount = resultSetToList(rs).size() > 0;
stmt.close();
return userCount;
}
}
45 changes: 3 additions & 42 deletions src/main/java/tech/autodirect/api/database/TableCars.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import tech.autodirect.api.interfaces.TableCarsInterface;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand All @@ -46,51 +43,15 @@ public TableCars(String dbName) throws SQLException, ClassNotFoundException {

@Override
public List<Map<String, Object>> getAllCars() throws SQLException {
// Construct and execute a prepared SQL statement selecting all cars
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT * FROM " + this.schemaName + "." + this.tableName
);
ResultSet rs = stmt.executeQuery();
List<Map<String, Object>> carMaps = resultSetToList(rs);
stmt.close();
return carMaps;
return getAllEntries(schemaName, tableName, dbConn);
}

@Override
public Map<String, Object> getCarById(String carId) throws SQLException, ResponseStatusException {
if (!checkCarExists(carId)) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "car not found"
);
}

// construct a prepared SQL statement selecting the specified car
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT * FROM " + this.schemaName + "." + this.tableName + " WHERE car_id = ?;"
);
stmt.setString(1, carId);

ResultSet rs = stmt.executeQuery();
List<Map<String, Object>> rsList = resultSetToList(rs);
stmt.close();
if (rsList.size() == 0) {
return Collections.emptyMap();
} else {
return rsList.get(0);
}
return getEntryById(carId, schemaName, tableName, dbConn, "car");
}

public boolean checkCarExists(String carId) throws SQLException {
// construct a prepared SQL statement selecting the specified car
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT 1 FROM " + this.schemaName + "." + this.tableName + " WHERE car_id = ?;"
);
stmt.setString(1, carId);

// execute the above SQL statement and check whether the car exists
ResultSet rs = stmt.executeQuery();
boolean carCount = resultSetToList(rs).size() > 0;
stmt.close();
return carCount;
return checkEntryExists(carId, schemaName, tableName, dbConn, "car");
}
}
64 changes: 5 additions & 59 deletions src/main/java/tech/autodirect/api/database/TableOffers.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,63 +124,19 @@ public int addOffer(
}

public void removeOfferByOfferId(int offerId) throws SQLException, ResponseStatusException {
if (!checkOfferExists(offerId)) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "offer not found"
);
}

// construct a prepared SQL statement deleting the specified offer
PreparedStatement stmt = this.dbConn.prepareStatement(
"DELETE FROM " + this.schemaName + "." + this.tableName + " WHERE offer_id = ?;"
);
stmt.setInt(1, offerId);

// execute and close the above SQL statement
stmt.executeUpdate();
stmt.close();
removeEntryById(offerId, schemaName, tableName, dbConn, "offer");
}

public void removeAllOffers() throws SQLException {
PreparedStatement stmt = this.dbConn.prepareStatement(
"DELETE FROM " + this.schemaName + "." + this.tableName + ";"
);
stmt.executeUpdate();
stmt.close();
removeAllEntries(schemaName, tableName, dbConn);
}

public Map<String, Object> getOfferByOfferId(int offerId) throws SQLException, ResponseStatusException {
if (!checkOfferExists(offerId)) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "offer not found"
);
}

// construct a prepared SQL statement selecting the specified offer
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT * FROM " + this.schemaName + "." + this.tableName + " WHERE offer_id = ?;"
);
stmt.setInt(1, offerId);

ResultSet rs = stmt.executeQuery();
List<Map<String, Object>> rsList = resultSetToList(rs);
stmt.close();
if (rsList.size() == 0) {
return Collections.emptyMap();
} else {
return rsList.get(0);
}
return getEntryById(offerId, schemaName, tableName, dbConn, "offer");
}

public List<Map<String, Object>> getAllOffers() throws SQLException {
// construct a SQL statement selecting all offers
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT * FROM " + this.schemaName + "." + this.tableName + ";"
);
ResultSet rs = stmt.executeQuery();
List<Map<String, Object>> offers = resultSetToList(rs);
stmt.close();
return offers;
return getAllEntries(schemaName, tableName, dbConn);
}

public List<Map<String, Object>> getClaimedOffers() throws SQLException {
Expand Down Expand Up @@ -273,17 +229,7 @@ public boolean checkTableExists(String tableName) throws SQLException {
}

public boolean checkOfferExists(int offerId) throws SQLException {
// construct a prepared SQL statement selecting the specified offer
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT 1 FROM " + this.schemaName + "." + this.tableName + " WHERE offer_id = ?;"
);
stmt.setInt(1, offerId);

// execute the above SQL statement and check whether the offer exists
ResultSet rs = stmt.executeQuery();
boolean offerCount = resultSetToList(rs).size() > 0;
stmt.close();
return offerCount;
return checkEntryExists(offerId, schemaName, tableName, dbConn, "offer");
}

}
49 changes: 3 additions & 46 deletions src/main/java/tech/autodirect/api/database/TableUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,59 +73,16 @@ public void addUser(

@Override
public Map<String, Object> getUserById(String userId) throws SQLException, ResponseStatusException {
if (!checkUserExists(userId)) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "user not found"
);
}

// construct a prepared SQL statement selecting the specified user
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT * FROM " + this.schemaName + "." + this.tableName + " WHERE user_id = ?;"
);
stmt.setString(1, userId);

ResultSet rs = stmt.executeQuery();
List<Map<String, Object>> rsList = resultSetToList(rs);
stmt.close();
if (rsList.size() == 0) {
return Collections.emptyMap();
} else {
return rsList.get(0);
}
return getEntryById(userId, schemaName, tableName, dbConn, "user");
}

@Override
public void removeUserById(String userId) throws SQLException, ResponseStatusException {
if (!checkUserExists(userId)) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "user not found"
);
}

// construct a prepared SQL statement selecting the specified user
PreparedStatement stmt = this.dbConn.prepareStatement(
"DELETE FROM " + this.schemaName + "." + this.tableName + " WHERE user_id = ?;"
);
stmt.setString(1, userId);

// execute the above SQL statement
stmt.executeUpdate();
stmt.close();
removeEntryById(userId, schemaName, tableName, dbConn, "user");
}

@Override
public boolean checkUserExists(String userId) throws SQLException {
// construct a prepared SQL statement selecting the specified user
PreparedStatement stmt = this.dbConn.prepareStatement(
"SELECT 1 FROM " + this.schemaName + "." + this.tableName + " WHERE user_id = ?;"
);
stmt.setString(1, userId);

// execute the above SQL statement and check whether the user exists
ResultSet rs = stmt.executeQuery();
boolean userCount = resultSetToList(rs).size() > 0; // TODO: cant be more than 1, right? Check?
stmt.close();
return userCount;
return checkEntryExists(userId, schemaName, tableName, dbConn, "user");
}
}

0 comments on commit 3e1f599

Please sign in to comment.