public ActionResult DeletePost(string postid, string grpid)
        {
            int  pid = Convert.ToInt32(postid);
            int  gid = Convert.ToInt32(grpid);
            Post pst = new Post();

            try
            {
                using (DISCUSSEntities dbcontext = new DISCUSSEntities())
                {
                    var grp = dbcontext.Groups.Where(t => t.id == gid).Select(i => i).FirstOrDefault();
                    if (!string.IsNullOrEmpty(grp.groupContent))
                    {
                        var lstPost = DesrializeString(grp.groupContent);
                        pst = lstPost.Where(t => t.id == pid).Select(i => i).FirstOrDefault();
                        lstPost.Remove(pst);
                        var xEle      = new XElement("Posts", GetElement(lstPost));
                        var xdoc      = new XDocument(xEle);
                        var xmlstring = GetXMLAsString(DocumentExtensions.ToXmlDocument(xdoc));
                        grp.groupContent = xmlstring;
                        dbcontext.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.StackTrace);
                return(Json("Fail"));
            }
            Session["GroupId"] = gid;
            return(Json("Success"));
        }
        public ActionResult DeleteGroup(string university, string department, string term, string course, string id)
        {
            SearchGroup  sgmodel     = new SearchGroup();
            List <Group> tempLst     = new List <Group>();
            int          userid      = Convert.ToInt32(Session["id"]);
            int          grpid       = Convert.ToInt32(id);
            Membership   membermodel = new Membership();

            try
            {
                using (DISCUSSEntities dbcontext = new DISCUSSEntities())
                {
                    var model = dbcontext.Groups.Where(t => t.id == grpid).FirstOrDefault();
                    dbcontext.Groups.Remove(model);
                    var tempList = dbcontext.Memberships.Where(t => t.groupId == grpid).Select(i => i).ToList();
                    dbcontext.Memberships.RemoveRange(tempList);
                    dbcontext.SaveChanges();
                }
            } catch (Exception ex)
            {
                System.Console.Write(ex.StackTrace);
            }

            return(PartialView("_SearchGrid", sgmodel.GrpList));
        }
        public ActionResult ChangeUserDetails(DiscussUser model)
        {
            if (!ModelState.IsValid)
            {
                return(View("UserDetails", model));
            }
            try
            {
                if (model != null)
                {
                    using (DISCUSSEntities dbcontext = new DISCUSSEntities())
                    {
                        var model1 = dbcontext.DiscussUsers.Where(t => t.id == model.id).FirstOrDefault();
                        dbcontext.DiscussUsers.Remove(model1);
                        dbcontext.DiscussUsers.Add(model);
                        dbcontext.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.Write(ex.StackTrace);
            }

            return(RedirectToAction("HomePage", "Home"));
        }
 public ActionResult UserDetails(DiscussUser model)
 {
     if (!ModelState.IsValid)
     {
         return(View());
     }
     try
     {
         if (model != null)
         {
             using (DISCUSSEntities dbcontext = new DISCUSSEntities())
             {
                 dbcontext.DiscussUsers.Add(model);
                 dbcontext.SaveChanges();
             }
         }
     }
     catch (Exception ex)
     {
         System.Console.Write(ex.StackTrace);
     }
     Session["Role"] = model.userrole;
     Session["Name"] = model.FirstName + " " + model.LastName;
     return(RedirectToAction("SearchGroups", "Home"));
 }
        public async Task <ActionResult> Register(UserAccount model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Username, Email = model.Username
                };
                var result = await UserManager.CreateAsync(user, model.userpassword);

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    //After creating a new user and sign-in sucessfully, add the user UserAccount table in Database.
                    try
                    {
                        using (DISCUSSEntities dbContext = new DISCUSSEntities())
                        {
                            if (dbContext != null)
                            {
                                dbContext.UserAccounts.Add(model);
                                dbContext.SaveChanges();
                                Session.Add("Username", model.Username);
                            }
                        }
                        //Add id of the new user to the session .
                        using (DISCUSSEntities dbContext1 = new DISCUSSEntities())
                        {
                            var userDetails = dbContext1.UserAccounts.Where(t => t.Username == model.Username).FirstOrDefault();
                            if (userDetails != null)
                            {
                                var id = userDetails.id;
                                Session["id"] = id;
                            }
                        }
                        TempData["Username"]         = model.Username;
                        Session["AuthenticatedUser"] = true;
                        return(RedirectToAction("UserDetails"));
                    }
                    catch (Exception ex)
                    {
                        model.Msg = "User already exists !!";
                    }
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public ActionResult CreateGroup(Group model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            Membership memebermodel = new Membership();

            try
            {
                if (model != null)
                {
                    model.createdBy = Convert.ToInt32(Session["id"]);
                    using (DISCUSSEntities dbcontext = new DISCUSSEntities())
                    {
                        var duplicateGroup = dbcontext.Groups.Where(t => t.groupname == model.groupname.Trim() && t.term == model.term && t.university == model.university &&
                                                                    t.department == model.department && t.courseCode == model.courseCode).Select(i => i).FirstOrDefault();
                        if (duplicateGroup == null)
                        {
                            dbcontext.Groups.Add(model);
                            dbcontext.SaveChanges();
                        }
                        else
                        {
                            TempData["ErrorMsg"] = "Group name already exists !! Please either change the group name or delete existing group.";
                            return(View(model));
                        }
                    }
                    using (DISCUSSEntities dbcontext1 = new DISCUSSEntities())
                    {
                        int grpid = dbcontext1.Groups.Where(t => t.groupname == model.groupname &&
                                                            t.term == model.term && t.createdBy == model.createdBy && t.university == model.university &&
                                                            t.courseCode == model.courseCode).Select(i => i.id).FirstOrDefault();
                        memebermodel.groupId = grpid;
                        memebermodel.userId  = model.createdBy;
                        dbcontext1.Memberships.Add(memebermodel);
                        dbcontext1.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Console.Write(ex.StackTrace);
            }
            TempData["successMsg"] = "Group Successfully created !!";
            return(View(model));
        }
 public Post AddToDB(Post post, int pid, int gid)
 {
     using (DISCUSSEntities dbcontext = new DISCUSSEntities())
     {
         Group grp = dbcontext.Groups.Where(i => i.id == gid).Select(t => t).FirstOrDefault();
         if (!string.IsNullOrEmpty(grp.groupContent))
         {
             var lstPost = DesrializeString(grp.groupContent);
             var oldpost = lstPost.Where(i => i.id == pid).FirstOrDefault();
             post.Replies.OrderBy(i => i.PostedDate);
             var modifiedPostList = lstPost.Remove(oldpost);
             lstPost.Add(post);
             var xEle      = new XElement("Posts", GetElement(lstPost));
             var xdoc      = new XDocument(xEle);
             var xmlstring = GetXMLAsString(DocumentExtensions.ToXmlDocument(xdoc));
             grp.groupContent = xmlstring;
             dbcontext.SaveChanges();
         }
     }
     return(post);
 }
        public ActionResult NewPost(string postto, string title, string desc, string postby, string groupId)
        {
            List <Post> lstpost   = new List <Post>();
            Group       grp       = new Group();
            int         grpId     = Convert.ToInt32(groupId);
            Post        postModel = new Post();

            postModel.id               = Guid.NewGuid().GetHashCode();
            postModel.TopicTitle       = title;
            postModel.TopicDescription = desc;
            postModel.PostedTo         = postto;
            if (postby != "Name")
            {
                postModel.PostedBy = "Anonymous";
            }
            else
            {
                postModel.PostedBy = Convert.ToString(Session["Name"]);
            }
            postModel.PostedbyId = Convert.ToInt32(Session["id"]);
            postModel.PostedDate = DateTime.Now;
            postModel.Replies    = null;
            if (string.IsNullOrEmpty(postto) || string.IsNullOrEmpty(title) || string.IsNullOrEmpty(desc) || string.IsNullOrEmpty(postby))
            {
                TempData["FailureMsg"] = "All the fields are compulsory.";
                return(View(postModel));
            }
            lstpost.Add(postModel);
            using (DISCUSSEntities dbcontext = new DISCUSSEntities())
            {
                grp = dbcontext.Groups.Where(t => t.id == grpId).Select(i => i).FirstOrDefault();
                if (grp != null)
                {
                    try
                    {
                        if (!String.IsNullOrEmpty(grp.groupContent))
                        {
                            XmlDocument doc = new XmlDocument();
                            doc.LoadXml(grp.groupContent);
                            var     xEle   = GetElement(lstpost);
                            XmlNode xmlnde = doc.ImportNode(XElementExtensions.ToXmlElement(xEle.FirstOrDefault()), true);
                            doc.DocumentElement.AppendChild(xmlnde);
                            var xmlstring = doc.OuterXml;
                            grp.groupContent = xmlstring;
                            dbcontext.SaveChanges();
                        }
                        else
                        {
                            var xEle      = new XElement("Posts", GetElement(lstpost));
                            var xdoc      = new XDocument(xEle);
                            var xmlstring = GetXMLAsString(DocumentExtensions.ToXmlDocument(xdoc));
                            grp.groupContent = xmlstring;
                            dbcontext.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        return(View());
                    }
                }
            }

            Session["GroupId"] = grpId;
            return(Json("Success"));
        }
        public ActionResult JoinLeaveGroup(string action, int id)
        {
            SearchGroup  sgmodel     = new SearchGroup();
            List <Group> tempList    = new List <Group>();
            int          userid      = Convert.ToInt32(Session["id"]);
            Membership   membermodel = new Membership();

            try
            {
                using (DISCUSSEntities dbcontext = new DISCUSSEntities())
                {
                    var model = dbcontext.Groups.Where(t => t.id == id).FirstOrDefault();
                    tempList.Add(model);
                }

                foreach (var grp in tempList)
                {
                    if (action == "Join")
                    {
                        using (DISCUSSEntities dbContext = new DISCUSSEntities())
                        {
                            membermodel.groupId = id;
                            membermodel.userId  = userid;
                            dbContext.Memberships.Add(membermodel);
                            dbContext.SaveChanges();
                            grp.IsMember = true;
                            int modelCount = dbContext.Memberships.Where(t => t.groupId == grp.id).ToList().Count;
                            if (grp.estimateEnrollment.HasValue)
                            {
                                grp.SeatsLeft = (int)grp.estimateEnrollment - modelCount;
                            }
                            string name = dbContext.DiscussUsers.Where(t => t.id == grp.createdBy).Select(i => i.FirstName + " " + i.LastName).FirstOrDefault();
                            grp.Creator = name;
                        }
                    }
                    else
                    {
                        using (DISCUSSEntities dbcontext1 = new DISCUSSEntities())
                        {
                            var model = dbcontext1.Memberships.Where(t => t.groupId == id && t.userId == userid).Select(i => i).FirstOrDefault();
                            dbcontext1.Memberships.Remove(model);
                            dbcontext1.SaveChanges();


                            int modelCount = dbcontext1.Memberships.Where(t => t.groupId == grp.id).ToList().Count;
                            if (grp.estimateEnrollment.HasValue)
                            {
                                grp.SeatsLeft = (int)grp.estimateEnrollment - modelCount;
                            }
                            string name = dbcontext1.DiscussUsers.Where(t => t.id == grp.createdBy).Select(i => i.FirstName + " " + i.LastName).FirstOrDefault();
                            grp.Creator = name;
                        }
                    }
                }
                sgmodel.GrpList = tempList;
            }

            catch (Exception ex)
            { System.Console.Write(ex.StackTrace); }

            TempData["ShowDiv"] = 1;
            return(PartialView("_SearchGrid", sgmodel.GrpList));
        }