Example #1
0
        public IActionResult Warjack(AddWarjackViewModel model)
        {
            if (ModelState.IsValid)
            {
                Warjack newJack = new Warjack();


                newJack.Name        = model.Name;
                newJack.ARM         = model.ARM;
                newJack.CMD         = model.CMD;
                newJack.DEF         = model.DEF;
                newJack.FA          = model.FA;
                newJack.MAT         = model.MAT;
                newJack.PointCost   = model.PointCost;
                newJack.RAT         = model.RAT;
                newJack.SPD         = model.SPD;
                newJack.STR         = model.STR;
                newJack.FA          = model.FA;
                newJack.Size        = model.Size;
                newJack.factionName = model.Faction;

                context.Warjacks.Add(newJack);
                context.SaveChanges();

                if (model.abilIDS != null)
                {
                    foreach (var abil in model.abilIDS)
                    {
                        WarjackAbillity NewSoloAbility = new WarjackAbillity();
                        NewSoloAbility.AbillityID = abil;
                        NewSoloAbility.WarjackID  = newJack.ID;
                        context.WarjackAbilities.Add(NewSoloAbility);
                        context.SaveChanges();
                    }
                }


                if (model.weapIDS != null)
                {
                    foreach (var weap in model.weapIDS)
                    {
                        WarjackWeapon NewSoloWeapon = new WarjackWeapon();
                        NewSoloWeapon.WeaponId  = weap;
                        NewSoloWeapon.WarjackId = newJack.ID;
                        context.WarjackWeapons.Add(NewSoloWeapon);
                        context.SaveChanges();
                    }
                }



                return(Redirect("/"));
            }

            return(View("Warjack", model));
        }
        public IActionResult Warjack(EditWarjackViewModel editModel)
        {
            Warjack editWarjack = context.Warjacks.Single(c => c.ID == editModel.soloID);



            editWarjack.ARM         = editModel.ARM;
            editWarjack.CMD         = editModel.CMD;
            editWarjack.DEF         = editModel.DEF;
            editWarjack.FA          = editModel.FA;
            editWarjack.MAT         = editModel.MAT;
            editWarjack.RAT         = editModel.RAT;
            editWarjack.Name        = editModel.Name;
            editWarjack.SPD         = editModel.SPD;
            editWarjack.PointCost   = editModel.PointCost;
            editWarjack.STR         = editModel.STR;
            editWarjack.factionName = editModel.Faction;
            editWarjack.Size        = editModel.Size;
            context.SaveChanges();

            editModel.currenntAbilIDs = context.WarjackAbilities.Where(c => c.WarjackID == editWarjack.ID).Select(x => x.AbillityID).ToList();

            editModel.currenntWeaponIDs = context.WarjackWeapons.Where(c => c.WarjackId == editWarjack.ID).Select(x => x.WeaponId).ToList();



            if (editModel.abilIDS != null)
            {
                foreach (var abil in editModel.abilIDS)
                {
                    if (!editModel.currenntAbilIDs.Contains(abil))
                    {
                        WarjackAbillity NewSoloAbility = new WarjackAbillity();
                        NewSoloAbility.AbillityID = abil;
                        NewSoloAbility.WarjackID  = editModel.soloID;
                        context.WarjackAbilities.Add(NewSoloAbility);
                        context.SaveChanges();
                    }



                    foreach (var currentAbil in editModel.currenntAbilIDs)
                    {
                        if (!editModel.abilIDS.Contains(currentAbil))
                        {
                            WarjackAbillity soloabil = (from s in context.WarjackAbilities where s.AbillityID == currentAbil where s.WarjackID == editModel.soloID select s).FirstOrDefault <WarjackAbillity>();
                            context.WarjackAbilities.Remove(soloabil);
                            context.SaveChanges();
                        }
                    }
                }
            }

            else
            {
                // delete all solo abils
                var soloAbils = context.WarjackAbilities.Where(c => c.WarjackID == editModel.soloID).ToList();

                foreach (var Abil in soloAbils)
                {
                    context.WarjackAbilities.Remove(Abil);
                    context.SaveChanges();
                }
            }



            if (editModel.weapIDS != null)
            {
                foreach (var weap in editModel.weapIDS)
                {
                    if (!editModel.currenntWeaponIDs.Contains(weap))
                    {
                        WarjackWeapon NewSoloWeapon = new WarjackWeapon();
                        NewSoloWeapon.WeaponId  = weap;
                        NewSoloWeapon.WarjackId = editModel.soloID;
                        context.WarjackWeapons.Add(NewSoloWeapon);
                        context.SaveChanges();
                    }



                    foreach (var weaps in editModel.currenntWeaponIDs)
                    {
                        if (!editModel.weapIDS.Contains(weaps))
                        {
                            WarjackWeapon soloWeap = (from s in context.WarjackWeapons where s.WeaponId == weap where s.WarjackId == editModel.soloID select s).FirstOrDefault <WarjackWeapon>();
                            context.WarjackWeapons.Remove(soloWeap);
                            context.SaveChanges();
                        }
                    }
                }
            }


            else
            {
                // delete all solo abils
                var soloWeaps = context.WarjackWeapons.Where(c => c.WarjackId == editModel.soloID).ToList();

                foreach (var Weap in soloWeaps)
                {
                    context.WarjackWeapons.Remove(Weap);
                    context.SaveChanges();
                }
            }



            return(Redirect("/View/Warjack/" + editModel.soloID));
        }