Skip to content

Commit

Permalink
Merge pull request #79 from CS196Illinois/matching_algo
Browse files Browse the repository at this point in the history
adding users to one way and two way tables
  • Loading branch information
adhi-thirumala authored Dec 8, 2024
2 parents d616207 + 052209b commit 4516610
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -74,4 +75,24 @@ public ResponseEntity<ReturnValue> login(@RequestBody String code)
pgController.createAuthRow(discordId, tr.getAccessToken(), tr.getRefreshToken())));
}
}

@PostMapping(path = "/api/v1/matching")
public ResponseEntity<List<User>> matching(@RequestParam int minAge, @RequestParam int maxAge, @RequestParam String[] ranks, @RequestParam String[] roles) throws IOException {
Matching matcher = new Matching();
boolean success = matcher.fillMatches(minAge, maxAge, ranks, roles);
if (success) {
List<User> matches = matcher.getMatchList();
return ResponseEntity.ok(matches);
} else {
return ResponseEntity.ok(new ArrayList<>());
}
}

@PostMapping(path = "/api/v1/update_matches")
public ResponseEntity<ReturnValue> updateMatches(@RequestParam String userId, @RequestParam String matchedId) {
OneAndTwoWayMatches oneAndTwoWayMatches = new OneAndTwoWayMatches();
boolean success = oneAndTwoWayMatches.updateMatches(userId, matchedId);
return ResponseEntity.ok(new ReturnValue(success, ""));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Main {
public static void main(String[] args) {
Dotenv dotenv = Dotenv.load();
System.setProperty(
"SPRING_DATASOURCE_URL", "jdbc:postgresql://" + dotenv.get("PGURL") + "/main");
"SPRING_DATASOURCE_URL", "jdbc:postgresql://" + dotenv.get("PGURL") + "/main");
System.setProperty("SPRING_DATASOURCE_USERNAME", dotenv.get("PGUSER"));
System.setProperty("SPRING_DATASOURCE_PASSWORD", dotenv.get("PGPASSWORD"));
SpringApplication.run(Main.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

public class Matching {
private List<User> matchList = new ArrayList<>();
private final String sessionToken;

public Matching(String sessionToken) {
this.sessionToken = sessionToken;
}


public void newFillMatchList() {
matchList.clear();
PostgreSQLController pgController = new PostgreSQLController();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.server;

public class OneAndTwoWayMatches {

public boolean updateMatches(String discordId, String matchedId) {
PostgreSQLController pgController = new PostgreSQLController();
boolean exists = pgController.existsInOneWay(discordId, matchedId);
if (exists) {
boolean deleted = pgController.deleteOneWayMatched(discordId, matchedId);
if (!deleted) {
return false;
}
boolean added_to_two_way = pgController.updateTwoWayMatched(discordId, matchedId);
if (!added_to_two_way) {
return false;
}
} else {
boolean added = pgController.updateOneWayMatched(discordId, matchedId);
if (!added) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,24 @@ public List<User> getUsers() throws SQLException {
throw new IllegalArgumentException("Error, users not found");
}

public boolean existsInOneWay(String discordId, String matchedId) {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, props);
connection.setAutoCommit(false);
String sql = "SELECT * FROM private WHERE ? = ANY(one_way_matched) AND discord_id = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, discordId);
stmt.setString(2, matchedId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return true;
public void updateUserPreferences(UserPrefs prefs) {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, props);
connection.setAutoCommit(false);

String sqlPrivate =
"UPDATE user_preferences SET min_age = ?, max_age = ?, ranks = ?, roles = ? WHERE session_token = ?";
try (PreparedStatement stmtPrivate = connection.prepareStatement(sqlPrivate)) {
Expand All @@ -395,6 +408,7 @@ public void updateUserPreferences(UserPrefs prefs) {
connection.rollback();
throw e;
}

}
connection.commit();
} catch (SQLException e) {
Expand All @@ -412,6 +426,104 @@ public void updateUserPreferences(UserPrefs prefs) {
System.err.println(e.getMessage());
}
}
return false;

}

public boolean updateOneWayMatched(String discordId, String matchedId) {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, props);
connection.setAutoCommit(false);
String sql = "UPDATE private SET one_way_matched = one_way_matched || ARRAY[?] WHERE discord_id = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, matchedId);
stmt.setString(2, discordId);
stmt.executeUpdate();
connection.commit();
} catch (SQLException e) {
connection.rollback();
throw e;
}
} catch (SQLException e) {
System.err.println(e.getMessage());
return false;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return true;
}

public boolean deleteOneWayMatched(String discordId, String matchedId) {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, props);
connection.setAutoCommit(false);
String sql = "UPDATE private SET one_way_matched = array_remove(one_way_matched, ?) WHERE discord_id = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, discordId);
stmt.setString(2, matchedId);
stmt.executeUpdate();
connection.commit();
} catch (SQLException e) {
connection.rollback();
throw e;
}
} catch (SQLException e) {
System.err.println(e.getMessage());
return false;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return true;
}

public boolean updateTwoWayMatched(String discordId, String matchedId) {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, props);
connection.setAutoCommit(false);
String sql1 = "UPDATE private SET two_way_matched = two_way_matched || ARRAY[?] WHERE discord_id = ?";
String sql2 = "UPDATE private SET two_way_matched = two_way_matched || ARRAY[?] WHERE discord_id = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql1);
PreparedStatement stmt2 = connection.prepareStatement(sql2)) {
stmt.setString(1, matchedId);
stmt.setString(2, discordId);
stmt2.setString(1, discordId);
stmt2.setString(2, matchedId);
stmt.executeUpdate();
stmt2.executeUpdate();
connection.commit();
} catch (SQLException e) {
connection.rollback();
throw e;
}
} catch (SQLException e) {
System.err.println(e.getMessage());
return false;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
System.err.println(e.getMessage());
}
}
return true;

}

/**
Expand Down

0 comments on commit 4516610

Please sign in to comment.