示例#1
0
        public async Task <DeleteInvoiceByIdResponse> Handle(DeleteInvoiceByIdRequest request, CancellationToken cancellationToken)
        {
            var query = new GetInvoiceQuery()
            {
                Id = request.Id
            };
            var getInvoice = await this.queryExecutor.Execute(query);

            if (getInvoice == null)
            {
                return(new DeleteInvoiceByIdResponse()
                {
                    Error = new ErrorModel(ErrorType.NotFound)
                });
            }
            var mappedCommand = this.mapper.Map <DataAccess.Entities.Invoice>(request);
            var command       = new DeleteInvoiceCommand()
            {
                Parameter = mappedCommand
            };
            var created = await this.commandExecutor.Execute(command);

            var response = new DeleteInvoiceByIdResponse()
            {
                Data = created
            };

            return(response);
        }
        public async Task <bool> Handle(DeleteInvoiceCommand request, CancellationToken cancellationToken)
        {
            if (!request.IsValid())
            {
                await _mediatorHandler.RaiseEvent(new DomainValidationEvent(request.ValidationResult.ToString()));

                return(false);
            }

            var oldInvoice = _invoiceRepository.GetById(request.Id);

            if (oldInvoice == null)
            {
                await _mediatorHandler.RaiseEvent(new NotFoundEvent(request.Id, "Invoice", "Invoice not found"));

                return(false);
            }

            _invoiceRepository.Delete(request.Id);

            await _mediatorHandler.RaiseEvent(new InvoiceDeletedEvent()
            {
                Old = oldInvoice
            });

            return(true);
        }
        public async Task <ActionResult> Delete([FromRoute] Guid id, DeleteInvoiceCommand command, CancellationToken cancellationToken)
        {
            command.SetInvoiceId(id);

            await Mediator.Send(command, cancellationToken);

            return(Ok());
        }
        public async Task <Response <bool> > Delete(string id)
        {
            if (!Guid.TryParse(id, out Guid guid))
            {
                return(FailureResponse <bool>(new Error("Invalid Guid"), System.Net.HttpStatusCode.BadRequest));
            }

            var invoiceCommand = new DeleteInvoiceCommand()
            {
                Id = guid
            };
            var statementCommand = new DeleteStatementByInvoiceIdCommand()
            {
                InvoiceId = guid
            };

            if (!await _mediatorHandler.SendCommand(statementCommand))
            {
                var validationEvent = _eventStore.GetEvent <DomainValidationEvent>();

                return(FailureResponse <bool>(validationEvent));
            }

            if (await _mediatorHandler.SendCommand(invoiceCommand))
            {
                var invoiceEvent = _eventStore.GetEvent <InvoiceDeletedEvent>();

                return(SuccessfulResponse(true, invoiceEvent));
            }
            else
            {
                var validationEvent = _eventStore.GetEvent <DomainValidationEvent>();

                return(FailureResponse <bool>(validationEvent));
            }
        }
 public DeleteInvoiceCommandTests()
 {
     _command    = new DeleteInvoiceCommand();
     _command.Id = Guid.NewGuid();
 }