diff --git a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java index cd31023..7a78e5a 100644 --- a/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java +++ b/buisnesslogic/src/main/java/com/plannaplan/services/ExchangeService.java @@ -23,8 +23,22 @@ public class ExchangeService { public Exchange save(Exchange exchange){ return this.repo.save(exchange); } - + + /** + * @param id Id of exchange in database + * @return Optional Exchange if found + */ + public Optional getById(Long id){ + return this.repo.findById(id); + } + /** + * @param entity Exchange entity which we would like to delete + */ + public void deleteExchange(Exchange entity){ + this.repo.delete(entity); + } + /** * @param assignment Assignment to trade for * @param group Desired group diff --git a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java index b9c4869..e255fe0 100644 --- a/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java +++ b/restservice/src/main/java/com/plannaplan/controllers/ExchangeController.java @@ -18,6 +18,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -79,5 +81,27 @@ public class ExchangeController extends TokenBasedController{ return new ResponseEntity<>("Success", HttpStatus.OK); } + @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 Optional exchange = this.exchangeService.getById(offerId); + + 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); + } + + this.exchangeService.deleteExchange(exchangeToDelete); + return new ResponseEntity<>("Success", HttpStatus.OK); + } }