Checkpoint: deleting is working

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2021-01-07 16:01:25 +01:00
parent c8c8b62263
commit 56120c4724
Signed by: y0rune
GPG Key ID: F204C385F57EB348
2 changed files with 39 additions and 1 deletions

View File

@ -24,6 +24,20 @@ public class ExchangeService {
return this.repo.save(exchange);
}
/**
* @param id Id of exchange in database
* @return Optional Exchange if found
*/
public Optional<Exchange> 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

View File

@ -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<String> deleteExchange(@PathVariable(name = "id", required = false) Long offerId)
throws UserNotFoundException {
final User asker = this.getCurrentUser()
.orElseThrow(() -> new UserNotFoundException("Invalid token"));
final Optional<Exchange> 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);
}
}