From e8d046d320744134ee1e0cfb88ce666eb3f9931c Mon Sep 17 00:00:00 2001 From: xmajdan <xmajdan@fi.muni.cz> Date: Wed, 14 Aug 2024 01:32:08 +0200 Subject: [PATCH] Add enpoint for access token by poolId --- VERSION.txt | 2 +- .../TrainingInstancesRestController.java | 36 ++++++++++++++++--- .../facade/TrainingInstanceFacade.java | 12 +++++++ .../training/TrainingInstanceService.java | 11 ++++++ 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index 51cfa71..4d89c4b 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -1,4 +1,4 @@ -2.3.0 Add training access tokens to sandbox api calls. +2.3.0 Add training access tokens to sandbox api calls. Add access token endpoints. 2.2.10 Fix user ref creation failing on parallel requests. 2.2.9 Add check to prevent reviving an expired training instance. 2.2.8 Add check for training definition being in use when trying to add, remove and update tasks. diff --git a/src/main/java/cz/muni/ics/kypo/training/adaptive/controller/TrainingInstancesRestController.java b/src/main/java/cz/muni/ics/kypo/training/adaptive/controller/TrainingInstancesRestController.java index 6034f7d..6384e86 100644 --- a/src/main/java/cz/muni/ics/kypo/training/adaptive/controller/TrainingInstancesRestController.java +++ b/src/main/java/cz/muni/ics/kypo/training/adaptive/controller/TrainingInstancesRestController.java @@ -89,7 +89,7 @@ public class TrainingInstancesRestController { /** * Get requested Training Instance by pool id. * - * @param poolId id of the assigned pool. + * @param poolId id of the assigned pool. * @return Requested Training Instance by pool id. */ @ApiOperation(httpMethod = "GET", @@ -112,6 +112,32 @@ public class TrainingInstancesRestController { return ResponseEntity.ok(trainingInstanceResource); } + /** + * Get Training instance access token by pool id. + * + * @param poolId id of the assigned pool. + * @return Requested access token by pool id if it exists. + */ + @ApiOperation(httpMethod = "GET", + value = "Get training instance access token by pool id.", + response = String.class, + nickname = "findTrainingInstanceAccessTokenByPoolId", + notes = "Returns training instance access token by pool id.", + produces = MediaType.APPLICATION_JSON_VALUE + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "The access token has been found", response = TrainingInstanceDTO.class), + @ApiResponse(code = 404, message = "The access token has not been found.", response = ApiError.class), + @ApiResponse(code = 500, message = "Unexpected condition was encountered.", response = ApiError.class) + }) + @GetMapping(path = "/access/{poolId}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity<String> findInstanceAccessTokenByPoolId( + @ApiParam(value = "Pool ID", required = true) + @PathVariable("poolId") Long poolId) { + String accessToken = trainingInstanceFacade.findInstanceAccessTokenByPoolId(poolId); + return ResponseEntity.ok(accessToken); + } + /** * Get all Training Instances. * @@ -133,7 +159,7 @@ public class TrainingInstancesRestController { @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Object> findAllTrainingInstances(@QuerydslPredicate(root = TrainingInstance.class) Predicate predicate, @ApiParam(value = "Pagination support.", required = false) - Pageable pageable, + Pageable pageable, @ApiParam(value = "Fields which should be returned in REST API response", required = false) @RequestParam(value = "fields", required = false) String fields) { PageResultResource<TrainingInstanceFindAllResponseDTO> trainingInstanceResource = trainingInstanceFacade.findAll(predicate, pageable); @@ -301,7 +327,7 @@ public class TrainingInstancesRestController { @ApiParam(value = "If only active or not active training runs should be returned.") @RequestParam(value = "isActive", required = false) Boolean isActive, @ApiParam(value = "Pagination support.") - Pageable pageable, + Pageable pageable, @ApiParam(value = "Fields which should be returned in REST API response", required = false) @RequestParam(value = "fields", required = false) String fields) { PageResultResource<TrainingRunDTO> trainingRunResource = trainingInstanceFacade.findTrainingRunsByTrainingInstance(instanceId, isActive, pageable); @@ -338,7 +364,7 @@ public class TrainingInstancesRestController { @ApiParam(value = "Family name filter.", required = true) @RequestParam(value = "familyName", required = false) String familyName, @ApiParam(value = "Pagination support.") - Pageable pageable) { + Pageable pageable) { PageResultResource<UserRefDTO> designers = trainingInstanceFacade.getOrganizersOfTrainingInstance(trainingInstanceId, pageable, givenName, familyName); return ResponseEntity.ok(SquigglyUtils.stringify(objectMapper, designers)); } @@ -373,7 +399,7 @@ public class TrainingInstancesRestController { @ApiParam(value = "Family name filter.", required = false) @RequestParam(value = "familyName", required = false) String familyName, @ApiParam(value = "Pagination support.") - Pageable pageable) { + Pageable pageable) { PageResultResource<UserRefDTO> designers = trainingInstanceFacade.getOrganizersNotInGivenTrainingInstance(trainingInstanceId, pageable, givenName, familyName); return ResponseEntity.ok(SquigglyUtils.stringify(objectMapper, designers)); } diff --git a/src/main/java/cz/muni/ics/kypo/training/adaptive/facade/TrainingInstanceFacade.java b/src/main/java/cz/muni/ics/kypo/training/adaptive/facade/TrainingInstanceFacade.java index 5114149..5e45f53 100644 --- a/src/main/java/cz/muni/ics/kypo/training/adaptive/facade/TrainingInstanceFacade.java +++ b/src/main/java/cz/muni/ics/kypo/training/adaptive/facade/TrainingInstanceFacade.java @@ -118,6 +118,18 @@ public class TrainingInstanceFacade { return trainingInstanceMapper.mapToDTO(trainingInstanceService.findByPoolId(poolId)); } + /** + * Get Training instance access token by pool id. + * + * @param poolId id of the assigned pool. + * @return Requested access token by pool id if it exists. + */ + @IsOrganizerOrAdmin + @TransactionalRO + public String findInstanceAccessTokenByPoolId(Long poolId) { + return trainingInstanceService.findInstanceAccessTokenByPoolId(poolId); + } + /** * Find all Training Instances. * diff --git a/src/main/java/cz/muni/ics/kypo/training/adaptive/service/training/TrainingInstanceService.java b/src/main/java/cz/muni/ics/kypo/training/adaptive/service/training/TrainingInstanceService.java index d9b70dd..e1345ce 100644 --- a/src/main/java/cz/muni/ics/kypo/training/adaptive/service/training/TrainingInstanceService.java +++ b/src/main/java/cz/muni/ics/kypo/training/adaptive/service/training/TrainingInstanceService.java @@ -85,6 +85,17 @@ public class TrainingInstanceService { .orElseThrow(() -> new EntityNotFoundException(new EntityErrorDetail(TrainingInstance.class, "poolId", poolId.getClass(), poolId))); } + /** + * Find Training instance access token by pool id if exists. + * + * @param poolId the pool id + * @return the access token + */ + public String findInstanceAccessTokenByPoolId(Long poolId) { + Optional<TrainingInstance> instance = trainingInstanceRepository.findByPoolId(poolId); + return instance.map(TrainingInstance::getAccessToken).orElse(null); + } + /** * Find specific Training instance by id including its associated Training definition. * -- GitLab