Skip to content

Commit

Permalink
#111 Also provide the group-name when listing participants
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-fra committed Apr 22, 2024
1 parent 2e4eb69 commit 7c69888
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
@RequestMapping(value = "/api/v1", produces = MediaType.APPLICATION_JSON_VALUE)
public class ExternalDataApiV1Controller implements ExternalDataApi {

private static final Logger LOG = LoggerFactory.getLogger(DataApiV1Controller.class);
private static final Logger LOG = LoggerFactory.getLogger(ExternalDataApiV1Controller.class);

private final ExternalService externalService;
private final ElasticService elasticService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@

import io.redlink.more.data.api.app.v1.model.ParticipantDTO;
import io.redlink.more.data.api.app.v1.model.ParticipantStatusDTO;
import io.redlink.more.data.api.app.v1.model.ParticipantStudyGroupDTO;
import io.redlink.more.data.model.Participant;

public final class ParticipantTransformer {

private ParticipantTransformer() {}

public static ParticipantDTO toDTO(Participant participant) {
if(participant == null) {
if (participant == null) {
return null;
}
return new ParticipantDTO(
String.valueOf(participant.id()),
participant.alias(),
ParticipantStatusDTO.fromValue(participant.status()),
participant.studyGroupId(),
toGroupDto(participant),
BaseTransformers.toOffsetDateTime(participant.start())
);
}

private static ParticipantStudyGroupDTO toGroupDto(Participant participant) {
if (participant.studyGroupId().isPresent()) {
final int groupId = participant.studyGroupId().getAsInt();
return new ParticipantStudyGroupDTO(
String.valueOf(groupId),
participant.studyGroupTitle()
);
} else {
return null;
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/io/redlink/more/data/model/Participant.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.redlink.more.data.model;

import java.time.Instant;
import java.util.OptionalInt;

public record Participant(
int id,
String alias,
String status,
Integer studyGroupId,
OptionalInt studyGroupId,
String studyGroupTitle,
Instant start
) {}
32 changes: 13 additions & 19 deletions src/main/java/io/redlink/more/data/repository/StudyRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ public class StudyRepository {
"WHERE study_id = :study_id AND participant_id = :participant_id AND status = :oldStatus::participant_status";

private static final String SQL_LIST_PARTICIPANTS_BY_STUDY =
"SELECT participant_id, alias, status, study_group_id, start FROM participants " +
"WHERE study_id = :study_id AND (study_group_id = :study_group_id OR :study_group_id::INT IS NULL)";
"SELECT participant_id, alias, status, sg.study_group_id, sg.title as study_group_title, start " +
"FROM participants p LEFT OUTER JOIN study_groups sg ON ( p.study_id = sg.study_id AND p.study_group_id = sg.study_group_id ) " +
"WHERE p.study_id = :study_id " +
"AND (p.study_group_id = :study_group_id OR :study_group_id::INT IS NULL)";

private static final String GET_OBSERVATION_PROPERTIES_FOR_PARTICIPANT =
"SELECT properties FROM participant_observation_properties " +
Expand Down Expand Up @@ -196,22 +198,13 @@ public Optional<SimpleParticipant> findParticipant(RoutingInfo routingInfo) {
}
}

public List<Participant> listParticipants(long studyId, int groupId) {
if(groupId < 0) {
return namedTemplate.query(
SQL_LIST_PARTICIPANTS_BY_STUDY,
new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("study_group_id", null),
getParticipantRowMapper());
} else {
return namedTemplate.query(
SQL_LIST_PARTICIPANTS_BY_STUDY,
new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("study_group_id", groupId),
getParticipantRowMapper());
}
public List<Participant> listParticipants(long studyId, OptionalInt groupId) {
return namedTemplate.query(
SQL_LIST_PARTICIPANTS_BY_STUDY,
new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("study_group_id", groupId.isPresent() ? groupId.getAsInt() : null),
getParticipantRowMapper());
}

private List<Observation> listObservations(long studyId, int groupId, int participantId, boolean filterByGroup) {
Expand Down Expand Up @@ -374,7 +367,8 @@ private static RowMapper<Participant> getParticipantRowMapper() {
rs.getInt("participant_id"),
rs.getString("alias"),
rs.getString("status"),
rs.getInt("study_group_id"),
DbUtils.readOptionalInt(rs, "study_group_id"),
rs.getString("study_group_title"),
toInstant(rs.getTimestamp("start"))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ExternalService(StudyRepository repository, PasswordEncoder passwordEncod
this.repository = repository;
this.passwordEncoder = passwordEncoder;
}

public ApiRoutingInfo getRoutingInfo(
String moreApiToken
) {
Expand Down Expand Up @@ -86,6 +87,6 @@ public Interval getIntervalForObservation(Long studyId, Integer observationId, I
}

public List<Participant> listParticipants(Long studyId, OptionalInt studyGroupId) {
return repository.listParticipants(studyId, studyGroupId.orElse(Integer.MIN_VALUE));
return repository.listParticipants(studyId, studyGroupId);
}
}
12 changes: 10 additions & 2 deletions src/main/resources/openapi/ExternalAPI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,16 @@ components:
status:
$ref: '#/components/schemas/ParticipantStatus'
studyGroup:
type: integer
format: int32
type: object
properties:
groupId:
type: string
name:
type: string
required:
- groupId
- name
nullable: true
start:
type: string
format: date-time
Expand Down

0 comments on commit 7c69888

Please sign in to comment.