Beispiel #1
0
        public async Task <Response <StatementResponse> > GetById(string id)
        {
            if (!Guid.TryParse(id, out Guid guid))
            {
                return(FailureResponse <StatementResponse>(
                           new Error("Invalid Guid"),
                           System.Net.HttpStatusCode.BadRequest));
            }

            var result = _statementRepository.GetById(guid);

            if (result == null)
            {
                return(FailureResponse <StatementResponse>(
                           new Error("Statement not found"),
                           System.Net.HttpStatusCode.NotFound));
            }

            var data = _mapper.Map <Expenses.Domain.Models.Statement, StatementResponse>(result);

            return(SuccessfulResponse(data));
        }
Beispiel #2
0
        public async Task <bool> Handle(UpdateStatementCommand 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 oldStatement = _statementRepository.GetById(request.Id);

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

                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 && getByDate.Id != request.Id)
            {
                await _mediatorHandler.RaiseEvent(
                    new DuplicatedRecordEvent(
                        "Name",
                        "Statement",
                        "A Stement with {0} and {1} is already present. Please select another value."));

                return(false);
            }

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

            await _statementRepository.UpdateAsync(model);

            await _mediatorHandler.RaiseEvent(new StatementUpdatedEvent()
            {
                Old = oldStatement,
                New = model
            });

            return(true);
        }