Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring boot #23

Closed
wants to merge 12 commits into from
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ dependencies {
exclude module: 'opus-java'
}

implementation 'org.springframework.boot:spring-boot-starter-web:3.0.2'

implementation 'me.xdrop:fuzzywuzzy:1.4.0'

implementation 'org.postgresql:postgresql:42.5.1'
Expand Down
40 changes: 35 additions & 5 deletions gradle.lockfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
# This is a Gradle generated file for dependency locking.
# Manual edits can break the build and are not advised.
# This file is expected to be part of source control.
com.fasterxml.jackson.core:jackson-annotations:2.13.2=runtimeClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-core:2.13.2=runtimeClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-databind:2.13.2.2=runtimeClasspath,testRuntimeClasspath
com.fasterxml.jackson:jackson-bom:2.13.2=runtimeClasspath,testRuntimeClasspath
ch.qos.logback:logback-classic:1.4.5=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
ch.qos.logback:logback-core:1.4.5=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-annotations:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-core:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.core:jackson-databind:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson.module:jackson-module-parameter-names:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.fasterxml.jackson:jackson-bom:2.14.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.neovisionaries:nv-websocket-client:2.14=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.squareup.okhttp3:okhttp:4.9.3=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
com.squareup.okio:okio:2.8.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.github.cdimascio:dotenv-java:2.3.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.micrometer:micrometer-commons:1.10.3=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
io.micrometer:micrometer-observation:1.10.3=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
jakarta.annotation:jakarta.annotation-api:2.1.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
me.xdrop:fuzzywuzzy:1.4.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
net.dv8tion:JDA:5.0.0-beta.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
net.sf.trove4j:trove4j:3.0.3=runtimeClasspath,testRuntimeClasspath
org.apache.commons:commons-collections4:4.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.logging.log4j:log4j-api:2.19.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.logging.log4j:log4j-to-slf4j:2.19.0=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.tomcat.embed:tomcat-embed-core:10.1.5=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.tomcat.embed:tomcat-embed-el:10.1.5=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apache.tomcat.embed:tomcat-embed-websocket:10.1.5=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
org.checkerframework:checker-qual:3.5.0=runtimeClasspath,testRuntimeClasspath
org.jetbrains.kotlin:kotlin-stdlib-common:1.4.10=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
Expand All @@ -25,6 +38,23 @@ org.junit.platform:junit-platform-engine:1.9.0=testRuntimeClasspath
org.junit:junit-bom:5.9.0=testCompileClasspath,testRuntimeClasspath
org.opentest4j:opentest4j:1.2.0=testCompileClasspath,testRuntimeClasspath
org.postgresql:postgresql:42.5.1=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-api:2.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:jul-to-slf4j:2.0.6=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-api:2.0.6=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.slf4j:slf4j-simple:2.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework.boot:spring-boot-autoconfigure:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework.boot:spring-boot-starter-json:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework.boot:spring-boot-starter-logging:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework.boot:spring-boot-starter-tomcat:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework.boot:spring-boot-starter-web:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework.boot:spring-boot-starter:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework.boot:spring-boot:3.0.2=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-aop:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-beans:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-context:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-core:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-expression:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-jcl:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-web:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.springframework:spring-webmvc:6.0.4=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
org.yaml:snakeyaml:1.33=compileClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
empty=annotationProcessor,shadow,testAnnotationProcessor
8 changes: 8 additions & 0 deletions src/main/java/com/acmcsuf/triggers/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Bot
{

Expand Down Expand Up @@ -71,5 +75,9 @@ public static void main( String[] args ) throws InterruptedException
log.error( "Failed to initialize database" );
e.printStackTrace();
}

SpringApplication.run(BotController.class, args);
log.info("Spring Boot application started.");

}
}
35 changes: 35 additions & 0 deletions src/main/java/com/acmcsuf/triggers/BotController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.acmcsuf.triggers;
import java.sql.SQLException;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BotController {

// TODO: Add authentication process (JWT?)

@GetMapping("/api/ping")
public String ping() {
return "Hello, world!";
}

@PostMapping("/api/sync")
public ResponseEntity<String> sync(@RequestParam("user_id") String userId) {

// Sync data from database
try {
Database.syncUserData(userId, Trigger.triggerMap, Trigger.triggerToggle);
} catch (SQLException e) {
e.printStackTrace();
return new ResponseEntity<>("Error syncing data for user_id: " + userId, HttpStatus.INTERNAL_SERVER_ERROR);
}

return new ResponseEntity<>("Data synced successfully for user_id: " + userId, HttpStatus.OK);
}

}
137 changes: 137 additions & 0 deletions src/main/java/com/acmcsuf/triggers/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,92 @@ public static void appendPhrase( Member member, String phrase, Connection connec
}
}

