From 707e26e0825d180aac96628eb8b8e0fe0aacc327 Mon Sep 17 00:00:00 2001 From: Filip Izydorczyk Date: Sat, 16 Jan 2021 14:40:17 +0100 Subject: [PATCH] conrollers docs --- .../controllers/ExchangeController.java | 79 ++++++++++++------- 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index 24c0d89..30a703f 100755 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -33,11 +33,15 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +/** + * Rest controller to Exchange related endpoints. More detailed api docs is + * available via swagger + */ @RestController @CrossOrigin @RequestMapping("/api/" + App.API_VERSION + "/exchanges") @Api(tags = { "Exchange" }, value = "Exchange", description = "Endpoint to exchange with accepted assignments.") -public class ExchangeController extends TokenBasedController{ +public class ExchangeController extends TokenBasedController { @Autowired private GroupService groupService; @@ -48,40 +52,47 @@ public class ExchangeController extends TokenBasedController{ @Autowired private ExchangeService exchangeService; + /** + * @param exchangeRequest mapped json body with requierd paramas (groupid and + * assignment) + * @return was job successfull + * @throws UserNotFoundException if user was not found + */ @PostMapping("/exchange") @ApiOperation(value = "Creates exchange offer.") public ResponseEntity createExchange( - @ApiParam( value = "Json object that contains assignment to trade and desired group") - @RequestBody - Map exchangeRequest) + @ApiParam(value = "Json object that contains assignment to trade and desired group. For example: { \"assignment\": 3, \"group\":32 }") @RequestBody Map exchangeRequest) throws UserNotFoundException { - final User asker = this.getCurrentUser() - .orElseThrow(() -> new UserNotFoundException("Invalid token")); + final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token")); final Long assignmentId = exchangeRequest.get("assignment"); final Long groupId = exchangeRequest.get("group"); - final List ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); + final List ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId) + .collect(Collectors.toList()); - if(ownedGroups.contains(groupId)){ + if (ownedGroups.contains(groupId)) { return new ResponseEntity<>("User has already got this group.", HttpStatus.BAD_REQUEST); } - if(assignmentId == null || groupId == null ){ + if (assignmentId == null || groupId == null) { return new ResponseEntity<>("Some of values are missing", HttpStatus.BAD_REQUEST); } final Optional assignment = this.assignmentService.getById(assignmentId); final Optional group = this.groupService.getGroupById(groupId); - if(assignment.isEmpty() || group.isEmpty()){ + if (assignment.isEmpty() || group.isEmpty()) { return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST); } final Assignment assignmentInstance = assignment.get(); final Groups groupInstance = group.get(); - if(!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId()) && assignmentInstance.isAccepted())){ - return new ResponseEntity<>("Some of problems appeared. Check if you have access to given assignment and if it is accepted or the exchange has not been already added.", HttpStatus.BAD_REQUEST); + if (!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId()) + && assignmentInstance.isAccepted())) { + return new ResponseEntity<>( + "Some of problems appeared. Check if you have access to given assignment and if it is accepted or the exchange has not been already added.", + HttpStatus.BAD_REQUEST); } this.exchangeService.save(new Exchange(assignmentInstance, groupInstance)); @@ -89,37 +100,43 @@ public class ExchangeController extends TokenBasedController{ return new ResponseEntity<>("Success", HttpStatus.OK); } + /** + * @param offerId id to delwete from db + * @return was jub successful + * @throws UserNotFoundException if user was not found + */ @DeleteMapping("/exchange/{id}") @ApiOperation(value = "Delete exchange offer") public ResponseEntity deleteExchange(@PathVariable(name = "id", required = false) Long offerId) throws UserNotFoundException { - final User asker = this.getCurrentUser() - .orElseThrow(() -> new UserNotFoundException("Invalid token")); + final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token")); final Optional exchange = this.exchangeService.getById(offerId); - if(exchange.isEmpty()){ - return new ResponseEntity<>("Given offer does not exist.", HttpStatus.BAD_REQUEST); + if (exchange.isEmpty()) { + return new ResponseEntity<>("Given offer does not exist.", HttpStatus.BAD_REQUEST); } final Exchange exchangeToDelete = exchange.get(); - if(!(exchangeToDelete.getOwnedAssignment().getCommision().getCommisionOwner().getId().equals(asker.getId()))){ - return new ResponseEntity<>("You have not permission for that action.", HttpStatus.BAD_REQUEST); + if (!(exchangeToDelete.getOwnedAssignment().getCommision().getCommisionOwner().getId().equals(asker.getId()))) { + return new ResponseEntity<>("You have not permission for that action.", HttpStatus.BAD_REQUEST); } this.exchangeService.deleteExchange(exchangeToDelete); return new ResponseEntity<>("Success", HttpStatus.OK); } + /** + * @return return all user's exchange offers + * @throws UserNotFoundException iF user was not found + */ @GetMapping("/exchange/all") @ApiOperation(value = "Get exchange offers") - public ResponseEntity> getExchange() - throws UserNotFoundException { + public ResponseEntity> getExchange() throws UserNotFoundException { - final User asker = this.getCurrentUser() - .orElseThrow(() -> new UserNotFoundException("Invalid token")); + final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token")); final List response = exchangeService.getByUserId(asker.getId()); final List listOfResponses = ExchangeResponseMappers.mapToDefaultResponse(response); @@ -127,23 +144,27 @@ public class ExchangeController extends TokenBasedController{ return new ResponseEntity<>(listOfResponses, HttpStatus.OK); } + /** + * @param offerId id of exchange in db + * @return Exchage response + * @throws UserNotFoundException if user was not found + */ @GetMapping("/exchange/{id}") - @ApiOperation(value = "Get exchange offers") + @ApiOperation(value = "Get exchange offer") public ResponseEntity getExchangeById(@PathVariable(name = "id", required = false) Long offerId) throws UserNotFoundException { - final User asker = this.getCurrentUser() - .orElseThrow(() -> new UserNotFoundException("Invalid token")); + final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token")); final Optional exchange = this.exchangeService.getById(offerId); - if(exchange.isEmpty()){ - return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + if (exchange.isEmpty()) { + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); } final Exchange exchangeInstance = exchange.get(); - if(!exchangeInstance.getOwnerId().equals(asker.getId())){ - return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + if (!exchangeInstance.getOwnerId().equals(asker.getId())) { + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); } return new ResponseEntity<>(new ExchangeResponse(exchangeInstance), HttpStatus.OK);