private void PopulateAssignedConditions(RunCombatEditCondition model) { var userID = Guid.Parse(User.Identity.GetUserId()); var service = new ConditionService(userID); var allConditions = service.GetConditionList(); if (model.ConditionIDs == null) { model.ConditionIDs = new List <int>(); } var characterConditions = new HashSet <int>(model.ConditionIDs); var viewModel = new List <RunCombatAssignedCondition>(); if (allConditions != null) { foreach (var condition in allConditions) { viewModel.Add(new RunCombatAssignedCondition { ConditionID = condition.ConditionID, ConditionName = condition.ConditionName, IsAssigned = characterConditions.Contains(condition.ConditionID) }); } } ViewBag.Conditions = viewModel; }
public ActionResult EditCondition(int id, string[] selectedConditions, RunCombatEditCondition model) { PopulateAssignedConditions(model); if (!ModelState.IsValid) { return(View(model)); } if (model.ID != id) { ModelState.AddModelError("", "ID does not match"); return(View(model)); } var svc = CreateRunCombatService(); svc.UpdateCharacterConditions(selectedConditions, model); if (svc.UpdateAttendanceConditions(model)) { TempData["SaveResult"] = "Conditions Updated"; return(RedirectToAction("RunCombat", new { id = model.CombatID, SelectedCharacter = model.CurrentTurn - 1 })); } ModelState.AddModelError("", "Conditions could not be updated"); return(View(model)); }
public RunCombatEditCondition GetEditCharacterConditionsModel(int id) { using (var ctx = new ApplicationDbContext()) { var query = ctx.Attendances .Include(e => e.Conditions) .Single(e => e.ID == id); RunCombatEditCondition model = new RunCombatEditCondition { ID = query.ID, CharacterID = query.CharacterID, CharacterName = query.Character.Name, CombatID = query.CombatID, }; List <int> conditionIDs = new List <int>(); foreach (var condition in query.Conditions) { conditionIDs.Add(condition.ConditionID); } model.ConditionIDs = conditionIDs; return(model); } }
public bool UpdateAttendanceConditions(RunCombatEditCondition model) { //Change attendances in db based on model using (var ctx = new ApplicationDbContext()) { var attendance = ctx .Attendances .Where(e => e.ID == model.ID) .Include(e => e.Conditions) .Single(); var newConditions = new HashSet <int>(model.ConditionIDs); var oldConditions = new HashSet <int>(); foreach (var condition in attendance.Conditions) { oldConditions.Add(condition.ConditionID); } var conditions = ctx.Conditions; foreach (var condition in conditions) { if (newConditions.Contains(condition.ConditionID)) { if (!oldConditions.Contains(condition.ConditionID)) { attendance.Conditions.Add(condition); } } else { if (oldConditions.Contains(condition.ConditionID)) { attendance.Conditions.Remove(condition); } } } var actual = ctx.SaveChanges(); return(actual >= 1); } }
public void UpdateCharacterConditions(string[] selectedConditions, RunCombatEditCondition model) { //Update conditions on the model based on checkboxes if (selectedConditions == null) { selectedConditions = new string[0] { }; model.ConditionIDs = new List <int>(); } if (model.ConditionIDs == null) { model.ConditionIDs = new List <int>(); } var selectedConditionsHS = new HashSet <string>(selectedConditions); var characterConditions = new HashSet <int>(model.ConditionIDs); using (var ctx = new ApplicationDbContext()) { var conditions = ctx.Conditions; foreach (var condition in conditions) { if (selectedConditionsHS.Contains(condition.ConditionID.ToString())) { if (!characterConditions.Contains(condition.ConditionID)) { model.ConditionIDs.Add(condition.ConditionID); } } else { if (characterConditions.Contains(condition.ConditionID)) { model.ConditionIDs.Remove(condition.ConditionID); } } } } }