Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion db/migration/V0006__Alter_match_cycles_and_matches.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ ALTER TABLE "match_cycles"

ALTER TABLE "matches"
DROP COLUMN "feedback_a",
DROP COLUMN "feedback_b";
DROP COLUMN "feedback_b";
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,5 @@ public class Match {
@Setter
private String status;

@Setter
private String feedbackA;

@Setter
private String feedbackB;

private Instant createdAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class MatchCycle {
@Setter
private Instant runAt;

@Setter
private boolean isDraft = true;

@Setter
private Integer totalMembers;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.Optional;

public record MatchCycleFilterCriteria(
Optional<String> period, Optional<Instant> startTime, Optional<Instant> endTime) {
Optional<String> period, Optional<Instant> startTime, Optional<Instant> endTime, Optional<Boolean> isDraft) {

public static MatchCycleFilterCriteria empty() {
return new MatchCycleFilterCriteria(Optional.empty(), Optional.empty(), Optional.empty());
return new MatchCycleFilterCriteria(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public interface MatchCycleRepo {
* Optional fields:
* <ul>
* <li>period
* <li>totalMembers
* <li>totalMatched
* <li>isDraft
* </ul>
* The id field will be auto-generated by the database.
*/
Expand All @@ -28,14 +27,15 @@ public interface MatchCycleRepo {
* <ul>
* <li>period
* <li>runAt
* <li>totalMembers
* <li>totalMatched
* <li>isDraft
* </ul>
*/
Optional<MatchCycle> updateMatchCycle(MatchCycle matchCycle);

Optional<MatchCycle> getMatchCycleById(Integer id);

Optional<MatchCycle> setMatchCycleDraft(Integer id, boolean isDraft);

Optional<MatchCycle> deleteMatchCycleById(Integer id);

List<MatchCycle> filterMatchCycles(MatchCycleFilterCriteria criteria);
Expand Down
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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "period" 4 times.

See more on https://sonarcloud.io/project/issues?id=Patina-Network_patchats&issues=AaARP7sLBRldXzvA0VWS&open=AaARP7sLBRldXzvA0VWS&pullRequest=74
.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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "run_at" 3 times.

See more on https://sonarcloud.io/project/issues?id=Patina-Network_patchats&issues=AaARP7sLBRldXzvA0VWQ&open=AaARP7sLBRldXzvA0VWQ&pullRequest=74
.isDraft(rs.getBoolean("is_draft"))

Check failure on line 27 in src/main/java/org/patinanetwork/patchats/api/match/db/repos/MatchCycleSqlRepo.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "is_draft" 5 times.

See more on https://sonarcloud.io/project/issues?id=Patina-Network_patchats&issues=AaARP7sLBRldXzvA0VWR&open=AaARP7sLBRldXzvA0VWR&pullRequest=74
.totalMembers(rs.getInt("total_members"))
.totalMatched(rs.getInt("total_matched"))
.build();
}

@Override
public MatchCycle createMatchCycle(MatchCycle matchCycle) {
String sql = """
INSERT INTO "match_cycles" (
"period",

Copy link
Copy Markdown

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.

"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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: better titled as setMatchCycleIsDraft, this way it's clearer that it's a Boolean switch.

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public interface MatchRepo {
* <li>matchCycleId
* <li>matchScore
* <li>status
* <li>feedbackA
* <li>feedbackB
* </ul>
*/
Optional<Match> updateMatch(Match match);
Expand All @@ -39,7 +37,5 @@ public interface MatchRepo {

Optional<Match> deleteMatchById(UUID id);

Optional<Match> recordFeedback(UUID id, UUID memberId, String feedback);

List<Match> filterMatches(MatchFilterCriteria criteria);
}
Loading