예제 #1
0
        // Like/Dislike
        public async Task <IActionResult> Create(int PostId)
        {
            var UserId = _userManager.GetUserId(User);

            //Like likeExists = await _context.Likes.FindAsync(UserId, PostId);
            Like likeExists = _context.Likes.Where(l => l.UserId == UserId && l.PostId == PostId).FirstOrDefault();

            if (ModelState.IsValid)
            {
                if (likeExists == null)
                {
                    Like like = new Like {
                        UserId = UserId, PostId = PostId
                    };
                    _context.Add(like);
                }
                else
                {
                    _context.Remove(likeExists);
                }

                await _context.SaveChangesAsync();
            }

            return(RedirectToAction("Index", "Posts"));
        }
예제 #2
0
        public async Task <IActionResult> SendFriendRequest(string UserId, bool accept = true, bool fr = false)
        {
            var CurrentUserId = _userManager.GetUserId(User);

            Friend requestSent = await _context.Friends.FindAsync(CurrentUserId, UserId);

            Friend requestRecieved = await _context.Friends.FindAsync(UserId, CurrentUserId);

            if (ModelState.IsValid)
            {
                if (requestSent == null && requestRecieved == null)
                {
                    Friend friend = new Friend {
                        RequestedById = CurrentUserId, RequestedToId = UserId, RequestTime = DateTime.Now, FriendRequestFlag = FriendRequestFlag.Pending
                    };
                    _context.Add(friend);
                }
                else if (requestSent != null)
                {
                    _context.Remove(requestSent);
                }
                else if (requestRecieved != null && accept)
                {
                    requestRecieved.FriendSince       = DateTime.Now;
                    requestRecieved.FriendRequestFlag = FriendRequestFlag.Approved;
                    _context.Update(requestRecieved);
                }
                else if (requestRecieved != null && !accept)
                {
                    _context.Remove(requestRecieved);
                }
                else
                {
                    //mislim da su to sve opcije
                }
                await _context.SaveChangesAsync();
            }
            if (fr)
            {
                return(RedirectToAction("FriendRequests", "AppUsers"));
            }
            return(RedirectToAction("Details", "AppUsers", new { id = UserId }));
        }