public async Task Invoke(CancelInvoice request) { var invoice = await _repository.GetByNumber(request.Number); if (invoice == null) { throw new InvoiceNotFoundException(request.Number); } if (invoice.Status == Status.Cancelled) { throw new InvalidInvoiceOperationException(request.Number, "Cancel"); } invoice.Status = Status.Cancelled; await _repository.Update(invoice); }
public async Task <ActionResult> Delete([FromRoute] int number) { try { var request = new CancelInvoice { Number = number }; await _useCases.Invoke(request); return(Ok()); } catch (InvoiceNotFoundException ex) { _logger.LogError(ex, "Invalid not found"); return(NotFound(new { message = ex.Message })); } catch (InvalidInvoiceOperationException ex) { _logger.LogError(ex, "Invalid operation"); return(BadRequest(new { message = ex.Message })); } catch (Exception ex) { _logger.LogError(ex, "Error cancelling invoice"); while (ex.InnerException != null) { ex = ex.InnerException; } return(StatusCode(500, new { message = ex.Message, details = ex.StackTrace })); } }