コード例 #1
0
        public ActionResult Accept(string id)
        {
            var friendsContext  = new FriendsDbContext();
            var requestsContext = new FriendRequestDbContext();
            var userId          = User.Identity.GetUserId();
            var requestToRemove = requestsContext.FriendRequests.FirstOrDefault(u => u.FriendId == userId && u.UserId == id);

            // Check that the users aren't already friends
            var friends = friendsContext.Friends.Where(u => u.User.UserId == userId && u.Friend.UserId == id);

            if (friends.Count() == 0)
            {
                friendsContext.Friends.Add(new FriendsModel {
                    UserId   = userId,
                    FriendId = id,
                    Category = "Friend"
                });
                // You're my friend == I'm your friend (allows users to pick different categories for each other)
                friendsContext.Friends.Add(new FriendsModel {
                    UserId   = id,
                    FriendId = userId,
                    Category = "Friend"
                });

                friendsContext.SaveChanges();
            }
            requestsContext.FriendRequests.Remove(requestToRemove);
            requestsContext.SaveChanges();

            return(RedirectToAction("Index"));
        }
コード例 #2
0
        //Ta vort från vänförfrågning
        private void RemoveFromRequests(string userId, string friendUserId)
        {
            var ctx         = new FriendRequestDbContext();
            var reqToRemove = ctx.Requests.
                              SingleOrDefault(r => r.RecieverId == userId && r.SenderId == friendUserId);

            if (reqToRemove != null)
            {
                ctx.Requests.Remove(reqToRemove);
            }
            ctx.SaveChanges();
        }
コード例 #3
0
        //Avböj vänförfrågan
        public ActionResult DenyFriendRequest(string userId)
        {
            var currentUserId = User.Identity.GetUserId();
            var ctx           = new FriendRequestDbContext();
            var request       = ctx.Requests.FirstOrDefault(r => r.RecieverId == currentUserId && r.SenderId == userId);

            if (request != null)
            {
                ctx.Requests.Remove(request);
                ctx.SaveChanges();
            }

            return(Redirect(Request.UrlReferrer.ToString()));
        }
コード例 #4
0
        public ActionResult Ignore(string id)
        {
            var requestsContext = new FriendRequestDbContext();
            var userId          = User.Identity.GetUserId();
            var requestToRemove = requestsContext.FriendRequests.FirstOrDefault(u => u.UserId == id && u.FriendId == userId);

            if (requestToRemove != null)
            {
                requestsContext.FriendRequests.Remove(requestToRemove);
                requestsContext.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
コード例 #5
0
        public ActionResult SendFriendRequest(string userId)
        {
            if (userId != User.Identity.GetUserId())
            {
                var ctx = new FriendRequestDbContext();
                ctx.Requests.Add(new FriendRequest()
                {
                    SenderId   = User.Identity.GetUserId(),
                    RecieverId = userId
                });
                ctx.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }
コード例 #6
0
        // TODO: Move this action to ProfileController when it's done
        public ActionResult Add(string id)
        {
            var userContext     = new UserDbContext();
            var friendsContext  = new FriendsDbContext();
            var requestsContext = new FriendRequestDbContext();
            var userId          = User.Identity.GetUserId();

            // Check that the sender hasn't already sent a request
            var sentRequest = requestsContext.FriendRequests.Where(u => u.UserId == userId && u.FriendId == id);

            if (sentRequest.Count() == 0)
            {
                // Check that the recipient hasn't already sent a request (redirect to requests page if they have)
                var receivedRequest = requestsContext.FriendRequests.Where(u => u.UserId == id && u.FriendId == userId);
                if (receivedRequest.Count() == 0)
                {
                    // Check that the users aren't already friends && that the user isn't trying to add themself
                    var friends = friendsContext.Friends.Where(u => u.User.UserId == userId && u.Friend.UserId == id);
                    if (friends.Count() == 0 && userId != id)
                    {
                        requestsContext.FriendRequests.Add(new FriendRequestModel {
                            UserId   = userId,
                            FriendId = id,
                            Seen     = false
                        });
                        requestsContext.SaveChanges();
                    }
                }
                else
                {
                    return(RedirectToAction("Index", "Friends"));
                }
            }
            // TODO: Change to appropriate action once the method has been moved to ProfileController
            return(RedirectToAction("All", "User"));
        }