-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/main/java/com/eternalcode/discordapp/review/database/GitHubReviewMentionRepository.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,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); | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
...in/java/com/eternalcode/discordapp/review/database/GitHubReviewMentionRepositoryImpl.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,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()); | ||
}); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/eternalcode/discordapp/review/database/GitHubReviewMentionWrapper.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,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); | ||
} | ||
|
||
} |