Skip to content

Commit

Permalink
Updated APIToDatabaseService
Browse files Browse the repository at this point in the history
Added insert statements for Category and Charity_Categories
  • Loading branch information
roaraf committed Apr 18, 2026
1 parent d3ac180 commit e54f602
Showing 1 changed file with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -38,8 +39,7 @@ public void addAPIDataToTable(List<Charity> charities) {
Connection conn = null;
try {
conn = connection.getMySqlConnection();
conn.setAutoCommit(
false);
conn.setAutoCommit(false);

String sql1 =
"""
Expand All @@ -63,8 +63,21 @@ INSERT INTO CharityVanity (UUID_charity, charity_name, charity_link, description
logoBlob = VALUES(logoBlob);
""";

String sql3 = "INSERT IGNORE INTO Categories (category) VALUES (?)";

String sql4 = "SELECT category_id FROM Categories WHERE category = ?";

String sql5 =
"""
INSERT IGNORE INTO Charity_Categories (Categories_category_id, Charities_UUID_charities)
VALUES (?, ?)
""";

try (PreparedStatement ps1 = conn.prepareStatement(sql1);
PreparedStatement ps2 = conn.prepareStatement(sql2)) {
PreparedStatement ps2 = conn.prepareStatement(sql2);
PreparedStatement ps3 = conn.prepareStatement(sql3);
PreparedStatement ps4 = conn.prepareStatement(sql4);
PreparedStatement ps5 = conn.prepareStatement(sql5)) {

for (Charity charity : charities) {
String uuid;
Expand Down Expand Up @@ -94,13 +107,39 @@ INSERT INTO CharityVanity (UUID_charity, charity_name, charity_link, description
ps2.setString(6, charity.getKeyValues());
ps2.setBytes(7, charity.getLogoBlob());
ps2.executeUpdate();

if (charity.getCategory() != null) {
for (String categoryRaw : charity.getCategory()) {
if (categoryRaw == null) continue;

String category = categoryRaw.toLowerCase().trim();

if (category.isEmpty()) continue;

ps3.setString(1, category);
ps3.executeUpdate();

ps4.setString(1, category);

try (ResultSet rs = ps4.executeQuery()) {
if (rs.next()) {
int categoryId = rs.getInt("category_id");

ps5.setInt(1, categoryId);
ps5.setString(2, uuid);
ps5.executeUpdate();
}
}
}
}
}

} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}

// Integrity Check
// Integrity Check
String createTemp =
"""
CREATE TEMPORARY TABLE temp_api_charities (
Expand Down Expand Up @@ -141,8 +180,22 @@ AND NOT EXISTS (
);
""";

try (PreparedStatement ps = conn.prepareStatement(deleteSql)) {
ps.executeUpdate();
String deleteUnusedCategoriesSql =
"""
DELETE FROM Categories c
WHERE NOT EXISTS (
SELECT 1
FROM Charity_Categories cc
WHERE cc.Categories_category_id = c.category_id
);
""";

try (PreparedStatement ps1 = conn.prepareStatement(deleteSql);
PreparedStatement ps2 = conn.prepareStatement(deleteUnusedCategoriesSql)) {

ps1.executeUpdate();
ps2.executeUpdate();
}

conn.commit();
Expand Down

0 comments on commit e54f602

Please sign in to comment.