Skip to content

Commit

Permalink
feat(LapCount): add endpoint for count for all teams for specific lap…
Browse files Browse the repository at this point in the history
…Source
  • Loading branch information
NuttyShrimp committed Apr 3, 2024
1 parent b314cc5 commit 4ad04ca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/telraam/api/LapCountResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.ws.rs.core.MediaType;
import telraam.database.daos.TeamDAO;
import telraam.database.models.Lap;
import telraam.database.models.LapCount;
import telraam.database.models.Team;
import telraam.util.AcceptedLapsUtil;

Expand Down Expand Up @@ -59,6 +60,17 @@ public Map<String, Integer> getLapCounts() {

return perName;
}

@GET
@Path("/{lapSourceId}")
public List<LapCount> getLapCountForLapSource(@PathParam("lapSourceId") Integer id, @QueryParam("end") Optional<String> endTimestamp) {
LocalDateTime dateTime = LocalDateTime.now();
if (endTimestamp.isPresent()) {
dateTime = LocalDateTime.parse(endTimestamp.get());
}
List<LapCount> laps = lapDAO.getAllBeforeTime(id, Timestamp.valueOf(dateTime));
return laps;
}

// EndTimestamp should be a ISO formatted date timestamp
@GET
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/telraam/database/daos/LapDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import telraam.database.models.Lap;
import telraam.database.models.LapCount;
import telraam.database.models.TeamLapCount;

import java.sql.Timestamp;
Expand All @@ -29,6 +30,10 @@ public interface LapDAO extends DAO<Lap> {
@RegisterBeanMapper(Lap.class)
List<Lap> getAllBySourceSorted(@Bind("lapSourceId") Integer lapSourceId);

@SqlQuery("SELECT t.id as team_id, (SELECT COUNT(*) FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp and team_id = t.id) as count FROM team t")
@RegisterBeanMapper(LapCount.class)
List<LapCount> getAllBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("timestamp") Timestamp timestamp);

@SqlQuery("SELECT * FROM lap WHERE lap_source_id = :lapSourceId AND timestamp <= :timestamp AND team_id = :teamId")
@RegisterBeanMapper(Lap.class)
List<Lap> getAllForTeamBeforeTime(@Bind("lapSourceId") Integer lapSourceId, @Bind("teamId") Integer teamId, @Bind("timestamp") Timestamp timestamp);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/telraam/database/models/LapCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package telraam.database.models;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter @Setter @NoArgsConstructor
public class LapCount {
private int teamId;
private int count;
}

0 comments on commit 4ad04ca

Please sign in to comment.