conrollers docs

This commit is contained in:
Filip Izydorczyk 2021-01-16 14:40:17 +01:00
parent 9372ea5562
commit 707e26e082

View File

@ -33,6 +33,10 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
/**
* Rest controller to Exchange related endpoints. More detailed api docs is
* available via swagger
*/
@RestController @RestController
@CrossOrigin @CrossOrigin
@RequestMapping("/api/" + App.API_VERSION + "/exchanges") @RequestMapping("/api/" + App.API_VERSION + "/exchanges")
@ -48,19 +52,23 @@ public class ExchangeController extends TokenBasedController{
@Autowired @Autowired
private ExchangeService exchangeService; 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") @PostMapping("/exchange")
@ApiOperation(value = "Creates exchange offer.") @ApiOperation(value = "Creates exchange offer.")
public ResponseEntity<String> createExchange( public ResponseEntity<String> createExchange(
@ApiParam( value = "Json object that contains assignment to trade and desired group") @ApiParam(value = "Json object that contains assignment to trade and desired group. For example: { \"assignment\": 3, \"group\":32 }") @RequestBody Map<String, Long> exchangeRequest)
@RequestBody
Map<String, Long> exchangeRequest)
throws UserNotFoundException { throws UserNotFoundException {
final User asker = this.getCurrentUser() final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token"));
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Long assignmentId = exchangeRequest.get("assignment"); final Long assignmentId = exchangeRequest.get("assignment");
final Long groupId = exchangeRequest.get("group"); final Long groupId = exchangeRequest.get("group");
final List<Long> ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); final List<Long> 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); return new ResponseEntity<>("User has already got this group.", HttpStatus.BAD_REQUEST);
@ -80,8 +88,11 @@ public class ExchangeController extends TokenBasedController{
final Assignment assignmentInstance = assignment.get(); final Assignment assignmentInstance = assignment.get();
final Groups groupInstance = group.get(); final Groups groupInstance = group.get();
if(!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId()) && assignmentInstance.isAccepted())){ if (!(assignmentInstance.getCommision().getCommisionOwner().getId().equals(asker.getId())
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); && 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)); this.exchangeService.save(new Exchange(assignmentInstance, groupInstance));
@ -89,13 +100,17 @@ public class ExchangeController extends TokenBasedController{
return new ResponseEntity<>("Success", HttpStatus.OK); 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}") @DeleteMapping("/exchange/{id}")
@ApiOperation(value = "Delete exchange offer") @ApiOperation(value = "Delete exchange offer")
public ResponseEntity<String> deleteExchange(@PathVariable(name = "id", required = false) Long offerId) public ResponseEntity<String> deleteExchange(@PathVariable(name = "id", required = false) Long offerId)
throws UserNotFoundException { throws UserNotFoundException {
final User asker = this.getCurrentUser() final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token"));
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Optional<Exchange> exchange = this.exchangeService.getById(offerId); final Optional<Exchange> exchange = this.exchangeService.getById(offerId);
@ -113,13 +128,15 @@ public class ExchangeController extends TokenBasedController{
return new ResponseEntity<>("Success", HttpStatus.OK); return new ResponseEntity<>("Success", HttpStatus.OK);
} }
/**
* @return return all user's exchange offers
* @throws UserNotFoundException iF user was not found
*/
@GetMapping("/exchange/all") @GetMapping("/exchange/all")
@ApiOperation(value = "Get exchange offers") @ApiOperation(value = "Get exchange offers")
public ResponseEntity<List<ExchangeResponse>> getExchange() public ResponseEntity<List<ExchangeResponse>> getExchange() throws UserNotFoundException {
throws UserNotFoundException {
final User asker = this.getCurrentUser() final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token"));
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final List<Exchange> response = exchangeService.getByUserId(asker.getId()); final List<Exchange> response = exchangeService.getByUserId(asker.getId());
final List<ExchangeResponse> listOfResponses = ExchangeResponseMappers.mapToDefaultResponse(response); final List<ExchangeResponse> listOfResponses = ExchangeResponseMappers.mapToDefaultResponse(response);
@ -127,13 +144,17 @@ public class ExchangeController extends TokenBasedController{
return new ResponseEntity<>(listOfResponses, HttpStatus.OK); 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}") @GetMapping("/exchange/{id}")
@ApiOperation(value = "Get exchange offers") @ApiOperation(value = "Get exchange offer")
public ResponseEntity<ExchangeResponse> getExchangeById(@PathVariable(name = "id", required = false) Long offerId) public ResponseEntity<ExchangeResponse> getExchangeById(@PathVariable(name = "id", required = false) Long offerId)
throws UserNotFoundException { throws UserNotFoundException {
final User asker = this.getCurrentUser() final User asker = this.getCurrentUser().orElseThrow(() -> new UserNotFoundException("Invalid token"));
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Optional<Exchange> exchange = this.exchangeService.getById(offerId); final Optional<Exchange> exchange = this.exchangeService.getById(offerId);