Ejemplo n.º 1
0
        public IActionResult UpdateCustomer(CustomerUpdateResource customerUpdateResource)
        {
            try {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState.GetErrorMessages()));
                }
                else
                {
                    Customer customer = mapper.Map <CustomerUpdateResource, Customer>(customerUpdateResource);
                    // var customers = this._CustomerRepo.FindByCustomerId(identityResource.Id);

                    // if (customers != null) {

                    var custom = this._CustomerRepo.UpdateCustomer(customer);
                    if (custom != null)
                    {
                        return(Ok(new ApiResponse <Customer> {
                            Status = true, Entry = custom
                        }));
                    }
                    return(BadRequest(new ApiResponse <Customer> {
                        Status = false, Entry = null
                    }));
                    //}
                    //return BadRequest(new ApiResponse<Customer> {Status=false,Entry=null,ErrorCode=   ErrorMessageCode.UserNotFound });
                }
            }
            catch (Exception ex) {
                _Logger.LogError(ex.Message);
                return(BadRequest(new ApiResponse <Customer> {
                    Status = false, Entry = null
                }));
            }
        }
Ejemplo n.º 2
0
        // Loop throught and update necessary fields
        private Customer UpdateExistingResource(Customer existingCustomer, CustomerUpdateResource updatedCustomer)
        {
            var result = existingCustomer;

            foreach (var item in updatedCustomer.ToDictionary())
            {
                var key   = item.Key;
                var value = item.Value;
                if (value is bool == false && value != null && value is int == false)
                {
                    result.GetType().GetProperty(key).SetValue(result, value, null);
                }
            }
            return(result);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] CustomerUpdateResource resource)
        {
            var exitstingCustomer = await _customerRepo.FindByIdAsync(id);

            if (exitstingCustomer == null)
            {
                return(NotFound());
            }
            var updatedCustomer = UpdateExistingResource(exitstingCustomer, resource);

            try
            {
                _customerRepo.Update(updatedCustomer);
                await _customerRepo.SaveChangeAsync();

                return(Ok(new { message = "Updated" }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new List <string> {
                    ex.Message
                }));
            }
        }