Exemple #1
0
 public AssoOrg_Npc(NpcOrg npcOrg)
 {
     OrgId        = npcOrg.OrgId;
     OrgOrder     = npcOrg.OrgOrder;
     BlurbNpcPage = npcOrg.BlurbNpcPage;
     BlurbOrgPage = npcOrg.BlurbOrgPage;
     MemberOrder  = npcOrg.MemberOrder;
     IsCurrent    = npcOrg.IsCurrent;
 }
        public ActionResult AssoEdit(short infoId, short orgId, string submit,
                                     List <AssoNpc_Org> npcs,
                                     List <AssoEvent_Org> events,
                                     List <AssoChar_Org> charas
                                     )
        {
            if (ModelState.IsValid)
            {
                #region Add Members
                List <short> currentNpcId = db.NpcOrgs.Where(x => x.OrgId == orgId).Select(x => x.NpcId).ToList();
                if (npcs != null)
                {
                    foreach (AssoNpc_Org member in npcs)
                    {
                        //if the association already exists
                        if (currentNpcId.Contains(member.NpcId))
                        {
                            NpcOrg toEdit = db.NpcOrgs.Where(x => x.NpcId == member.NpcId && x.OrgId == orgId).First();

                            //make the edits
                            toEdit.OrgOrder     = member.OrgOrder;
                            toEdit.BlurbNpcPage = member.BlurbNpcPage;
                            toEdit.BlurbOrgPage = member.BlurbOrgPage;
                            toEdit.MemberOrder  = member.MemberOrder;
                            toEdit.IsCurrent    = member.IsCurrent;

                            db.Entry(toEdit).State = EntityState.Modified;
                            db.SaveChanges();
                            currentNpcId.Remove(member.NpcId);
                        }
                        else
                        {
                            NpcOrg toAdd = new NpcOrg
                            {
                                OrgId        = orgId,
                                NpcId        = member.NpcId,
                                OrgOrder     = member.OrgOrder,
                                BlurbNpcPage = member.BlurbNpcPage,
                                BlurbOrgPage = member.BlurbOrgPage,
                                MemberOrder  = member.MemberOrder,
                                IsCurrent    = member.IsCurrent
                            };
                            db.NpcOrgs.Add(toAdd);
                        }
                    }
                }
                if (currentNpcId.Count != 0)
                {
                    foreach (short id in currentNpcId)
                    {
                        NpcOrg gone = db.NpcOrgs.Where(x => x.OrgId == orgId && x.NpcId == id).FirstOrDefault();
                        db.NpcOrgs.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion

                #region Add Events
                List <short> currentEventId = db.OrgEvents.Where(x => x.OrgId == orgId).Select(x => x.EventId).ToList();
                if (events != null)
                {
                    foreach (AssoEvent_Org assoEvent in events)
                    {
                        //if the association already exists
                        if (currentEventId.Contains(assoEvent.EventId))
                        {
                            OrgEvent toEdit = db.OrgEvents.Where(x => x.EventId == assoEvent.EventId && x.OrgId == orgId).First();
                            toEdit.Blurb           = assoEvent.Blurb;
                            db.Entry(toEdit).State = EntityState.Modified;
                            db.SaveChanges();
                            currentEventId.Remove(assoEvent.EventId);
                        }
                        else
                        {
                            OrgEvent toAdd = new OrgEvent
                            {
                                OrgId   = orgId,
                                EventId = assoEvent.EventId,
                                Blurb   = assoEvent.Blurb
                            };
                            db.OrgEvents.Add(toAdd);
                        }
                    }
                }
                if (currentEventId.Count != 0)
                {
                    foreach (short id in currentEventId)
                    {
                        OrgEvent gone = db.OrgEvents.Where(x => x.OrgId == orgId && x.EventId == id).FirstOrDefault();
                        db.OrgEvents.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion

                #region Add CharacterOrgs
                List <short> currentCharId = db.CharOrgs.Where(x => x.OrgId == orgId).Select(x => x.CharId).ToList();
                if (charas != null)
                {
                    foreach (AssoChar_Org assoChar in charas)
                    {
                        if (currentCharId.Contains(assoChar.CharId))
                        {
                            CharOrg toEdit = db.CharOrgs.Where(x => x.CharId == assoChar.CharId && x.OrgId == orgId).First();
                            toEdit.BlurbOrgPage      = assoChar.BlurbOrgPage;
                            toEdit.IsCurrent         = assoChar.IsCurrent;
                            toEdit.IsPublic          = assoChar.IsPublic;
                            toEdit.KatherineApproved = assoChar.KatherineApproved;
                            db.Entry(toEdit).State   = EntityState.Modified;
                            db.SaveChanges();
                            currentCharId.Remove(assoChar.CharId);
                        }
                        else
                        {
                            CharOrg toAdd = new CharOrg
                            {
                                OrgId             = orgId,
                                CharId            = assoChar.CharId,
                                BlurbOrgPage      = assoChar.BlurbOrgPage,
                                IsPublic          = assoChar.IsPublic,
                                IsCurrent         = assoChar.IsCurrent,
                                KatherineApproved = assoChar.KatherineApproved
                            };
                            db.CharOrgs.Add(toAdd);
                        }
                    }
                }
                if (currentCharId.Count != 0)
                {
                    foreach (short id in currentCharId)
                    {
                        CharOrg gone = db.CharOrgs.Where(x => x.OrgId == orgId && x.CharId == id).FirstOrDefault();
                        db.CharOrgs.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion


                return(Json(true));
            }
            //if model fails
            ViewBag.Npcs       = db.NPCs.OrderBy(n => n.Name).ToList();
            ViewBag.Events     = db.Events.OrderBy(e => e.Name).ToList();
            ViewBag.Characters = db.Characters.Where(i => i.IsApproved).OrderBy(c => c.CharacterName).ToList();
            var       org   = db.Orgs.Find(orgId);
            AssoOrgVM model = new AssoOrgVM {
                InfoId = infoId, OrgId = orgId, Submit = submit, Name = org.Name, AssoNpcs = npcs, AssoEvents = events, AssoChars = charas
            };
            return(View(model));
        }
Exemple #3
0
        public ActionResult AssoEdit(short infoId, short npcId, string submit,
                                     List <AssoClass_Npc> assoClasses,
                                     List <AssoOrg_Npc> orgs)
        {
            if (ModelState.IsValid)
            {
                #region Add Classes
                List <byte> currentClassId = db.ClassNPCs.Where(x => x.NpcId == npcId).Select(x => (byte)x.ClassId).ToList();
                if (assoClasses != null)
                {
                    foreach (AssoClass_Npc cn in assoClasses)
                    {
                        //if the association already exists
                        if (currentClassId.Contains(cn.ClassId))
                        {
                            ClassNPC toEdit = db.ClassNPCs.Where(x => x.ClassId == cn.ClassId && x.NpcId == npcId).First();
                            toEdit.ClassOrder      = cn.ClassOrder;
                            db.Entry(toEdit).State = EntityState.Modified;
                            db.SaveChanges();
                            currentClassId.Remove(cn.ClassId);
                        }
                        else
                        {
                            ClassNPC toAdd = new ClassNPC
                            {
                                NpcId      = npcId,
                                ClassId    = cn.ClassId,
                                ClassOrder = cn.ClassOrder
                            };
                            db.ClassNPCs.Add(toAdd);
                        }
                    }
                }
                if (currentClassId.Count != 0)
                {
                    foreach (byte id in currentClassId)
                    {
                        ClassNPC gone = db.ClassNPCs.Where(x => x.NpcId == npcId && x.ClassId == id).FirstOrDefault();
                        db.ClassNPCs.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion

                #region Add Orgs
                List <short> currentOrgId = db.NpcOrgs.Where(x => x.NpcId == npcId).Select(x => x.OrgId).ToList();
                if (orgs != null)
                {
                    foreach (AssoOrg_Npc assoOrg in orgs)
                    {
                        //if the association already exists
                        if (currentOrgId.Contains(assoOrg.OrgId))
                        {
                            NpcOrg toEdit = db.NpcOrgs.Where(x => x.OrgId == assoOrg.OrgId && x.NpcId == npcId).First();
                            toEdit.OrgOrder     = assoOrg.OrgOrder;
                            toEdit.BlurbNpcPage = assoOrg.BlurbNpcPage;
                            toEdit.BlurbOrgPage = assoOrg.BlurbOrgPage;
                            toEdit.MemberOrder  = assoOrg.MemberOrder;
                            toEdit.IsCurrent    = assoOrg.IsCurrent;

                            db.Entry(toEdit).State = EntityState.Modified;
                            db.SaveChanges();
                            currentOrgId.Remove(assoOrg.OrgId);
                        }
                        else
                        {
                            NpcOrg toAdd = new NpcOrg
                            {
                                NpcId        = npcId,
                                OrgId        = assoOrg.OrgId,
                                OrgOrder     = assoOrg.OrgOrder,
                                BlurbNpcPage = assoOrg.BlurbNpcPage,
                                BlurbOrgPage = assoOrg.BlurbOrgPage,
                                MemberOrder  = assoOrg.MemberOrder,
                                IsCurrent    = assoOrg.IsCurrent
                            };
                            db.NpcOrgs.Add(toAdd);
                        }
                    }
                }
                if (currentOrgId.Count != 0)
                {
                    foreach (short id in currentOrgId)
                    {
                        NpcOrg gone = db.NpcOrgs.Where(x => x.NpcId == npcId & x.OrgId == id).FirstOrDefault();
                        db.NpcOrgs.Remove(gone);
                    }
                }
                db.SaveChanges();
                #endregion
                return(Json(true));
            }
            //if model fails
            ViewBag.Orgs    = db.Orgs.ToList();
            ViewBag.Classes = db.Classes.ToList();
            var       npc   = db.NPCs.Find(npcId);
            AssoNpcVM model = new AssoNpcVM {
                InfoId = infoId, NpcId = npcId, Submit = submit, Name = npc.Name, AssoClasses = assoClasses, AssoOrgs = orgs
            };
            return(View(model));
        }