public IActionResult Unfollow(List <Int32> values)
        {
            Follower follower = new Follower();

            follower.who_id  = values[1];
            follower.whom_id = values[0];
            _context.Remove(follower);
            _context.SaveChanges();

            return(RedirectToAction("Timeline"));
        }
示例#2
0
        public IActionResult Unfollow(List <Int32> values)
        {
            Follower follower = new Follower();

            follower.who_id  = values[1];
            follower.whom_id = values[0];
            _context.Remove(follower);
            _context.SaveChanges();
            _logger.LogInformation("{whoID}, is not following {whomID} anymore.", follower.who_id.ToString(), follower.whom_id.ToString());
            return(RedirectToAction("Timeline"));
        }
示例#3
0
        public IActionResult Follow(string username, int no_followers = 100)
        {
            var verb   = _accessor.HttpContext.Request.Method.ToString();
            var json   = _accessor.HttpContext.Request.ReadFromJsonAsync <ApiDataFollow>();
            int userid = GetUserId(username);

            UpdateLatest();
            if (userid == -1)
            {
                _logger.LogWarning("No user exist for {Username}.", username);
                return(BadRequest("error"));
            }

            if (verb == "POST" && json.Result.follow != null)
            {
                string userToFollow   = json.Result.follow;
                int    userToFollowId = GetUserId(userToFollow); //find the id of user to follow
                if (userToFollowId == -1)
                {
                    return(NotFound());
                }

                var followersOfUserId = _context.follower.Where(f => f.who_id == userid).ToList();

                if (followersOfUserId.Where(f => f.whom_id == userToFollowId).Any())
                {
                    //_logger.LogInformation("{whoID}, already follows {whomID}.", userid.ToString(), userToFollowId.ToString());
                    return(BadRequest(username + " already follows " + userToFollow));
                }

                Follower follower = new Follower();
                follower.who_id  = userid;
                follower.whom_id = userToFollowId;
                _context.Add(follower);
                _context.SaveChanges();
                //_logger.LogInformation("{whoID}, now follows {whomID}.", userid.ToString(), userToFollowId.ToString());
                return(NoContent());
            }
            else if (verb == "POST" && json.Result.unfollow != null)
            {
                string userToUnfollow   = json.Result.unfollow;
                int    userToUnfollowId = GetUserId(userToUnfollow);
                if (userToUnfollowId == -1)
                {
                    return(NotFound());
                }

                var followersOfUserId = _context.follower.AsNoTracking().Where(f => f.who_id == userid).ToList();

                if (!followersOfUserId.Where(f => f.whom_id == userToUnfollowId).Any())
                {
                    //_logger.LogInformation("{whoID}, is not currently following {whomID}.", userid.ToString(), userToUnfollowId.ToString());
                    return(BadRequest(username + " isn't following " + userToUnfollow + " to begin with"));
                }

                Follower follower = new Follower();
                follower.who_id  = userid;
                follower.whom_id = userToUnfollowId;
                _context.Remove(follower);
                _context.SaveChanges();
                //_logger.LogInformation("{whoID}, is not following {whomID} anymore.", userid.ToString(), userToUnfollowId.ToString());
                return(NoContent());
            }
            else if (verb == "GET")   //needs refactoring to use ORM instead of query
            {
                var query = (from f in _context.follower

                             join u in _context.user on f.whom_id equals u.user_id
                             where f.who_id == userid
                             select
                             new { u.username })
                            .Take(no_followers).ToList();
                List <string> Follows = new List <string>();
                foreach (var item in query)
                {
                    Follows.Add(item.username);
                }
                ApiDataFollows returnfollows = new ApiDataFollows {
                    follows = Follows
                };
                var jsonreturn = JsonSerializer.Serialize(returnfollows);
                //_logger.LogInformation("Fetched follower list for {whoID}", userid.ToString());
                return(Ok(jsonreturn));
            }
            return(Ok("other"));
        }