-
Notifications
You must be signed in to change notification settings - Fork 0
Implement match and match cycle repos #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: alter-match-schema
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package org.patinanetwork.patchats.api.match.db.repos; | ||
|
|
||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.patinanetwork.patchats.api.match.db.models.MatchCycle; | ||
| import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
| import org.springframework.jdbc.core.simple.JdbcClient; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| @Repository | ||
| @RequiredArgsConstructor | ||
| public class MatchCycleSqlRepo implements MatchCycleRepo { | ||
| private final JdbcClient jdbc; | ||
|
|
||
| private static final String TOTAL_MATCHED_SQL = | ||
| "(SELECT count(*) FROM matches m WHERE m.cycle_id = match_cycles.id) AS total_matched"; | ||
|
|
||
| private MatchCycle parseResultSetToMatchCycle(final ResultSet rs) throws SQLException { | ||
| return MatchCycle.builder() | ||
| .id(rs.getInt("id")) | ||
| .period(rs.getString("period")) | ||
|
Check failure on line 25 in src/main/java/org/patinanetwork/patchats/api/match/db/repos/MatchCycleSqlRepo.java
|
||
| .runAt(rs.getObject("run_at", Instant.class)) | ||
|
Check failure on line 26 in src/main/java/org/patinanetwork/patchats/api/match/db/repos/MatchCycleSqlRepo.java
|
||
| .isDraft(rs.getBoolean("is_draft")) | ||
|
Check failure on line 27 in src/main/java/org/patinanetwork/patchats/api/match/db/repos/MatchCycleSqlRepo.java
|
||
| .totalMembers(rs.getInt("total_members")) | ||
| .totalMatched(rs.getInt("total_matched")) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public MatchCycle createMatchCycle(MatchCycle matchCycle) { | ||
| String sql = """ | ||
| INSERT INTO "match_cycles" ( | ||
| "period", | ||
| "run_at", | ||
| "is_draft", | ||
| "total_members" | ||
| ) | ||
| VALUES( | ||
| :period, | ||
| :run_at, | ||
| :is_draft, | ||
| :total_members | ||
| ) | ||
| RETURNING | ||
| *, | ||
| %s | ||
| """.formatted(TOTAL_MATCHED_SQL); | ||
|
|
||
| return jdbc.sql(sql) | ||
| .param("period", matchCycle.getPeriod()) | ||
| .param("run_at", matchCycle.getRunAt()) | ||
| .param("is_draft", matchCycle.isDraft()) | ||
| .param("total_members", matchCycle.getTotalMembers()) | ||
| .query((rs, rowNum) -> parseResultSetToMatchCycle(rs)) | ||
| .single(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<MatchCycle> updateMatchCycle(MatchCycle matchCycle) { | ||
| String sql = """ | ||
| UPDATE "match_cycles" SET | ||
| "period" = :period, | ||
| "run_at" = :run_at, | ||
| "is_draft" = :is_draft | ||
| WHERE "id" = :id | ||
| RETURNING | ||
| *, | ||
| %s | ||
| """.formatted(TOTAL_MATCHED_SQL); | ||
|
|
||
| return jdbc.sql(sql) | ||
| .param("id", matchCycle.getId()) | ||
| .param("period", matchCycle.getPeriod()) | ||
| .param("run_at", matchCycle.getRunAt()) | ||
| .param("is_draft", matchCycle.isDraft()) | ||
| .query((rs, rowNum) -> parseResultSetToMatchCycle(rs)) | ||
| .optional(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<MatchCycle> getMatchCycleById(Integer id) { | ||
| String sql = """ | ||
| SELECT | ||
| *, | ||
| %s | ||
| FROM match_cycles | ||
| WHERE id = :id | ||
| """.formatted(TOTAL_MATCHED_SQL); | ||
| return jdbc.sql(sql) | ||
| .param("id", id) | ||
| .query((rs, rowNum) -> parseResultSetToMatchCycle(rs)) | ||
| .optional(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<MatchCycle> setMatchCycleDraft(Integer id, boolean isDraft) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: better titled as |
||
| String sql = """ | ||
| UPDATE "match_cycles" SET | ||
| "is_draft" = :is_draft | ||
| WHERE "id" = :id | ||
| RETURNING | ||
| *, | ||
| %s | ||
| """.formatted(TOTAL_MATCHED_SQL); | ||
|
|
||
| return jdbc.sql(sql) | ||
| .param("id", id) | ||
| .param("is_draft", isDraft) | ||
| .query((rs, rowNum) -> parseResultSetToMatchCycle(rs)) | ||
| .optional(); | ||
| } | ||
|
|
||
| @Override | ||
| public Optional<MatchCycle> deleteMatchCycleById(Integer id) { | ||
| String sql = """ | ||
| DELETE FROM match_cycles | ||
| WHERE id = :id | ||
| RETURNING | ||
| *, | ||
| 0 AS total_matched | ||
| """; | ||
| return jdbc.sql(sql) | ||
| .param("id", id) | ||
| .query((rs, rowNum) -> parseResultSetToMatchCycle(rs)) | ||
| .optional(); | ||
| } | ||
|
|
||
| @Override | ||
| public List<MatchCycle> filterMatchCycles(MatchCycleFilterCriteria criteria) { | ||
| StringBuilder sql = new StringBuilder("SELECT *, " + TOTAL_MATCHED_SQL + " FROM match_cycles WHERE 1=1"); | ||
| MapSqlParameterSource params = new MapSqlParameterSource(); | ||
|
|
||
| criteria.period().ifPresent(period -> { | ||
| sql.append(" AND period = :period"); | ||
| params.addValue("period", period); | ||
| }); | ||
|
|
||
| criteria.startTime().ifPresent(start -> { | ||
| sql.append(" AND run_at >= :start_time"); | ||
| params.addValue("start_time", start); | ||
| }); | ||
|
|
||
| criteria.endTime().ifPresent(end -> { | ||
| sql.append(" AND run_at <= :end_time"); | ||
| params.addValue("end_time", end); | ||
| }); | ||
|
|
||
| criteria.isDraft().ifPresent(isDraft -> { | ||
| sql.append(" AND is_draft = :is_draft"); | ||
| params.addValue("is_draft", isDraft); | ||
| }); | ||
|
|
||
| return jdbc.sql(sql.toString()) | ||
| .paramSource(params) | ||
| .query((rs, rowNum) -> parseResultSetToMatchCycle(rs)) | ||
| .list(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Postgres has range types we can use to do range searches, that would probably be a better type fit for this field. This would probably be more effective than shoehorning the range into a single format. Unsure how much we'll need that in the future though.