Skip to content

Commit

Permalink
-fix verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ronen4822 committed Sep 3, 2024
1 parent 3e3afd5 commit 5122a5a
Show file tree
Hide file tree
Showing 20 changed files with 290 additions and 129 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.env
25 changes: 25 additions & 0 deletions .github/workflows/deploy.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ build/

### VS Code ###
.vscode/

/.env
6 changes: 6 additions & 0 deletions Dockerfile
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"]
4 changes: 3 additions & 1 deletion README.md
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 .

13 changes: 9 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.leetcoders</groupId>
<artifactId>token_registration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>token_registration</name>
<artifactId>user_query_handling</artifactId>
<version>1.0.0</version>
<name>user_query_handling</name>
<description>token registration service</description>
<url/>
<licenses>
Expand All @@ -27,14 +27,19 @@
<url/>
</scm>
<properties>
<java.version>17</java.version>
<java.version>22</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.leetcoders.token_registration;
package com.leetcoders.user_query_handling;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.leetcoders.token_registration.beans;
package com.leetcoders.user_query_handling.beans;

import com.leetcoders.token_registration.utils.EnvironmentHandler;
import com.leetcoders.token_registration.utils.PostgresHandler;
import com.leetcoders.user_query_handling.utils.CassandraHandler;
import com.leetcoders.user_query_handling.utils.EnvironmentHandler;
import com.leetcoders.user_query_handling.utils.PostgresHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand All @@ -12,4 +13,9 @@ public PostgresHandler postgresHandler() {
return new PostgresHandler(EnvironmentHandler.getPGServerIP(), EnvironmentHandler.getPGServerPort(),
EnvironmentHandler.getPGUsername(),EnvironmentHandler.getPGPassword());
}

@Bean
public CassandraHandler cassandraHandler() {
return new CassandraHandler(EnvironmentHandler.getCassandraUrls());
}
}
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();

}
}
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();
}
}
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;
}
}
}
Loading

0 comments on commit 5122a5a

Please # to comment.