public async Task <IActionResult> GoUserStar(int id, bool isStar) { var data = new MoData(); HttpContext.TryGetUserInfo(out var userInfo); if (userInfo.Id == id) { data.IsOk = false; data.Msg = "你不能关注你自己"; return(Json(data)); } var user = await _uf.UserRepository.GetByIdAsync(id); if (user == null) { data.IsOk = false; data.Msg = "找不到指定的用户"; return(Json(data)); } var userStar = await _uf.UserStarRepository.GetAsync(x => x.StarUserId == user.Id && x.UserId == userInfo.Id); if (isStar) { // 关注 if (userStar != null) { data.IsOk = false; data.Msg = $"你已经关注{user.UserName}"; return(Json(data)); } var newUserStar = new UserStar { StarUserId = user.Id, UserId = userInfo.Id }; await _uf.UserStarRepository.InsertAsync(newUserStar); } else { // 取消关注 if (userStar == null) { data.IsOk = false; data.Msg = $"你还没有关注{user.UserName}"; return(Json(data)); } _uf.UserStarRepository.Delete(userStar); } await _uf.SaveChangesAsync(); data.IsOk = true; return(Json(data)); }
public ActionResult StarNewBee(Guid id) { NewBee newBee = NewBeeDataSvc.GetByID(id); string starKey = MyRedisKeys.Pre_UserStarCache + CurrentUser.ID; IEnumerable <UserStarCache> userStarCaches = MyRedisDB.GetSet <UserStarCache>(starKey); bool add = false; if (userStarCaches.Count() == 0) { IEnumerable <UserStar> userStars = UserStarDataSvc.GetByCondition(s => s.OwnerID == CurrentUser.ID); if (userStars.Count() > 0) { //添加收藏缓存 foreach (UserStar star in userStars) { MyRedisDB.SetAdd(starKey, new UserStarCache() { ObjID = newBee.ID, ObjType = star.ObjType }); } MyRedisDB.RedisDB.KeyExpire(starKey, DateTime.Now.AddHours(3)); //添加收藏 if (userStars.Where(s => s.ObjID == newBee.ID).Count() == 0) { add = true; } } else { add = true; } } else if (userStarCaches.Where(s => s.ObjID == newBee.ID).Count() == 0) { add = true; } if (add) { //添加收藏 UserStar star = new UserStar(); star.OwnerID = CurrentUser.ID; star.ObjID = newBee.ID; star.Title = newBee.Title; star.ObjType = (int)EnumObjectType.NewBee; UserStarDataSvc.Add(star); MyRedisDB.SetAdd(starKey, new UserStarCache() { ObjID = newBee.ID, ObjType = star.ObjType }); MyRedisDB.RedisDB.KeyExpire(starKey, DateTime.Now.AddHours(3)); } return(Json(new { msg = "done" })); }
public ActionResult StarDelete(Guid id) { UserStar star = UserStarDataSvc.GetByID(id); if (star.OwnerID != CurrentUser.ID) { return(Json(new { msg = "小伙子你想干嘛" })); } UserStarDataSvc.DeleteByID(id); string starKey = MyRedisKeys.Pre_UserStarCache + CurrentUser.ID; IEnumerable <UserStarCache> userStarCaches = MyRedisDB.GetSet <UserStarCache>(starKey); if (userStarCaches.Count() > 0) { UserStarCache starCache = userStarCaches.Where(s => s.ObjID == star.ObjID).FirstOrDefault(); if (starCache != null) { MyRedisDB.SetRemove(starKey, starCache); } } return(Json(new { msg = "done" })); }