// Loop throught and update necessary fields private Store UpdateExistingResource(Store existingStore, StoreUpdateResource updatedStore) { var result = existingStore; foreach (var item in updatedStore.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); }
public async Task <IActionResult> PutAsync(int id, [FromBody] StoreUpdateResource resource) { var existingStore = await _storeRepo.FindByIdAsync(id); if (existingStore == null) { return(NotFound()); } var updatedStore = UpdateExistingResource(existingStore, resource); try { _storeRepo.Update(updatedStore); await _storeRepo.SaveChangeAsync(); return(Ok(new { message = "Updated" })); } catch (Exception ex) { return(BadRequest(new List <string> { ex.Message })); } }