Esempio n. 1
0
        public async Task <IHttpActionResult> Follow(int questionId)
        {
            var userId = User.Identity.GetUserId();

            if (userId != null)
            {
                string   s;
                Question q = await db.Questions.FindAsync(questionId);

                var followed = q.FollowQuestions.FirstOrDefault(x => x.followedBy == userId);
                if (followed != null)
                {
                    db.FollowQuestions.Remove(followed);
                    db.SaveChanges();
                    s = "Follow";
                    return(Ok(s));
                }
                FollowQuestion f = new FollowQuestion();
                f.followedBy = userId;
                f.questionId = questionId;
                db.FollowQuestions.Add(f);
                db.SaveChanges();
                s = "UnFollow";
                return(Ok(s));
            }
            else
            {
                return(BadRequest("You are not login"));
            }
        }
Esempio n. 2
0
        //取消问题关注
        public ActionResult _followQuestion(int id)
        {
            FollowQuestion fq = db.FollowQuestions.First(d => d.FollowingQID == id);
            Question       q  = db.Questions.Find(id); //修改问题关注数量

            q.QFollowNum--;
            User u = db.Users.Find(fq.FQUserID);

            u.UFQuestionNum++;
            db.FollowQuestions.Remove(fq); //删除一条关注
            db.SaveChanges();
            return(JavaScript("$('.unfollow-button').css('display','none');$('.follow-button').css('display','block');"));
        }
Esempio n. 3
0
        //关注问题
        public ActionResult followQuestion(int id)
        {
            //在focusQuestion数据库表增加一条关注
            FollowQuestion fq = new FollowQuestion();

            fq.FollowingQID = id;
            if (getCookie("id") == -1)
            {
                return(RedirectToAction("index", "index"));
            }
            fq.FQUserID = getCookie("id");
            db.FollowQuestions.Add(fq);
            Question q = db.Questions.Find(id);//修改问题关注数量

            q.QFollowNum++;
            User u = db.Users.Find(fq.FQUserID);

            u.UFQuestionNum++;
            db.SaveChanges();
            return(JavaScript("$('.unfollow-button').css('display','block');$('.follow-button').css('display','none');"));
        }
Esempio n. 4
0
        //获得所关注用户的所有动态
        //参数:当前用户id
        public List <UserActivity> getUserActivity(int id)
        {
            List <UserActivity>      UA    = new List <UserActivity>();
            IEnumerable <FollowUser> users = db.FollowUsers.Where(d => d.FUUserID == id).ToList();

            for (var i = 0; i < users.Count(); i++)
            {
                UserActivity temp = new UserActivity();
                temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID);
                temp.answer  = new Answer();
                int uID = temp.keyUser.UID;
                IEnumerable <Answer> tempA = db.Answers.Where(d => d.AUserID == uID);
                if (tempA.Count() == 0)
                {
                    continue;
                }
                tempA         = tempA.OrderByDescending(d => d.ATime);
                temp.answer   = tempA.FirstOrDefault(d => d.AUserID == uID);
                temp.topic    = null;
                temp.question = db.Questions.First(d => d.QID == temp.answer.AQuestionID);
                temp.time     = temp.answer.ATime;
                temp._time    = getTimeSpan(temp.time);
                temp.flag     = 'a';//回答了某个问题
                UA.Add(temp);
            }
            for (var i = 0; i < users.Count(); i++)
            {
                UserActivity temp = new UserActivity();
                temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID);
                int uID = temp.keyUser.UID;
                temp.question = new Question();
                IEnumerable <Question> tempA = db.Questions.Where(d => d.QUserID == uID);
                if (tempA.Count() == 0)
                {
                    continue;
                }
                tempA         = tempA.OrderByDescending(d => d.QTime);
                temp.question = tempA.FirstOrDefault(d => d.QUserID == uID);
                temp.time     = temp.question.QTime;
                temp._time    = getTimeSpan(temp.time);
                temp.answer   = null;
                temp.topic    = null;
                temp.flag     = 'q';//关注用户提了问题
                if (temp.question != null)
                {
                    UA.Add(temp);
                }
            }
            for (var i = 0; i < users.Count(); i++)
            {
                UserActivity temp = new UserActivity();
                temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID);
                int uID = temp.keyUser.UID;
                temp.question = null;
                temp.answer   = null;
                IEnumerable <FollowTopic> ft = db.FollowTopics.Where(d => d.FTUserID == uID);
                if (ft.Count() == 0)
                {
                    continue;
                }
                ft = ft.OrderByDescending(d => d.FTID);
                FollowTopic a = ft.FirstOrDefault(d => d.FTUserID == uID);
                if (a == null)
                {
                    continue;
                }
                temp.topic = db.Topics.FirstOrDefault(d => d.TID == a.FollowingTID);
                if (temp.topic == null)
                {
                    continue;
                }
                //TODO: 修改数据库后更改此处,应该是followTopic的时间
                temp.time  = a.FTTime;
                temp._time = getTimeSpan(temp.time);
                temp.flag  = 't';//关注了话题
                UA.Add(temp);
            }

            for (var i = 0; i < users.Count(); i++)
            {
                UserActivity temp = new UserActivity();
                temp.keyUser = db.Users.Find(users.ToList()[i].FollowingUID);
                temp.answer  = null;
                IEnumerable <FollowQuestion> ft = db.FollowQuestions.Where(d => d.FQUserID == temp.keyUser.UID);
                if (ft.Count() == 0)
                {
                    continue;
                }
                ft = ft.OrderByDescending(d => d.FQID);
                FollowQuestion fq = ft.FirstOrDefault();
                if (fq == null)
                {
                    continue;
                }
                temp.question = db.Questions.Find(fq.FollowingQID);
                //TODO: 修改数据库后更改此处,应该是followTopic的时间
                temp.time  = fq.FQTime;
                temp._time = getTimeSpan(temp.time);
                temp.flag  = 'q';//关注了问题
                UA.Add(temp);
            }
            return(UA);
        }