Skip to content

Commit

Permalink
Merge pull request #76 from CS196Illinois/matching_algo
Browse files Browse the repository at this point in the history
matching with SQL
  • Loading branch information
adhi-thirumala authored Dec 8, 2024
2 parents d7f1083 + 1706985 commit 1f70514
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Matching {
private List<User> matchList = new ArrayList<>();
private Map<String, Integer> ranks = new HashMap<>();

public boolean fillMatchList() {
matchList.clear();
Expand All @@ -19,6 +24,18 @@ public boolean fillMatchList() {
return true;
}

public boolean newFillMatchList(int minAge, int maxAge, String[] ranks, String[] roles) {
matchList.clear();
PostgreSQLController pgController = new PostgreSQLController();
try {
matchList = pgController.filterWithSQL(minAge, maxAge, ranks, roles);
} catch (SQLException e) {
System.out.println(e.getMessage());
return false;
}
return true;
}

public List<User> getMatchList() {
return matchList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,63 @@ public User getUser(String discordID) {
throw new IllegalArgumentException("User not found");
}

public List<User> filterWithSQL(int minAge, int maxAge, String[] ranks, String[] roles) throws SQLException {
List<User> users = new ArrayList<>();
Connection connection = null;
try {
connection = DriverManager.getConnection(url, props);
connection.setAutoCommit(false);

String sql = "SELECT * FROM private JOIN public ON private.discord_id = public.discord_id WHERE EXTRACT(YEAR FROM age(private.dob)) BETWEEN ? AND ? AND public.rank = ANY(?) AND public.roles && ?";

try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setInt(1, minAge);
stmt.setInt(2, maxAge);
stmt.setArray(3, connection.createArrayOf("text", ranks));
stmt.setArray(4, connection.createArrayOf("text", roles));

ResultSet rs = stmt.executeQuery();
while (rs.next()) {
PrivateUser privateUser = new PrivateUser(
rs.getString("discord_id"),
rs.getDate("dob").toString(),
rs.getArray("one_way_matched") != null ? (String[]) rs.getArray("one_way_matched").getArray() : new String[0],
rs.getArray("two_way_matched") != null ? (String[]) rs.getArray("two_way_matched").getArray() : new String[0]
);

PublicUser publicUser = new PublicUser(
rs.getString("discord_id"),
rs.getString("riot_id"),
rs.getString("first_name"),
rs.getString("last_name"),
(String[]) rs.getArray("pronouns").getArray(),
rs.getString("description"),
(String[]) rs.getArray("roles").getArray(),
rs.getString("rank"),
rs.getString("image")
);

users.add(new User(privateUser, publicUser));
}
connection.commit();
} catch (SQLException e) {
connection.rollback();
throw e;
}
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return users;
}

public List<User> getUsers() throws SQLException {
List<User> users = new ArrayList<>();
Connection connection = null;
Expand Down

0 comments on commit 1f70514

Please sign in to comment.