Esempio n. 1
0
        public async Task Handle_DeleteStatementByInvoiceIdCommand_NotFound()
        {
            //arrange
            Guid invoiceId = Guid.NewGuid();
            var command = new DeleteStatementByInvoiceIdCommand()
            {
                InvoiceId = invoiceId
            };

            _mocker.GetMock<IInvoiceRepository>()
                .Setup(m => m.GetById(It.Is<Guid>(id => id == invoiceId)))
                .Returns(value: null)
                .Verifiable("IInvoiceRepository.GetById should have been called");

            _mocker.GetMock<IMediatorHandler>()
                .Setup(m => m.RaiseEvent(It.IsAny<NotFoundEvent>()))
                .Verifiable("An event NotFoundEvent should have been raised");

            //act
            var result = await _statementCommandHandler.Handle(command, CancellationToken.None);

            //assert
            _mocker.GetMock<IInvoiceRepository>().Verify();
            _mocker.GetMock<IMediatorHandler>().Verify();
            Assert.False(result);
        }
Esempio n. 2
0
        public async Task <bool> Handle(DeleteStatementByInvoiceIdCommand request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!request.IsValid())
            {
                await _mediatorHandler.RaiseEvent(
                    new DomainValidationEvent(request.ValidationResult.ToString()));

                return(false);
            }

            var invoice = _invoiceRepository.GetById(request.InvoiceId);

            if (invoice == null)
            {
                await _mediatorHandler.RaiseEvent(new NotFoundEvent(request.InvoiceId, "Statement", "Invoice not found."));

                return(false);
            }

            var deletedStatements = await _statementRepository.DeleteByInvoiceIdAsync(invoice.Id);

            foreach (var statement in deletedStatements)
            {
                await _mediatorHandler.RaiseEvent(new StatementDeletedEvent()
                {
                    Old = statement
                });
            }

            return(true);
        }
        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));
            }
        }
Esempio n. 4
0
 public DeleteStatementByInvoiceIdCommandTests()
 {
     _command           = new DeleteStatementByInvoiceIdCommand();
     _command.InvoiceId = Guid.NewGuid();
 }