Skip to content

Commit

Permalink
Add missing review system classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Jul 12, 2024
1 parent de65d66 commit 1df057a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.eternalcode.discordapp.review.database;

import com.eternalcode.discordapp.review.GitHubPullRequest;
import java.util.concurrent.CompletableFuture;

public interface GitHubReviewMentionRepository {

CompletableFuture<Void> markReviewerAsMentioned(GitHubPullRequest pullRequest, long userId);

CompletableFuture<Boolean> isMentioned(GitHubPullRequest pullRequest, long userId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.eternalcode.discordapp.review.database;

import com.eternalcode.discordapp.database.DataAccessException;
import com.eternalcode.discordapp.database.DatabaseManager;
import com.eternalcode.discordapp.database.repository.AbstractRepository;
import com.eternalcode.discordapp.review.GitHubPullRequest;
import com.j256.ormlite.table.TableUtils;
import io.sentry.Sentry;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.CompletableFuture;

public class GitHubReviewMentionRepositoryImpl extends AbstractRepository<GitHubReviewMentionWrapper, String>
implements GitHubReviewMentionRepository {

private static final Duration MENTION_INTERVAL = Duration.ofHours(12);

public GitHubReviewMentionRepositoryImpl(DatabaseManager databaseManager) {
super(databaseManager, GitHubReviewMentionWrapper.class);
}

public static GitHubReviewMentionRepository create(DatabaseManager databaseManager) {
try {
TableUtils.createTableIfNotExists(databaseManager.getConnectionSource(), GitHubReviewMentionWrapper.class);
}
catch (SQLException sqlException) {
Sentry.captureException(sqlException);
throw new DataAccessException("Failed to create table", sqlException);
}

return new GitHubReviewMentionRepositoryImpl(databaseManager);
}

@Override
public CompletableFuture<Void> markReviewerAsMentioned(GitHubPullRequest pullRequest, long userId) {
return CompletableFuture.runAsync(() -> {
GitHubReviewMentionWrapper mention =
GitHubReviewMentionWrapper.create(pullRequest.toUrl(), userId, Instant.now());
this.save(mention);
});
}

@Override
public CompletableFuture<Boolean> isMentioned(GitHubPullRequest pullRequest, long userId) {
return this.select(pullRequest.toUrl()).thenApply(mentionOptional -> {
if (mentionOptional.isEmpty()) {
return false;
}

GitHubReviewMentionWrapper mention = mentionOptional.get();
Instant lastMention = mention.getLastMention();
Instant nextMention = lastMention.plus(MENTION_INTERVAL);

return nextMention.isAfter(Instant.now());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.eternalcode.discordapp.review.database;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import java.time.Instant;

@DatabaseTable(tableName = "review_mentions")
public final class GitHubReviewMentionWrapper {

@DatabaseField(id = true)
private String pullRequest;

@DatabaseField
private long userId;

@DatabaseField
private long lastMention;

public GitHubReviewMentionWrapper() {
}

private GitHubReviewMentionWrapper(String pullRequest, long userId, long lastMention) {
this.pullRequest = pullRequest;
this.userId = userId;
this.lastMention = lastMention;
}

public static GitHubReviewMentionWrapper create(String pullRequest, long userId, Instant lastMention) {
return new GitHubReviewMentionWrapper(pullRequest, userId, lastMention.toEpochMilli());
}

public String getPullRequest() {
return this.pullRequest;
}

public long getUserId() {
return this.userId;
}

public Instant getLastMention() {
return Instant.ofEpochMilli(this.lastMention);
}

}

0 comments on commit 1df057a

Please sign in to comment.