Skip to content

Commit

Permalink
Fix: Expanded database to uphold new scraper data
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBalunan committed Apr 13, 2026
1 parent 20031eb commit 60deef4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
Binary file modified docs/SqlDatabase/ER-DiagramFile.mwb
Binary file not shown.
Binary file modified docs/SqlDatabase/ER-DiagramFile.mwb.bak
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void init() {
try {
HttpClient https = HttpClient.newHttpClient();
APICharityScraper scraper = new APICharityScraper(https);

DatabaseConnection conn = new DatabaseConnection();
APIToDatabaseService db = new APIToDatabaseService(conn);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,14 @@ public void createTables() {
-- -----------------------------------------------------
-- Table `HelpMeHelp`.`Charities`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `apbaluna`.`Charities` (
`UUID_charities` CHAR(36) NOT NULL,
`org_number` VARCHAR(255) NOT NULL,
`charity_name` VARCHAR(255) NOT NULL,
`charity_description` VARCHAR(255) NOT NULL,
`charity_link` VARCHAR(255) NOT NULL,
`pre_approved` TINYINT NOT NULL,
`status` VARCHAR(255) NOT NULL,
PRIMARY KEY (`UUID_charities`),
UNIQUE INDEX `org_number_UNIQUE` (`org_number` ASC) VISIBLE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `apbaluna`.`Charities` (
`UUID_charities` CHAR(36) NOT NULL,
`org_number` VARCHAR(255) NOT NULL,
`pre_approved` TINYINT NOT NULL,
`status` VARCHAR(255) NOT NULL,
PRIMARY KEY (`UUID_charities`),
UNIQUE INDEX `org_number_UNIQUE` (`org_number` ASC) VISIBLE)
ENGINE = InnoDB;
""";
String donationsTable =
"""
Expand Down Expand Up @@ -118,14 +114,14 @@ PRIMARY KEY (`UUID_User`))
-- Table `apbaluna`.`Settings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `apbaluna`.`Settings` (
`User_UUID_User` CHAR(36) NOT NULL,
`UUID_user` CHAR(36) NOT NULL,
`isAnonymous` TINYINT NOT NULL,
`language` VARCHAR(45) NOT NULL,
`lightmode` TINYINT NOT NULL,
PRIMARY KEY (`User_UUID_User`),
INDEX `fk_Settings_User1_idx` (`User_UUID_User` ASC) VISIBLE,
INDEX `fk_Settings_User1_idx` (`UUID_user` ASC) VISIBLE,
CONSTRAINT `fk_Settings_User1`
FOREIGN KEY (`User_UUID_User`)
FOREIGN KEY (`UUID_user`)
REFERENCES `apbaluna`.`User` (`UUID_User`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
Expand Down Expand Up @@ -253,6 +249,17 @@ FOREIGN KEY (`User_UUID_User`)
ON UPDATE NO ACTION)
ENGINE = InnoDB;
""";
String charityVanityTable =
"""
CREATE TABLE IF NOT EXISTS `apbaluna`.`Charities` (
`UUID_charities` CHAR(36) NOT NULL,
`org_number` VARCHAR(255) NOT NULL,
`pre_approved` TINYINT NOT NULL,
`status` VARCHAR(255) NOT NULL,
PRIMARY KEY (`UUID_charities`),
UNIQUE INDEX `org_number_UNIQUE` (`org_number` ASC) VISIBLE)
ENGINE = InnoDB;
""";

try (Connection conn = connection.getMySqlConnection();
Statement s = conn.createStatement()) {
Expand All @@ -266,6 +273,7 @@ FOREIGN KEY (`User_UUID_User`)
s.execute(categoriesTable);
s.execute(charityCategoriesTable);
s.execute(charityUserTable);
s.execute(charityVanityTable);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error creating table.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class APIToDatabaseService {
private final DatabaseConnection connection;

/**
* Contractor for DatabaseManager. It uses a DatabaseConnection object that contains a connection
* Contractor for APIToDatabaseService. It uses a DatabaseConnection object that contains a connection
* credentials.
*
* @param connection
Expand All @@ -23,29 +23,33 @@ public APIToDatabaseService(DatabaseConnection connection) {

/**
* This method is used to verify the integrity of the data in the {@code charities} table and to
* update it based on the data retrieved from the IK API. The param charities are retrieved from
* update it based on the data retrieved from the IK API and the charity's URL.
* The param charities are retrieved from
* the IK API through the APICharityData class. Called in initialize method in
* HmHApplication.java, which is the main class of the application, to ensure that the data is up
* to date when the application starts. Uses a a temp table to ensure that the data in the
* database is consistent with the data from the API.
* to date when the application starts. Uses a temp table to ensure that the data in the database
* is consistent with the data from the API.
* <p>Uses a URLScraper object to get data not contained in the API, and static methods from
* LogoDownloader to get the charity's logo as a blob.</p>
*
* @param charities
* @param charities a list of {@code Charity} objects to add to the database
*/
public void addAPIDataToTable(List<Charity> charities) {
Connection conn = null;
int charityCounter = 0;
try {
conn = connection.getMySqlConnection();
conn.setAutoCommit(false);
String sql_query =
"""
INSERT INTO Charities (UUID_charities, org_number, charity_name, charity_description, charity_link, pre_approved, status)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
charity_name = VALUES(charity_name),
charity_link = VALUES(charity_link),
pre_approved = VALUES(pre_approved),
status = VALUES(status)
""";
INSERT INTO Charities (UUID_charities, org_number, charity_name, charity_description, charity_link, pre_approved, status)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
charity_name = VALUES(charity_name),
charity_link = VALUES(charity_link),
pre_approved = VALUES(pre_approved),
status = VALUES(status)
""";

try (PreparedStatement ps = conn.prepareStatement(sql_query)) {
for (Charity charity : charities) {
Expand Down

0 comments on commit 60deef4

Please sign in to comment.