public async Task <IActionResult> UpdateCustomer(int id, [FromBody] CustomerToUpdate customer) { var toUpdateCustomer = await _context.Customer.FirstOrDefaultAsync(c => c.Customerid == id); if (toUpdateCustomer == null) { await TimeStampError(); return(NoContent()); } // map our form data to our updated model _mapper.Map(customer, toUpdateCustomer); await TimeStampTransaction(); return(Ok(await _context.SaveChangesAsync())); }
public async Task <ReturnResponse> UpdateCustomer(int customerId, CustomerToUpdate customer) { if (customer == null) { return(new ReturnResponse() { StatusCode = Utils.ObjectNull, StatusMessage = Utils.StatusMessageObjectNull }); } //GET USER THAT MADE THIS REQUEST var userTypeIdClaim = _httpContextAccessor.HttpContext.User.Claims.FirstOrDefault(a => a.Type == ClaimTypes.NameIdentifier); if (userTypeIdClaim == null) { return(new ReturnResponse() { StatusCode = Utils.UserClaimNotFound, StatusMessage = Utils.StatusMessageUserClaimNotFound }); } var userTypeIdVal = Convert.ToInt32(userTypeIdClaim.Value); if ((customerId != customer.CustomerId) || (customerId != userTypeIdVal)) { return(new ReturnResponse() { StatusCode = Utils.BadRequest, StatusMessage = Utils.StatusMessageBadRequest }); } var customerDetails = await _dataContext.Customer.Where(a => a.CustomerId == customerId).FirstOrDefaultAsync(); if (customerDetails == null) { return(new ReturnResponse() { StatusCode = Utils.NotFound, StatusMessage = Utils.StatusMessageNotFound }); } var customerToUpdate = _mapper.Map(customer, customerDetails); var updateResult = _globalRepository.Update(customerToUpdate); if (!updateResult) { return(new ReturnResponse() { StatusCode = Utils.NotSucceeded, StatusMessage = Utils.StatusMessageNotSucceeded }); } var saveResult = await _globalRepository.SaveAll(); if (!saveResult.HasValue) { return(new ReturnResponse() { StatusCode = Utils.SaveError, StatusMessage = Utils.StatusMessageSaveError }); } if (!saveResult.Value) { return(new ReturnResponse() { StatusCode = Utils.SaveNoRowAffected, StatusMessage = Utils.StatusMessageSaveNoRowAffected }); } return(new ReturnResponse() { StatusCode = Utils.Success, StatusMessage = Utils.StatusMessageSuccess, ObjectValue = customerToUpdate }); }
public async Task <ActionResult> PutCustomerManagement([FromRoute] int customerId, [FromBody] CustomerToUpdate customerToUpdate) { var dbTransaction = await _context.Database.BeginTransactionAsync(); var result = await _customerManagementRepository.UpdateCustomer(customerId, customerToUpdate); if (result.StatusCode == Utils.Success) { var customer = _mapper.Map <CustomerResponse>((Customer)result.ObjectValue); result.ObjectValue = customer; await dbTransaction.CommitAsync(); return(StatusCode(StatusCodes.Status200OK, result)); } else { await dbTransaction.RollbackAsync(); return(StatusCode(StatusCodes.Status400BadRequest, result)); } }