Esempio n. 1
0
        public async Task <ActionResult <Guid> > Post([FromBody] CustomerRequest3A request)
        {
            // value objects validate their inputs
            var nameResult = CustomerName.Create(request.Title,
                                                 request.FirstName,
                                                 request.LastName);

            if (nameResult.Status == OperationStatus.ValidationFailure)
            {
                return(BadRequest(nameResult.ErrorMessages));
            }

            var dobResult = Dob.Create(request.DateOfBirth);

            if (dobResult.Status == OperationStatus.ValidationFailure)
            {
                return(BadRequest(dobResult.ErrorMessages));
            }

            var idDocumentResult = IdDocument.Create(request.IdDocumentType,
                                                     request.IdDocumentNumber);

            if (idDocumentResult.Status == OperationStatus.ValidationFailure)
            {
                return(BadRequest(idDocumentResult.ErrorMessages));
            }

            // convert request DTO to domain model
            var customer = new Customer3A(request.CustomerId,
                                          nameResult.Value,
                                          dobResult.Value,
                                          idDocumentResult.Value,
                                          request.Addresses.Select(a => new Address3A(a.HouseNoOrName,
                                                                                      a.Street,
                                                                                      a.City,
                                                                                      a.County,
                                                                                      a.PostCode,
                                                                                      a.CurrentAddress
                                                                                      )));

            // command just wrap domain model
            var response = await _mediator.Send(new CreateCustomerCommand3A(customer));

            var apiResponse = new CreateCustomerApiResponse3A(response.Value);

            if (response.Status == OperationStatus.Conflict)
            {
                return(Conflict(apiResponse));
            }

            return(Created($"/customers/customer/{apiResponse.CustomerId}", apiResponse));
        }
 public CreateCustomerCommand3A(Customer3A customer)
 {
     Customer = customer;
 }