public async Task <Customer4A4> Get(Guid customerId) { using (var conn = new SqlConnection()) { var customerData = await conn.QuerySingleOrDefaultAsync <CustomerData4A>( "SELECT * FROM Customers WHERE CustomerId = @customerId", new { customerId }); var addressesData = await conn.QueryAsync <AddressData4A>( "SELECT * FROM Addresses WHERE Customer", new { customerId }); var nameResult = CustomerName.Create(customerData.Title, customerData.FirstName, customerData.LastName); var dobResult = Dob.Create(customerData.DateOfBirth); var idDocumentResult = IdDocument.Create(customerData.IdDocumentType, customerData.IdDocumentNumber); var addresses = addressesData.Select(a => Address4A.Create(a.AddressId, a.HouseNoOrName, a.Street, a.City, a.County, a.PostCode, a.CurrentAddress).Value); return(new Customer4A4(customerData.CustomerId, nameResult.Value, dobResult.Value, idDocumentResult.Value, addresses)); } }
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)); }