-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
290 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Build and deploy docker image | ||
run-name: ${{ github.actor }} | ||
on: | ||
push: | ||
branches: | ||
- main | ||
jobs: | ||
build-and-deploy-docker-image: | ||
runs-on: [ubuntu-latest] | ||
steps: | ||
- | ||
name: Login to Docker Hub | ||
uses: docker/#-action@v3 | ||
with: | ||
username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
- | ||
name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
- | ||
name: Build and push | ||
uses: docker/build-push-action@v6 | ||
with: | ||
push: true | ||
tags: ${{ secrets.DOCKERHUB_USERNAME }}/user_query_handling:latest, ${{ secrets.DOCKERHUB_USERNAME }}/user_query_handling:1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,5 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
/.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM openjdk:24 | ||
COPY . /usr/src/app | ||
WORKDIR /usr/src/app | ||
RUN chmod -R 777 ./ | ||
RUN ./mvnw package -DskipTests | ||
ENTRYPOINT ["java","-jar","target/user_query_handling-1.0.0.jar"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# token_registration | ||
# user_query_handling | ||
to build run docker build -t leetcode-rs/user_query_handling . | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/main/java/com/leetcoders/token_registration/utils/EnvironmentHandler.java
This file was deleted.
Oops, something went wrong.
92 changes: 0 additions & 92 deletions
92
src/main/java/com/leetcoders/token_registration/utils/PostgresHandler.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...tration/TokenRegistrationApplication.java → ...andling/TokenRegistrationApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/com/leetcoders/user_query_handling/utils/CassandraHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.leetcoders.user_query_handling.utils; | ||
|
||
import com.datastax.driver.core.*; | ||
import com.leetcoders.user_query_handling.web.LeetCodeQuestionMarking; | ||
import com.leetcoders.user_query_handling.web.YoutubeVideoMarking; | ||
import org.springframework.lang.NonNull; | ||
|
||
import java.util.List; | ||
|
||
public class CassandraHandler { | ||
static final String KEYSPACE = "leetcode_rs"; | ||
static final String UPDATE_LEET_CODE_QUESTION = """ | ||
UPDATE user_questions SET manually_marked_by_user = ? WHERE user_name = ? AND question_name = ? IF EXISTS; | ||
"""; | ||
static final String UPDATE_YOUTUBE_VIDEO = """ | ||
UPDATE user_videos SET watched = ? WHERE user_name = ? AND video_name = ? IF EXISTS; | ||
"""; | ||
final PreparedStatement updateLeetCodeQuestionStatement; | ||
final PreparedStatement updateYoutubeVideoStatement; | ||
final Cluster dbCluster; | ||
final Session dbSession; | ||
|
||
public CassandraHandler(@NonNull List<String> cassandraUrls) { | ||
Cluster.Builder connectionBuilder = Cluster.builder(); | ||
cassandraUrls.forEach(connectionBuilder::addContactPoint); | ||
this.dbCluster = connectionBuilder.build(); | ||
this.dbSession = this.dbCluster.connect(KEYSPACE); | ||
updateLeetCodeQuestionStatement = dbSession.prepare(UPDATE_LEET_CODE_QUESTION); | ||
updateYoutubeVideoStatement = dbSession.prepare(UPDATE_YOUTUBE_VIDEO); | ||
} | ||
|
||
public boolean updateLeetCodeQuestion(LeetCodeQuestionMarking details) { | ||
var res = this.dbSession.execute(updateLeetCodeQuestionStatement.bind( | ||
details.user_marked(), | ||
details.user_name(), | ||
details.question_name())); | ||
return !res.all().isEmpty(); | ||
} | ||
|
||
public boolean updateYoutubeVideo(YoutubeVideoMarking details) { | ||
ResultSet res = this.dbSession.execute(updateYoutubeVideoStatement.bind( | ||
details.user_marked(), | ||
details.user_name(), | ||
details.video_name())); | ||
return !res.all().isEmpty(); | ||
|
||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/leetcoders/user_query_handling/utils/EnvironmentHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.leetcoders.user_query_handling.utils; | ||
import io.github.cdimascio.dotenv.Dotenv; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class EnvironmentHandler { | ||
public static String getPGServerIP() { | ||
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); | ||
return dotenv.get("PG_SERVER_IP"); | ||
} | ||
public static Integer getPGServerPort() { | ||
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); | ||
return Integer.valueOf(dotenv.get("PG_SERVER_PORT")); | ||
} | ||
public static String getPGUsername() { | ||
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); | ||
return dotenv.get("PG_SERVER_USERNAME"); | ||
} | ||
|
||
public static String getPGPassword() { | ||
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); | ||
return dotenv.get("PG_SERVER_PASSWORD"); | ||
} | ||
|
||
public static List<String> getCassandraUrls() { | ||
Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); | ||
String urls = dotenv.get("CASSANDRA_URL"); | ||
return Arrays.stream(urls.split(";")).toList(); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/leetcoders/user_query_handling/utils/PostgresHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.leetcoders.user_query_handling.utils; | ||
|
||
import com.leetcoders.user_query_handling.web.UserDetails; | ||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.sql.Timestamp; | ||
|
||
|
||
public class PostgresHandler { | ||
private static final String UPDATE_USER_DETAILS = """ | ||
INSERT INTO user_details (name, access_key, csrf_token, companies, solved_questions, time_to_update, access_key_expiration, being_processed) | ||
VALUES | ||
( | ||
?, | ||
?, | ||
?, | ||
?, | ||
0, | ||
now() AT TIME ZONE 'UTC', | ||
?, | ||
FALSE | ||
) | ||
ON CONFLICT (name) | ||
DO | ||
UPDATE | ||
SET name=?, access_key=?, csrf_token=?, companies=?, time_to_update=now() AT TIME ZONE 'UTC', access_key_expiration=?, being_processed=FALSE; | ||
"""; | ||
private static final Logger logger = LoggerFactory.getLogger(PostgresHandler.class); | ||
private static final String DB_NAME = "leetcode-rs"; | ||
private static HikariDataSource ds; | ||
|
||
public PostgresHandler(String serverName, int serverPort, String username, String password) { | ||
HikariConfig config = new HikariConfig(); | ||
config.setJdbcUrl(String.format("jdbc:postgresql://%s:%d/%s", serverName, serverPort, DB_NAME)); | ||
config.setUsername(username); | ||
config.setPassword(password); | ||
config.setMaximumPoolSize(10); | ||
config.addDataSourceProperty("cachePrepStmts", "true"); | ||
config.addDataSourceProperty("prepStmtCacheSize", "250"); | ||
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); | ||
config.addDataSourceProperty("connectionTimeout", "30000"); | ||
ds = new HikariDataSource(config); | ||
logger.info("Opened postgres connection pool successfully"); | ||
} | ||
|
||
public boolean updateUserDetails(UserDetails userDetails) { | ||
LocalDate localDate = LocalDate.parse(userDetails.expirationTime()); | ||
Instant instant = localDate.atStartOfDay().atZone(ZoneId.of("UTC")).toInstant(); | ||
Timestamp timestamp = Timestamp.from(instant); | ||
|
||
try (Connection connection = ds.getConnection(); var statement = connection.prepareStatement(UPDATE_USER_DETAILS)) { | ||
statement.setString(1, userDetails.name()); | ||
statement.setString(6, userDetails.name()); | ||
statement.setString(2, userDetails.token()); | ||
statement.setString(7, userDetails.token()); | ||
statement.setString(3, userDetails.csrfToken()); | ||
statement.setString(8, userDetails.csrfToken()); | ||
java.sql.Array companiesArray = connection.createArrayOf("text", userDetails.companies().toArray()); | ||
statement.setArray(4, companiesArray); | ||
statement.setArray(9, companiesArray); | ||
statement.setTimestamp(5, timestamp); | ||
statement.setTimestamp(10, timestamp); | ||
|
||
statement.execute(); | ||
logger.info("User successfully updated"); | ||
return true; | ||
} catch (SQLException e) { | ||
logger.error("Failed updating user", e); | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.