/// <summary> /// Adds a customer using the customerDataTransferObject /// </summary> public async Task <CustomerEntity> AddCustomerAsync(CustomerDataTransferObject customerDataTransferObject) { if (customerDataTransferObject == null) { throw new ArgumentNullException(); } if (!customerDataTransferObject.ValidateCustomerDataTransferObject()) { throw new ArgumentException(); } var newCustomerEntity = new CustomerEntity(); UpdateCustomerInfo(newCustomerEntity, customerDataTransferObject); var customerEntityAdded = Customers.Add(newCustomerEntity); if (customerEntityAdded?.Entity == null) { return(null); } await SaveChangesAsync(); return(customerEntityAdded.Entity); }
public async Task <ObjectResult> PutAsync(Guid id, [FromBody] CustomerDataTransferObject customerDataTransferObject) { if (customerDataTransferObject == null || !customerDataTransferObject.ValidateCustomerDataTransferObject()) { _logger.LogError(BuildLogInfo(nameof(PutAsync), "CustomerInfoInvalid")); return(BadRequest(BuildStringFromResource("CustomerInfoInvalid"))); } _logger.LogInformation(BuildLogInfo(nameof(PutAsync), "LoggingUpdatingCustomer", id)); if (!_customersDataProvider.CustomerExists(id)) { _logger.LogError(BuildLogInfo(nameof(PutAsync), "CustomerNotFound", id)); return(new NotFoundObjectResult(BuildStringFromResource("CustomerNotFound", id))); } var customerDataActionResult = await _customersDataProvider.TryUpdateCustomerAsync(id, customerDataTransferObject); if (!customerDataActionResult.IsSuccess) { _logger.LogError(BuildLogInfo(nameof(PutAsync), "UnexpectedServerError")); return(StatusCode(StatusCodes.Status500InternalServerError, BuildStringFromResource("UnexpectedServerError"))); } _logger.LogInformation(BuildLogInfo(nameof(PutAsync), "LoggingUpdatedCustomer", id)); return(Ok(customerDataActionResult.CustomerEntity)); }
/// <summary> /// Updates an existing customer's information with the customerDataTransferObject /// </summary> private void UpdateCustomerInfo(CustomerEntity customerEntity, CustomerDataTransferObject customerDataTransferObject) { if (customerEntity == null || customerDataTransferObject == null || !customerDataTransferObject.ValidateCustomerDataTransferObject()) { throw new ArgumentException(); } foreach (var item in customerDataTransferObject.GetType().GetTypeInfo().GetProperties()) { var desinationPropertyInfo = typeof(CustomerEntity).GetProperty(item.Name); desinationPropertyInfo?.SetValue(customerEntity, item.GetValue(customerDataTransferObject)); } }
public async Task <ObjectResult> PostAsync([FromBody] CustomerDataTransferObject customerDataTransferObject, [FromServices] ResourceManager resourceManager) { if (customerDataTransferObject == null || !customerDataTransferObject.ValidateCustomerDataTransferObject()) { _logger.LogError(BuildLogInfo(nameof(PostAsync), "CustomerInfoInvalid")); return(BadRequest(BuildStringFromResource("CustomerInfoInvalid"))); } var customerName = $"{customerDataTransferObject.FirstName} {customerDataTransferObject.LastName}"; _logger.LogInformation(BuildLogInfo(nameof(PostAsync), "LoggingAddingCustomer", customerName)); var customerDataActionResult = await _customersDataProvider.TryAddCustomerAsync(customerDataTransferObject); if (!customerDataActionResult.IsSuccess) { _logger.LogError(BuildLogInfo(nameof(PostAsync), "UnexpectedServerError")); return(StatusCode(StatusCodes.Status500InternalServerError, BuildStringFromResource("UnexpectedServerError"))); } _logger.LogInformation(BuildLogInfo(nameof(PostAsync), "LoggingAddedCustomer", customerName)); return(Ok(customerDataActionResult.CustomerEntity)); }