public IActionResult Leave([FromBody] GroupIdModel model) { var user = HttpContext.User; FollowGroupReturn ret = new FollowGroupReturn(); try { Claim idClaim = User.FindFirst("sub"); string userId = idClaim.Value; ret = _userInfoDataService.UnfollowGroup(userId, model.GroupId); if (ret.IsActionSucceed || ret.LastFollowState == GroupFollowState.Unfollowed) { _bus.Publish <UserUnfollowedGroupAction>(new UserUnfollowedGroupAction() { DateUtcAction = DateTime.UtcNow, GroupId = model.GroupId, UserId = userId }); } return(Ok(Json(ret))); } catch (Exception ex) { ret.ErrorInformation.ErrorType = ErrorType.NoAction; ret.ErrorInformation.UserInformation = ex.Message; ret.IsActionSucceed = false; return(Ok(Json(ret))); } }
public FollowGroupReturn FollowGroup(string appUserId, int groupId) { FollowGroupReturn ret = new FollowGroupReturn(); ret.IsActionSucceed = true; // Check if relation exist UserGroup grp = _context.SetChild <UserGroup>().FirstOrDefault(p => p.GroupId == groupId && p.UserId == appUserId); // If not if (grp == null) { UserGroup newUserGroupRelation = new UserGroup() { DateUtcFollowed = DateTime.UtcNow, GroupId = groupId, UserId = appUserId, GroupFollowState = GroupFollowState.Followed, UserReputationInGroup = 0 }; _context.SetChild <UserGroup>().Add(newUserGroupRelation); ret.LastFollowState = GroupFollowState.Followed; // If exist before } else if (grp.GroupFollowState == GroupFollowState.Unfollowed) { grp.GroupFollowState = GroupFollowState.Followed; } else if (grp.GroupFollowState == GroupFollowState.Banned) { ret.IsActionSucceed = false; ret.LastFollowState = GroupFollowState.Banned; ret.ErrorInformation.UserInformation = "You have been blocked from interest."; } else { ret.IsActionSucceed = false; ret.LastFollowState = GroupFollowState.Followed; ret.ErrorInformation.UserInformation = "You have already following interest."; } // Refresh Cache int[] prevCacheIds = _userLikesCacheService.GetUserFollowingGroupsIds(appUserId); if (prevCacheIds == null) { prevCacheIds = new int[] { }; } else { prevCacheIds = prevCacheIds.Append(groupId).ToArray(); } _userLikesCacheService.SetUserFollowingGroupsIds(appUserId, prevCacheIds, 40); _context.SaveChanges(); return(ret); }
public FollowGroupReturn UnfollowGroup(string appUserId, int groupId) { FollowGroupReturn ret = new FollowGroupReturn { IsActionSucceed = true }; // Check if relation exist UserGroup grp = _context.SetChild <UserGroup>().FirstOrDefault(p => p.GroupId == groupId && p.UserId == appUserId); // If not if (grp == null) { ret.IsActionSucceed = false; ret.LastFollowState = GroupFollowState.Unfollowed; ret.ErrorInformation.UserInformation = "You are not following interest."; // If exist before } else if (grp.GroupFollowState == GroupFollowState.Unfollowed) { ret.IsActionSucceed = false; ret.LastFollowState = GroupFollowState.Unfollowed; ret.ErrorInformation.UserInformation = "You are not following interest."; } else if (grp.GroupFollowState == GroupFollowState.Followed) { grp.GroupFollowState = GroupFollowState.Unfollowed; _context.SetChild <UserGroup>().Update(grp); ret.IsActionSucceed = true; ret.LastFollowState = GroupFollowState.Unfollowed; } else { grp.GroupFollowState = GroupFollowState.Unfollowed; _context.SetChild <UserGroup>().Update(grp); ret.IsActionSucceed = true; ret.LastFollowState = GroupFollowState.Unfollowed; } // Refresh Cache int[] prevCacheIds = _userLikesCacheService.GetUserFollowingGroupsIds(appUserId); if (prevCacheIds == null) { return(ret); } prevCacheIds = prevCacheIds.Where(p => p != groupId).ToArray(); _userLikesCacheService.SetUserFollowingGroupsIds(appUserId, prevCacheIds, 40); _context.SaveChanges(); return(ret); }