public ActionResult likePost(int postid, string action_, string controller_, int ownerid, int userid = 0)
        {
            var con       = new SOCIALMEDIA_DBEntities();
            var likedpost = con.LIKEDPOSTS.Where(l => l.USERID == ownerid && l.POSTID == postid);

            if (likedpost.Count() == 0)
            {
                var post = con.POSTS.Find(postid);
                post.LIKES++;
                LIKEDPOST liked = new LIKEDPOST();
                var       last  = con.LIKEDPOSTS.OrderByDescending(l => l.ID);
                liked.ID     = (last.Count() != 0)? con.LIKEDPOSTS.OrderByDescending(l => l.ID).First().ID + 1 : 1;
                liked.USERID = ownerid;
                liked.POSTID = postid;
                con.LIKEDPOSTS.Add(liked);
            }
            else
            {
                var post = con.POSTS.Find(postid);
                post.LIKES--;
                con.LIKEDPOSTS.Remove(likedpost.First());
            }

            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid, ownerid = ownerid }));
        }
        public ActionResult createPost(string postContent, int ownerid, string action_, string controller_, HttpPostedFileBase attach)
        {
            POST post = new POST();

            post.CONTENT  = postContent;
            post.POSTDATE = DateTime.Now;
            post.LIKES    = 0;
            post.USERID   = ownerid;

            //upload attachments
            if (attach != null)
            {
                var fileName = Path.GetFileName(attach.FileName);
                var path     = Path.Combine(Server.MapPath("~/Data/Posts_Attachments/"), fileName);
                attach.SaveAs(path);

                post.MEDIA = "../Data/Posts_Attachments/" + fileName;
            }

            var con = new SOCIALMEDIA_DBEntities();

            var posts = con.POSTS.OrderByDescending(p => p.POSTID);

            post.POSTID = (posts.Count() != 0)? posts.First().POSTID + 1:1;
            con.POSTS.Add(post);

            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = ownerid }));
        }
        public ActionResult cancelFR(int userid, int ownerid, String action_, string controller_)
        {
            var con    = new SOCIALMEDIA_DBEntities();
            var friend = con.FOLLOWINGS.Where(f => f.USERID == userid && f.FOLLOWINGID == ownerid).First();

            con.FOLLOWINGS.Remove(friend);
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult editReply(int replyid, string action_, string controller_, string content, int userid)
        {
            var con   = new SOCIALMEDIA_DBEntities();
            var reply = con.REPLIES.Find(replyid);

            reply.CONTENT = content;
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult editComment(int commentid, string action_, string controller_, string content, int userid)
        {
            var con     = new SOCIALMEDIA_DBEntities();
            var comment = con.COMMENTS.Find(commentid);

            comment.CONTENT = content;
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult editPost(int postid, string action_, string controller_, string content, int userid)
        {
            var con  = new SOCIALMEDIA_DBEntities();
            var post = con.POSTS.Find(postid);

            post.CONTENT = content;
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult delReply(int replyid, string action_, string controller_, int userid = 0)
        {
            var con   = new SOCIALMEDIA_DBEntities();
            var reply = con.REPLIES.Find(replyid);

            con.REPLIES.Remove(reply);
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult delComment(int commentid, string action_, string controller_, int userid = 0)
        {
            var con     = new SOCIALMEDIA_DBEntities();
            var comment = con.COMMENTS.Find(commentid);
            var reps    = con.REPLIES.Where(r => r.COMMENTID == commentid);

            con.REPLIES.RemoveRange(reps);
            con.COMMENTS.Remove(comment);
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult unfriend(int userid, int ownerid, String action_, string controller_)
        {
            var con    = new SOCIALMEDIA_DBEntities();
            var follow = con.FOLLOWINGS.Where(f => f.USERID == userid && f.FOLLOWINGID == ownerid);

            if (follow.Count() == 0)
            {
                follow = con.FOLLOWINGS.Where(f => f.FOLLOWINGID == userid && f.USERID == ownerid);
            }

            con.FOLLOWINGS.Remove(follow.FirstOrDefault());
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult sendFR(int userid, int ownerid, String action_, string controller_)
        {
            var       con    = new SOCIALMEDIA_DBEntities();
            FOLLOWING follow = new FOLLOWING();

            //get last following id
            var arr    = con.FOLLOWINGS.OrderByDescending(f => f.ID);
            int lastId = (arr.Count() != 0) ?arr.First().ID : 0;

            follow.ID           = lastId + 1;
            follow.USERID       = userid;
            follow.FOLLOWINGID  = ownerid;
            follow.FR_CONFIRMED = false;
            //add user to the other user as temp friend
            con.FOLLOWINGS.Add(follow);
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult delPost(int postid, string action_, string controller_, int userid = 0)
        {
            var con      = new SOCIALMEDIA_DBEntities();
            var post     = con.POSTS.Find(postid);
            var comments = con.COMMENTS.Where(c => c.POSTID == postid);

            foreach (var c in comments)//remove replies for each comment
            {
                con.REPLIES.RemoveRange(con.REPLIES.Where(r => r.COMMENTID == c.COMMENTID));
            }
            var liked = con.LIKEDPOSTS.Where(l => l.POSTID == postid);

            con.COMMENTS.RemoveRange(comments);
            con.LIKEDPOSTS.RemoveRange(liked);
            con.POSTS.Remove(post);
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult createReply(int commentid, string replyContent, int ownerid, string action_, string controller_, int userid = 1)
        {
            REPLY reply = new REPLY();

            reply.CONTENT   = replyContent;
            reply.LIKES     = 0;
            reply.USERID    = ownerid;
            reply.COMMENTID = commentid;
            var con = new SOCIALMEDIA_DBEntities();

            //get the last id and ++ the new id
            var reps = con.REPLIES.OrderByDescending(r => r.REPLYID);

            reply.REPLYID = (reps.Count() != 0)? reps.First().REPLYID + 1:1;

            con.REPLIES.Add(reply);
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult createComment(int postid, string commentContent, int ownerid, string action_, string controller_, int userid = 1)
        {
            COMMENT comment = new COMMENT();

            comment.CONTENT = commentContent;
            comment.LIKES   = 0;
            comment.USERID  = ownerid;
            comment.POSTID  = postid;
            var con = new SOCIALMEDIA_DBEntities();

            //get the last id and ++ the new id
            var comms = con.COMMENTS.OrderByDescending(c => c.COMMENTID);

            comment.COMMENTID = (comms.Count() != 0)? comms.First().COMMENTID + 1:1;

            con.COMMENTS.Add(comment);
            con.SaveChanges();
            return(RedirectToAction(action_, controller_, new { userid = userid }));
        }
        public ActionResult Register(USER user)
        {
            var m   = new RegisterModel();
            var con = new SOCIALMEDIA_DBEntities();
            //check email , username
            var us = con.USERS.Where(u => u.EMAIL == user.EMAIL || u.USERNAME == user.USERNAME);

            if (us.Count() != 0)
            {
                m.status = "registered";
                return(View(m));
            }
            else
            {
                //get last id
                var ids = con.USERS.OrderByDescending(u => u.USERID);
                user.USERID = (ids.Count() != 0) ? ids.First().USERID + 1 : 1;
                con.USERS.Add(user);
                con.SaveChanges();
            }
            return(RedirectToAction("Login", "Login"));
        }
        public void upload(HttpPostedFileBase file, int ownerid, string dataPath, string type)
        {
            //upload Display Picture
            if (file != null)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path     = Path.Combine(Server.MapPath(dataPath), fileName);
                file.SaveAs(path);
                var con   = new SOCIALMEDIA_DBEntities();
                var owner = con.USERS.Find(ownerid);
                if (type == "dp")
                {
                    owner.DISPLAYPICTURE = "../Data/Display_Pictures/" + fileName;
                }
                else if (type == "bg")
                {
                    owner.BG = "../Data/BG/" + fileName;
                }

                con.SaveChanges();
            }
        }