//取消话题关注 public ActionResult _followTopic(int id) { if (getCookie("id") == -1) { return(RedirectToAction("index", "index")); } int uid = getCookie("id"); FollowTopic fq = db.FollowTopics.Where(d => d.FollowingTID == id) .Where(d => d.FTUserID == uid).First(); Topic q = db.Topics.Find(id);//话题被关注数量- q.TFollowerNum--; User u = db.Users.Find(fq.FTUserID);//用户关注话题数目-- u.UFTopicNum--; db.FollowTopics.Remove(fq);//删除关注话题 db.SaveChanges(); return(JavaScript("$('.unfollow-button').css('display','none');$('.follow-button').css('display','block');")); }
public void UnFollow(Topic topic) { if (topic == null) { throw new ArgumentNullException("topic"); } User user = this.userProvider.CurrentUser; if (user == null) { throw new NoAuthenticatedUserFoundException(); } FollowTopic ft = this.topicRepo.ByUserAndTopic(topic, user); if (ft != null) { this.topicRepo.Delete(ft); } }
//关注话题 public ActionResult followTopic(int id) { FollowTopic fq = new FollowTopic(); fq.FollowingTID = id; if (getCookie("id") == -1) { return(RedirectToAction("index", "index")); } fq.FTUserID = getCookie("id"); db.FollowTopics.Add(fq); db.SaveChanges(); Topic q = db.Topics.Find(id); q.TFollowerNum++; User u = db.Users.Find(fq.FTUserID); u.UFTopicNum++; db.SaveChanges(); return(JavaScript("$('.unfollow-button').css('display','block');$('.follow-button').css('display','none');")); }
//获得所关注用户的所有动态 //参数:当前用户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); }