Skip to content

Commit 1df057a

Browse files
committed
Add missing review system classes.
1 parent de65d66 commit 1df057a

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.eternalcode.discordapp.review.database;
2+
3+
import com.eternalcode.discordapp.review.GitHubPullRequest;
4+
import java.util.concurrent.CompletableFuture;
5+
6+
public interface GitHubReviewMentionRepository {
7+
8+
CompletableFuture<Void> markReviewerAsMentioned(GitHubPullRequest pullRequest, long userId);
9+
10+
CompletableFuture<Boolean> isMentioned(GitHubPullRequest pullRequest, long userId);
11+
12+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.eternalcode.discordapp.review.database;
2+
3+
import com.eternalcode.discordapp.database.DataAccessException;
4+
import com.eternalcode.discordapp.database.DatabaseManager;
5+
import com.eternalcode.discordapp.database.repository.AbstractRepository;
6+
import com.eternalcode.discordapp.review.GitHubPullRequest;
7+
import com.j256.ormlite.table.TableUtils;
8+
import io.sentry.Sentry;
9+
import java.sql.SQLException;
10+
import java.time.Duration;
11+
import java.time.Instant;
12+
import java.util.concurrent.CompletableFuture;
13+
14+
public class GitHubReviewMentionRepositoryImpl extends AbstractRepository<GitHubReviewMentionWrapper, String>
15+
implements GitHubReviewMentionRepository {
16+
17+
private static final Duration MENTION_INTERVAL = Duration.ofHours(12);
18+
19+
public GitHubReviewMentionRepositoryImpl(DatabaseManager databaseManager) {
20+
super(databaseManager, GitHubReviewMentionWrapper.class);
21+
}
22+
23+
public static GitHubReviewMentionRepository create(DatabaseManager databaseManager) {
24+
try {
25+
TableUtils.createTableIfNotExists(databaseManager.getConnectionSource(), GitHubReviewMentionWrapper.class);
26+
}
27+
catch (SQLException sqlException) {
28+
Sentry.captureException(sqlException);
29+
throw new DataAccessException("Failed to create table", sqlException);
30+
}
31+
32+
return new GitHubReviewMentionRepositoryImpl(databaseManager);
33+
}
34+
35+
@Override
36+
public CompletableFuture<Void> markReviewerAsMentioned(GitHubPullRequest pullRequest, long userId) {
37+
return CompletableFuture.runAsync(() -> {
38+
GitHubReviewMentionWrapper mention =
39+
GitHubReviewMentionWrapper.create(pullRequest.toUrl(), userId, Instant.now());
40+
this.save(mention);
41+
});
42+
}
43+
44+
@Override
45+
public CompletableFuture<Boolean> isMentioned(GitHubPullRequest pullRequest, long userId) {
46+
return this.select(pullRequest.toUrl()).thenApply(mentionOptional -> {
47+
if (mentionOptional.isEmpty()) {
48+
return false;
49+
}
50+
51+
GitHubReviewMentionWrapper mention = mentionOptional.get();
52+
Instant lastMention = mention.getLastMention();
53+
Instant nextMention = lastMention.plus(MENTION_INTERVAL);
54+
55+
return nextMention.isAfter(Instant.now());
56+
});
57+
}
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.eternalcode.discordapp.review.database;
2+
3+
import com.j256.ormlite.field.DatabaseField;
4+
import com.j256.ormlite.table.DatabaseTable;
5+
import java.time.Instant;
6+
7+
@DatabaseTable(tableName = "review_mentions")
8+
public final class GitHubReviewMentionWrapper {
9+
10+
@DatabaseField(id = true)
11+
private String pullRequest;
12+
13+
@DatabaseField
14+
private long userId;
15+
16+
@DatabaseField
17+
private long lastMention;
18+
19+
public GitHubReviewMentionWrapper() {
20+
}
21+
22+
private GitHubReviewMentionWrapper(String pullRequest, long userId, long lastMention) {
23+
this.pullRequest = pullRequest;
24+
this.userId = userId;
25+
this.lastMention = lastMention;
26+
}
27+
28+
public static GitHubReviewMentionWrapper create(String pullRequest, long userId, Instant lastMention) {
29+
return new GitHubReviewMentionWrapper(pullRequest, userId, lastMention.toEpochMilli());
30+
}
31+
32+
public String getPullRequest() {
33+
return this.pullRequest;
34+
}
35+
36+
public long getUserId() {
37+
return this.userId;
38+
}
39+
40+
public Instant getLastMention() {
41+
return Instant.ofEpochMilli(this.lastMention);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)