Skip to content

Commit

Permalink
#284: Create new Observations API Endpoint to be able to update the o…
Browse files Browse the repository at this point in the history
…bservation endpoint token label.
  • Loading branch information
bnitsch authored and benitsch committed Jun 3, 2024
1 parent 92fd368 commit 01327dd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ public ResponseEntity<EndpointTokenDTO> createToken(Long studyId, Integer observ
);
}

@Override
@RequiresStudyRole({StudyRole.STUDY_ADMIN, StudyRole.STUDY_OPERATOR})
public ResponseEntity<EndpointTokenDTO> updateTokenLabel(Long studyId, Integer observationId, Integer tokenId, String tokenLabel) {
Optional<EndpointToken> token = integrationService.updateToken(studyId, observationId, tokenId, tokenLabel.replace("\"", ""));

if(token.isEmpty()) {
throw new BadRequestException("Token with given id doesn't exist for given observation");
}

return ResponseEntity.ok().body(
EndpointTokenTransformer.toEndpointTokenDTO(
token.get()
)
);
}

@Override
@RequiresStudyRole({StudyRole.STUDY_ADMIN, StudyRole.STUDY_OPERATOR})
public ResponseEntity<EndpointTokenDTO> getToken(Long studyId, Integer observationId, Integer tokenId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

@Component
public class IntegrationRepository {
private static final String GET_TOKEN_BY_IDS = "SELECT * FROM observation_api_tokens WHERE study_id = ? AND observation_id = ? AND token_id = ?";
private static final String ADD_TOKEN =
"INSERT INTO observation_api_tokens(study_id, observation_id, token_id, token_label, token) " +
"VALUES (:study_id, :observation_id, (SELECT COALESCE(MAX(token_id),0)+1 FROM observation_api_tokens WHERE study_id = :study_id AND observation_id = :observation_id), :token_label, :token) " +
Expand All @@ -37,7 +38,10 @@ public class IntegrationRepository {
"DELETE FROM observation_api_tokens " +
"WHERE study_id = ? AND observation_id = ? AND token_id = ?";
private static final String DELETE_ALL = "DELETE FROM observation_api_tokens";

private static final String UPDATE_TOKEN =
"UPDATE observation_api_tokens " +
"SET token_label = :token_label " +
"WHERE study_id = :study_id AND observation_id = :observation_id AND token_id = :token_id";
private static final String DELETE_ALL_FOR_STUDY_ID =
"DELETE FROM observation_api_tokens " +
"WHERE study_id = ?";
Expand Down Expand Up @@ -87,6 +91,25 @@ public void deleteToken(Long studyId, Integer observationId, Integer tokenId) {
template.update(DELETE_TOKEN, studyId, observationId, tokenId);
}

public Optional<EndpointToken> updateToken(Long studyId, Integer observationId, Integer tokenId, String tokenLabel) {
try {
namedTemplate.update(UPDATE_TOKEN,
new MapSqlParameterSource()
.addValue("study_id", studyId)
.addValue("observation_id", observationId)
.addValue("token_id", tokenId)
.addValue("token_label", tokenLabel)
);
return getById(studyId, observationId, tokenId);
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}

public Optional<EndpointToken> getById(long studyId, int observationId, int tokenId) {
return Optional.ofNullable(template.queryForObject(GET_TOKEN_BY_IDS, getHiddenTokenRowMapper(), studyId, observationId, tokenId));
}

private static RowMapper<EndpointToken> getHiddenTokenRowMapper() {
return (rs, rowNum) -> new EndpointToken(
rs.getInt("token_id"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public void deleteToken(Long studyId, Integer observationId, Integer tokenId) {
repository.deleteToken(studyId, observationId, tokenId);
}

public Optional<EndpointToken> updateToken(Long studyId, Integer observationId, Integer tokenId, String tokenLabel) {
studyStateService.assertStudyNotInState(studyId, Study.Status.CLOSED);
return repository.updateToken(studyId, observationId, tokenId, tokenLabel);
}

public void alignIntegrationsWithStudyState(Study study) {
if(study.getStudyState() == Study.Status.CLOSED) {
repository.clearForStudyId(study.getStudyId());
Expand Down
27 changes: 27 additions & 0 deletions studymanager/src/main/resources/openapi/StudyManagerAPI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,33 @@ paths:
'404':
description: not found

put:
tags:
- observations
description: Update label from observation endpoint token
operationId: updateTokenLabel
parameters:
- $ref: '#/components/parameters/StudyId'
- $ref: '#/components/parameters/ObservationId'
- $ref: '#/components/parameters/TokenId'
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/TokenLabel'
responses:
'200':
description: Token label successfully updated
content:
application/json:
schema:
$ref: '#/components/schemas/EndpointToken'

'400':
description: could not update observation
'404':
description: not found

delete:
tags:
Expand Down

0 comments on commit 01327dd

Please sign in to comment.