public async Task <RedirectToActionResult> StartSession(/*[Bind("id")] int cardioSetId,*/ [FromQuery] int workoutHistoryId, [FromQuery] int cardioSetId) { var jsonPatch = new JsonPatchDocument(); jsonPatch.Replace("/datetimestart", DateTime.Now); var patchResult = await _cardioSetViewModelRepository.PartiallyUpdate(cardioSetId, jsonPatch); if (patchResult.HttpStatusCode == HttpStatusCode.Unauthorized || patchResult.HttpStatusCode == HttpStatusCode.Forbidden) { return(RedirectToAction("AccessDenied", "Authorization")); } //if ((int)patchResult.HttpStatusCode != 200) //{ // // Todo: Do something when posting failed //} return(RedirectToAction("HistoryDetails", "WorkoutHistory", new { workoutHistoryId = workoutHistoryId })); }
public async Task <RedirectToActionResult> PatchWorkoutName(int id, [FromForm] WorkoutOfmForPatch workoutOfmForPatch) { JsonPatchDocument jsonPatchDocument = new JsonPatchDocument(); jsonPatchDocument.Replace("/" + nameof(workoutOfmForPatch.Name), workoutOfmForPatch.Name); var queryResult = await _workoutViewModelRepository.PartiallyUpdate(id, jsonPatchDocument); if (queryResult.HttpStatusCode == HttpStatusCode.Unauthorized || queryResult.HttpStatusCode == HttpStatusCode.Forbidden) { return(RedirectToAction("AccessDenied", "Authorization")); } if ((int)queryResult.HttpStatusCode == 200) { // Todo: Do something when patching failed } return(RedirectToAction("Overview")); }
public async Task<RedirectToActionResult> PatchExerciseName(int id, [FromForm] ExerciseOfmForPatch exerciseOfmForPatch) { JsonPatchDocument jsonPatchDocument = new JsonPatchDocument(); jsonPatchDocument.Replace("/" + nameof(exerciseOfmForPatch.Name), exerciseOfmForPatch.Name); var patchResult = await _exerciseViewModelRepository.PartiallyUpdate(id, jsonPatchDocument); if (patchResult.HttpStatusCode == HttpStatusCode.Unauthorized || patchResult.HttpStatusCode == HttpStatusCode.Forbidden) { return RedirectToAction("AccessDenied", "Authorization"); } //if ((int)patchResult.HttpStatusCode != 200) //{ // // Todo: Do something when posting failed //} return RedirectToAction("Overview"); }
public async Task <RedirectToActionResult> SaveChangesForWeightLiftingSets(int workoutHistoryId, IFormCollection formCollection) { // The formCollection is a flat array (there is no nested item). // Each form item needs to be parsed and assigned to its unique weightliftingSet // Each form item's name is composed of the weightliftingSetId and the property name to be patched var listWls = new List <WeightLiftingSetViewModel>(); var regexProperty = new Regex(@"^\w+-{1}\d+-{1}\w+$"); // For example "CurrentWeightLiftingSetId-98-RepetitionsFull" foreach (var formItem in formCollection) { if (regexProperty.IsMatch(formItem.Key)) { if (formItem.Key.ToLower().Contains("CurrentWeightLiftingSet".ToLower())) { // Getting the weightliftingSetId int firstIndexOfIdInString = formItem.Key.IndexOf("-") + 1; int lengthOfIdString = formItem.Key.LastIndexOf("-") - firstIndexOfIdInString; var weightLiftingSetId = Int32.Parse(formItem.Key.Substring(firstIndexOfIdInString, lengthOfIdString)); var propertyName = formItem.Key.Substring(formItem.Key.LastIndexOf("-") + 1); var weightLiftingSet = listWls.FirstOrDefault(wls => wls.Id == weightLiftingSetId); if (weightLiftingSet == null) { // Creating not yet created weightLiftingSetViewModel listWls.Add(new WeightLiftingSetViewModel() { Id = weightLiftingSetId }); weightLiftingSet = listWls.FirstOrDefault(wls => wls.Id == weightLiftingSetId); } var property = weightLiftingSet?.GetType().GetProperty(propertyName); if (Int32.TryParse(formItem.Value, out int parsedFormValue)) { property?.SetValue(weightLiftingSet, parsedFormValue); } else if (string.IsNullOrWhiteSpace(formItem.Value)) { property?.SetValue(weightLiftingSet, null); } } } } // Now that we have collected all data for all weightliftingSets we can create and send the PATCH request // var jsonPatchCollection = new List<JsonPatchDocument>(); foreach (var wls in listWls) { var jsonPatchDocument = new JsonPatchDocument(); foreach (var prop in wls.GetType().GetProperties()) { if (prop.GetValue(wls) != null && !prop.Name.ToLower().Contains("id")) { var jsonPatchOperation = new Operation("replace", prop.Name, null, prop.GetValue(wls)); jsonPatchDocument.Operations.Add(jsonPatchOperation); } } var patchResult = await _weightLiftingSetViewModelRepository.PartiallyUpdate(wls.Id, jsonPatchDocument); ////.ConfigureAwait(false).GetAwaiter().GetResult(); // Must be called synchronously, or System.InvalidOperationException is thrown: This instance has already started one or more requests. Properties can only be modified before sending the first request. if (patchResult.HttpStatusCode == HttpStatusCode.Unauthorized || patchResult.HttpStatusCode == HttpStatusCode.Forbidden) { return(RedirectToAction("AccessDenied", "Authorization")); } //if ((int)patchResult.HttpStatusCode != 200) //{ // // Todo: Do something when patching failed //} } return(RedirectToAction("HistoryDetails", "WorkoutHistory", new { workoutHistoryId = workoutHistoryId })); }