/**
* Appends trigger phrase
*
* <p>
* Appends new phrase to user's trigger list if it is not already in the list and syncs with database afterwards
* </p>
*
* @param userID Event member user ID
* @param phrase Phrase to add
* @param connection Connection object
* @param triggerMap Map of user IDs to trigger phrases
* @param triggerToggle Map of user IDs to trigger toggle
* @throws SQLException On failure to interact with database
*/
public static void appendPhrase( String userID, String phrase, Connection connection,
HashMap<String, LinkedHashSet<String>> triggerMap,
HashMap<String, Boolean> triggerToggle ) throws SQLException
{

String sql = """
UPDATE triggers
SET phrase = array_append(phrase, ?::text)
WHERE user_id =""" + userID;

try
{
PreparedStatement preparedStatement = connection.prepareStatement( sql );
preparedStatement.setString( 1, phrase );
preparedStatement.executeUpdate();
}
catch ( PSQLException e )
{
log.error( "Error appending phrase to database", e );
}
try
{
syncUserData( connection, userID, triggerMap, triggerToggle );
}
catch ( PSQLException e )
{
log.error( "Error syncing user data", e );
}
}

/**
* Deletes trigger phrase
*
* <p>
* Deletes phrase from user's trigger list and syncs with database afterwards
* </p>
*
* @param userID Event member user ID
* @param phrase Phrase to delete
* @throws SQLException On failure to interact with database
*/
public static void deletePhrase( String userID, String phrase,
HashMap<String, LinkedHashSet<String>> triggerMap,
HashMap<String, Boolean> triggerToggle ) throws SQLException
{

String sql = """
UPDATE triggers
SET phrase = array_remove(phrase, ?::text)
WHERE user_id =""" + userID;

try ( Connection conn = getConnect() )
{
PreparedStatement preparedStatement = conn.prepareStatement( sql );
preparedStatement.setString( 1, phrase );
preparedStatement.executeUpdate();

try
{
syncUserData( conn, userID, triggerMap, triggerToggle );
}
catch ( PSQLException e )
{
log.error( "Error syncing user data", e );
}
}
catch ( PSQLException e )
{
log.error( "Error deleting phrase from database", e );
}
}

/**
* Deletes trigger phrase
*
Expand Down Expand Up @@ -373,6 +459,57 @@ public static void syncUserData( Member member, HashMap<String, LinkedHashSet<St
}
}

/**
* Sync member's in-memory trigger with database
*
* @param userID Event member user ID
* @param triggerMap In-memory trigger map
* @param triggerToggle In-memory trigger toggle map
* @throws SQLException On failure to interact with database
*/
public static void syncUserData( String userID, HashMap<String, LinkedHashSet<String>> triggerMap,
HashMap<String, Boolean> triggerToggle ) throws SQLException
{
String sql = String.format( """
SELECT * FROM triggers
WHERE user_id = %s;""", userID );

try ( Connection conn = getConnect() )
{
ResultSet set = conn.createStatement().executeQuery( sql );

if ( set.next() )
{
insertData( triggerMap, triggerToggle, userID, set );
}
}
}

/**
* Sync member's in-memory trigger with database
*
* @param userID Event member user ID
* @param member Event member
* @param triggerMap In-memory trigger map
* @param triggerToggle In-memory trigger toggle map
* @throws SQLException On failure to interact with database
*/
public static void syncUserData( Connection connection, String userID,
HashMap<String, LinkedHashSet<String>> triggerMap,
HashMap<String, Boolean> triggerToggle ) throws SQLException
{
String sql = String.format( """
SELECT * FROM triggers
WHERE user_id = %s;""", userID );

ResultSet set = connection.createStatement().executeQuery( sql );

if ( set.next() )
{
insertData( triggerMap, triggerToggle, userID, set );
}
}

/**
* Sync member's in-memory trigger with database
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/acmcsuf/triggers/Trigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public Trigger( List<String> authorizedRoleIDs )
List<String> authorizedRoleIDs;

// Discord member ID : Set of trigger phrases
HashMap<String, LinkedHashSet<String>> triggerMap = new HashMap<>();
public static HashMap<String, LinkedHashSet<String>> triggerMap = new HashMap<>();

// Discord member ID : Trigger Activation (true = activated, false = deactivated)
HashMap<String, Boolean> triggerToggle = new HashMap<>();
public static HashMap<String, Boolean> triggerToggle = new HashMap<>();

int min = 0;
int max = 5;
Expand Down