public async Task <IActionResult> MainEdit(Guid guid, [Bind("Guid,Description,ActivityTimeInviteeTableIds")] ActivityEditInputModel inputModel) { if (guid != inputModel.Guid) { return(NotFound()); } if (ModelState.IsValid) { _repository.UpdateActivityDescription(guid, inputModel.Description); //TODO: ensure that invitees cannot change other peoples availabilities by playing around with the HTTP post shennanigans //TODO: See above but then for description of activitee and organiser if (!await _repository.TryUpdateActivityAtis(inputModel.Guid, inputModel.ActivityTimeInviteeTableIds)) { return(NotFound()); } return(RedirectToAction(nameof(MainEdit), new { guid = inputModel.Guid })); } var viewModel = GuidToActivityEditViewModel(guid); if (viewModel == null) { return(NotFound()); } return(View(viewModel)); }
public async Task <IActionResult> ActivityEdit(int id, [Bind("ActivityId,Name,Description,ActivityTimeInviteeTableIds")] ActivityEditInputModel inputModel) { if (id != inputModel.ActivityId) { return(NotFound()); } if (ModelState.IsValid) { try { var activity = new Activity() { ActivityId = inputModel.ActivityId, Name = inputModel.Name, Description = inputModel.Description }; _context.Activities.Update(activity); //What are the ati's that are recieved form Post? var activityTimeInviteesFromPost = new List <ActivityTimeInvitee>(); //Gather the ati's from Post int tempInviteeId; int tempTimeId; for (int i = 0; i < inputModel.ActivityTimeInviteeTableIds.Length; i++) { if (TryParseStringActivityTimeInviteeTableIds( inputModel.ActivityTimeInviteeTableIds[i], out tempInviteeId, out tempTimeId)) { activityTimeInviteesFromPost.Add(new ActivityTimeInvitee() { ActivityId = activity.ActivityId, InviteeId = tempInviteeId, TimeProposedId = tempTimeId }); } else { return(RedirectToAction()); } } //What are the ati's that are already in the database that connect a timeproposed whith an invitee. var activityTimeInviteesInDb = await _context.ActivityTimeInvitees .Where(ati => ati.ActivityId == inputModel.ActivityId && !(ati.TimeProposedId == null || ati.InviteeId == null)) .ToListAsync(); //Remove all ati's from dataBase from above list that are not in the post for (int i = 0; i < activityTimeInviteesInDb.Count; i++) { if (!activityTimeInviteesFromPost.Exists(ati => ati.TimeProposedId == activityTimeInviteesInDb[i].TimeProposedId && ati.InviteeId == activityTimeInviteesInDb[i].InviteeId)) { _context.ActivityTimeInvitees.Remove(activityTimeInviteesInDb[i]); } } //Add all ati's from the Post that are not already in the database for (int i = 0; i < activityTimeInviteesFromPost.Count; i++) { if (!activityTimeInviteesInDb.Exists(ati => ati.TimeProposedId == activityTimeInviteesFromPost[i].TimeProposedId && ati.InviteeId == activityTimeInviteesFromPost[i].InviteeId)) { _context.ActivityTimeInvitees.Add(new ActivityTimeInvitee() { ActivityId = inputModel.ActivityId, InviteeId = activityTimeInviteesFromPost[i].InviteeId, TimeProposedId = activityTimeInviteesFromPost[i].TimeProposedId }); } } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ActivityExists(inputModel.ActivityId)) { return(NotFound()); } else { throw; } } return(RedirectToAction()); } return(RedirectToAction(nameof(Index))); //The function below is used to parse the string output from the POST back to keys bool TryParseStringActivityTimeInviteeTableIds(string str, out int InviteeIdnum, out int timeIdnum) { InviteeIdnum = 0; timeIdnum = 0; var sb = new StringBuilder(); for (int i = 0; i < str.Length; i++) { if (str[i] != '+') { sb.Append(str[i]); } else { if (int.TryParse(sb.ToString(), out InviteeIdnum)) { sb.Clear(); } } } int.TryParse(sb.ToString(), out timeIdnum); return(!(InviteeIdnum == 0 || timeIdnum == 0)); } }