Esempio n. 1
0
        public async Task <bool> Handle(CreateStatementCommand 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 DomainValidationEvent(
                        "Statement Invoice ID does not exist. Please select a valid value."));

                return(false);
            }

            var getByDate = _statementRepository.GetByDate(request.InvoiceId, request.Date);

            if (getByDate != null)
            {
                await _mediatorHandler.RaiseEvent(
                    new DuplicatedRecordEvent(
                        "Multiple",
                        "Statement",
                        "A Statement with {0} and {1} is already present. Please select another value."));

                return(false);
            }

            var model = _mapper.Map <Statement>(request);

            _statementRepository.Create(model);

            await _mediatorHandler.RaiseEvent(new StatementCreatedEvent()
            {
                New = model
            });

            return(true);
        }