예제 #1
0
        public ActionResult AddLike(PostViewModel post)
        {
            ApplicationUser ApplicationUser;
            string          id = User.Identity.GetUserId();

            ApplicationUser = UnitofWork.UserManager.Users
                              .Where(e => e.Id == id).FirstOrDefault();
            if (UnitofWork.LikesManager.GetLikers(post.Id).Any(l => l == ApplicationUser))
            {
                return(RemoveLike(ApplicationUser, post.Id));
            }
            Post post_    = UnitofWork.LikesManager.AddLikes(ApplicationUser, post.Id);
            int  likesNum = post_.numOfLikes;

            post_.numOfLikes = likesNum;
            LikesViewModel likesViewModel = new LikesViewModel
            {
                num    = likesNum,
                likers = UnitofWork.LikesManager.GetLikers(post.Id).ToList(),
                PostId = post.Id
            };

            //turn Content(likesNum.ToString());
            return(PartialView("_LikesPartial", likesViewModel));
        }
예제 #2
0
        protected override void HandleOnNavigatedTo(NavigationEventArgs e)
        {
            base.HandleOnNavigatedTo(e);
            if (this._isInitialized)
            {
                return;
            }
            long           ownerId    = long.Parse(((Page)this).NavigationContext.QueryString["OwnerId"]);
            long           itemId     = long.Parse(((Page)this).NavigationContext.QueryString["ItemId"]);
            int            num1       = int.Parse(((Page)this).NavigationContext.QueryString["Type"]);
            int            knownCount = int.Parse(((Page)this).NavigationContext.QueryString["knownCount"]);
            int            num2       = bool.Parse(((Page)this).NavigationContext.QueryString["SelectFriendLikes"]) ? 1 : 0;
            LikesViewModel vm         = new LikesViewModel(ownerId, itemId, (LikeObjectType)num1, knownCount);

            base.DataContext = vm;
            this.ucPullToRefresh.TrackListBox((ISupportPullToRefresh)this.listBoxAll);
            this.listBoxAll.OnRefresh = (Action)(() => vm.All.LoadData(true, false, null, false));
            this.ucPullToRefresh.TrackListBox((ISupportPullToRefresh)this.listBoxShared);
            this.listBoxShared.OnRefresh = (Action)(() => vm.Shared.LoadData(true, false, null, false));
            this.ucPullToRefresh.TrackListBox((ISupportPullToRefresh)this.listBoxFriends);
            this.listBoxFriends.OnRefresh = (Action)(() => vm.Friends.LoadData(true, false, null, false));
            vm.All.LoadData(false, false, null, false);
            if (num2 != 0)
            {
                this.pivot.SelectedItem = this.pivotItemFriends;
            }
            this._isInitialized = true;
        }
예제 #3
0
        public async Task <IHttpActionResult> Get(int changeRequestId)
        {
            LikesModel likesModel = await _likeStagingApplicationService.GetLikesByChangeRequestId(changeRequestId, CurrentUser.CustomerId);

            LikesViewModel likesViewModel = Mapper.Map <LikesViewModel>(likesModel);

            return(Ok(likesViewModel));
        }
예제 #4
0
        public void AddLike([FromBody] LikesViewModel vm)
        {
            var likes   = _context.Like;
            var validar = likes.Where(x => x.PostId == vm.PostId && x.UserId == vm.UserId).Count() > 0;

            if (validar == false)
            {
                var like = likes.Add(vm);
                _context.SaveChanges();
            }
        }
예제 #5
0
        public ActionResult Likes()
        {
            Manager        manager = new Manager(Properties.Settings.Default.ConStr);
            string         email   = User.Identity.Name;
            User           user    = manager.GetByEmail(email);
            LikesViewModel vm      = new LikesViewModel();

            vm.Images = manager.GetLikesForUser(user.Id);
            vm.User   = user;
            return(View(vm));
        }
예제 #6
0
        public ActionResult Create(LikesViewModel lvm, int idp)
        {
            Likes like = new Likes();

            like.IdParticipant = int.Parse(User.Identity.GetUserId());
            like.LikedDate     = DateTime.Now;

            like.IdPost = idp;
            LS.Add(like);
            LS.Commit();


            return(RedirectToAction("../Post/Details"));
        }
예제 #7
0
        public async Task <Likes> LikeAPost(LikesViewModel model)
        {
            var like = new Likes()
            {
                PostId    = model.ItemId,
                UserId    = model.LikedBy,
                DateLiked = DateTime.Now
            };
            await likesRepo.AddNewAsync(like);

            await likesRepo.SaveChangesAsync();

            return(like);
        }
예제 #8
0
        public ActionResult Index()
        {
            bool            liked = false;
            ApplicationUser ApplicationUser;
            string          id = User.Identity.GetUserId();

            ApplicationUser = UnitofWork.UserManager.Users
                              .Where(e => e.Id == id).FirstOrDefault();
            HomeViewModel homeViewModel = new HomeViewModel();

            homeViewModel.ApplicationUser = ApplicationUser;
            List <PostViewModel> postViewModels = new List <PostViewModel>();
            var            allPosts             = UnitofWork.PostsManager.GetAllByDate(id, 0).ToArray();
            LikesViewModel LikesViewModel;

            foreach (Post post in allPosts)
            {
                liked = false;
                var likes = UnitofWork.LikesManager.GetLikers(post.Id);
                LikesViewModel = new LikesViewModel {
                    likers = likes, num = post.numOfLikes, PostId = post.Id
                };
                if (likes.Any(l => l.Id == id) && likes != null && LikesViewModel != null)
                {
                    liked = true;
                }
                postViewModels.Add(new PostViewModel
                {
                    CurrentUser     = ApplicationUser,
                    ApplicationUser = post.ApplicationUser,
                    Comments        = post.Comments,
                    Date            = post.Date,
                    Id    = post.Id,
                    liked = liked,
                    Likes = post.Likes
                    ,
                    numOfComments = post.numOfComments,
                    numOfLikes    = post.numOfLikes,
                    numOfShares   = post.numOfShares
                    ,
                    postText       = post.postText,
                    Post_Shared    = post.Post_Shared,
                    LikesViewModel = LikesViewModel
                });
            }
            homeViewModel.PostViewModels = postViewModels.ToArray();

            return(View(homeViewModel));
        }
예제 #9
0
        public async Task <IActionResult> Post([FromBody] LikesViewModel crypto)
        {
            if (ModelState.IsValid)
            {
                var newLike = Mapper.Map <Likes>(crypto);
                newLike.UserName = User.Identity.Name;
                _repositiry.AddLikes(newLike);

                if (await _repositiry.SaveChangesAsync())
                {
                    return(Created($"api/Crypto/{crypto.Name}", Mapper.Map <LikesViewModel>(crypto)));
                }
            }
            return(BadRequest("Failed to save like"));
        }
예제 #10
0
        public ActionResult RemoveLike(ApplicationUser ApplicationUser, int postId)
        {
            Post post     = UnitofWork.LikesManager.deleteLikes(ApplicationUser, postId);
            int  likesNum = post.numOfLikes;

            post.numOfLikes = likesNum;
            LikesViewModel likesViewModel = new LikesViewModel
            {
                num    = likesNum,
                likers = UnitofWork.LikesManager.GetLikers(post.Id).ToList(),
                PostId = postId
            };

            return(PartialView("_LikesPartial", likesViewModel));
            //return PartialView("_LikesPartial", likesNum);
        }
예제 #11
0
        //Submit the post to the server
        public ActionResult AddPost(Post post)
        {
            ApplicationUser ApplicationUser;
            string          id = User.Identity.GetUserId();

            ApplicationUser = UnitofWork.UserManager.Users
                              .Where(e => e.Id == id).FirstOrDefault();
            post.ApplicationUser = ApplicationUser;
            post.Date            = DateTime.Now;
            UnitofWork.PostsManager.Add(post);
            bool liked = false;
            List <PostViewModel> postViewModels = new List <PostViewModel>();
            var            allPosts             = UnitofWork.PostsManager.GetAllByDate(id, 0).ToArray();
            LikesViewModel LikesViewModel;

            foreach (Post post_ in allPosts)
            {
                LikesViewModel = new LikesViewModel {
                    likers = UnitofWork.LikesManager.GetLikers(post_.Id)
                    , num  = post_.numOfLikes, PostId = post.Id
                };
                if (UnitofWork.LikesManager.GetLikers(post_.Id).Any(l => l.Id == id))
                {
                    liked = true;
                }
                postViewModels.Add(new PostViewModel
                {
                    CurrentUser     = ApplicationUser,
                    ApplicationUser = post_.ApplicationUser,
                    Comments        = post_.Comments,
                    Date            = post_.Date,
                    Id    = post_.Id,
                    liked = liked,
                    Likes = post_.Likes
                    ,
                    numOfComments = post_.numOfComments,
                    numOfLikes    = post_.numOfLikes,
                    numOfShares   = post_.numOfShares
                    ,
                    postText       = post_.postText,
                    Post_Shared    = post_.Post_Shared,
                    LikesViewModel = LikesViewModel
                });
            }
            return(PartialView("_PostsPartial", postViewModels.ToArray()));
        }
예제 #12
0
        protected virtual PartialViewResult Likes(IEnumerable <LikeModel> likes, Guid entityId, bool isReadOnly = false, bool showTitle = false)
        {
            var currenMemberId = _intranetMemberService.GetCurrentMemberId();
            var likeModels     = likes as IList <LikeModel> ?? likes.ToList();
            var canAddLike     = likeModels.All(el => el.UserId != currenMemberId);
            var model          = new LikesViewModel
            {
                EntityId   = entityId,
                MemberId   = currenMemberId,
                Count      = likeModels.Count,
                CanAddLike = canAddLike,
                Users      = likeModels.Select(el => el.User),
                IsReadOnly = isReadOnly,
                ShowTitle  = showTitle
            };

            return(PartialView(LikesViewPath, model));
        }
예제 #13
0
        public IActionResult Likes()
        {
            LikesViewModel lvm = new LikesViewModel();

            List <Car>           Cars             = new List <Car>();
            List <Configuration> Configurations   = new List <Configuration>();
            List <string>        configurationIds = new List <string>(db.Users.Where(x => x.Email == User.Identity.Name).FirstOrDefault().LikedString.Split(','));

            foreach (var id in configurationIds)
            {
                Configuration conf = db.Configurations.Where(x => x.Id == Convert.ToInt32(id)).FirstOrDefault();
                Car           car  = db.Cars.Where(x => x.Id == conf.CarId).FirstOrDefault();
                Cars.Add(car);
                Configurations.Add(conf);
            }

            lvm.Configurations = Configurations;
            lvm.Cars           = Cars;

            return(View(lvm));
        }
예제 #14
0
        public ActionResult Like(int id, LikesViewModel lvm)
        {
            int u = int.Parse(User.Identity.GetUserId());

            if (LS.Test(id, u).Equals(true))
            {
                return(RedirectToAction("Details", "Post", new { id }));
            }
            else
            {
                List <LikesViewModel> list = new List <LikesViewModel>();


                Likes l = new Likes();
                l.IdParticipant = int.Parse(User.Identity.GetUserId());
                l.IdPost        = id;
                l.LikedDate     = DateTime.Now;
                LS.Add(l);
                LS.Commit();
                return(RedirectToAction("Details", "Post", new { id }));
            }
        }
예제 #15
0
        public ActionResult GetSortedPosts(int?x, int count)
        {
            bool            liked = false;
            ApplicationUser ApplicationUser;
            string          id = User.Identity.GetUserId();

            ApplicationUser = UnitofWork.UserManager.Users
                              .Where(e => e.Id == id).FirstOrDefault();
            Post[] LatestPosts;
            Post[] topPosts;
            List <PostViewModel> postViewModels = new List <PostViewModel>();

            if (x == null || x == 0)
            {
                LatestPosts = GetLatest(id, count);
                LikesViewModel LikesViewModel;

                foreach (Post post in LatestPosts)
                {
                    liked = false;
                    var likes = UnitofWork.LikesManager.GetLikers(post.Id);
                    LikesViewModel = new LikesViewModel {
                        likers   = likes, num = post.numOfLikes
                        , PostId = post.Id
                    };
                    if (LikesViewModel != null && likes != null && likes.Any(l => l.Id == id))
                    {
                        liked = true;
                    }
                    postViewModels.Add(new PostViewModel
                    {
                        CurrentUser     = ApplicationUser,
                        ApplicationUser = post.ApplicationUser,
                        Comments        = post.Comments,
                        Date            = post.Date,
                        Id    = post.Id,
                        liked = liked,
                        Likes = post.Likes
                        ,
                        numOfComments = post.numOfComments,
                        numOfLikes    = post.numOfLikes,
                        numOfShares   = post.numOfShares
                        ,
                        postText       = post.postText,
                        Post_Shared    = post.Post_Shared,
                        LikesViewModel = LikesViewModel
                    });
                }
            }
            else
            {
                topPosts = GetTop(id, count);
                LikesViewModel LikesViewModel;

                foreach (Post post in topPosts)
                {
                    liked = false;
                    var likes = UnitofWork.LikesManager.GetLikers(post.Id);
                    LikesViewModel = new LikesViewModel {
                        likers = likes, num = post.numOfLikes, PostId = post.Id
                    };
                    if (LikesViewModel != null && likes != null && likes.Any(l => l.Id == id))
                    {
                        liked = true;
                    }
                    postViewModels.Add(new PostViewModel
                    {
                        CurrentUser     = ApplicationUser,
                        ApplicationUser = post.ApplicationUser,
                        Comments        = post.Comments,
                        Date            = post.Date,
                        Id    = post.Id,
                        liked = liked,
                        Likes = post.Likes
                        ,
                        numOfComments = post.numOfComments,
                        numOfLikes    = post.numOfLikes,
                        numOfShares   = post.numOfShares
                        ,
                        postText       = post.postText,
                        Post_Shared    = post.Post_Shared,
                        LikesViewModel = LikesViewModel
                    });
                }
            }

            return(PartialView("_PostsPartial", postViewModels.ToArray()));
        }
예제 #16
0
 public IActionResult LikePost(LikesViewModel model)
 {
     model.LikedBy = Guid.Parse(GetUser().Result.Id);
     likesService.LikeAPost(model);
     return(Ok());
 }
예제 #17
0
        public IActionResult Recommendations()
        {
            List <Car>           RecommendCars    = new List <Car>();
            List <Configuration> RecommendConfigs = new List <Configuration>();

            string[] arr = db.Users.Where(x => x.Email == User.Identity.Name).FirstOrDefault().LikedString.Split(',');

            List <Configuration> LikedConfigs = new List <Configuration>();

            foreach (var item in arr)
            {
                LikedConfigs.Add(db.Configurations.Where(x => x.Id == Convert.ToInt32(item)).FirstOrDefault());
            }

            Dictionary <int, int> details = new Dictionary <int, int>();

            foreach (var conf in LikedConfigs)
            {
                string[] dets = conf.DetailsString.Split(',');
                foreach (var det in dets)
                {
                    Detail detail = db.Details.Where(x => x.Id == Convert.ToInt32(det)).FirstOrDefault();

                    if (detail != null)
                    {
                        if (details.ContainsKey(detail.Id))
                        {
                            details[detail.Id]++;
                        }
                        else
                        {
                            details[detail.Id] = 1;
                        }
                    }
                }
            }

            List <int> colors = new List <int>();
            List <int> wheels = new List <int>();

            foreach (KeyValuePair <int, int> keyValue in details)
            {
                if (db.Details.Where(x => x.Id == keyValue.Key).FirstOrDefault().Category == "Color")
                {
                    for (int i = 0; i < keyValue.Value; i++)
                    {
                        colors.Add(keyValue.Key);
                    }
                }
                else if (db.Details.Where(x => x.Id == keyValue.Key).FirstOrDefault().Category == "Wheels")
                {
                    for (int i = 0; i < keyValue.Value; i++)
                    {
                        wheels.Add(keyValue.Key);
                    }
                }
            }

            Random rand    = new Random();
            int    colorId = colors[rand.Next(0, colors.Count)];
            int    wheelId = wheels[rand.Next(0, wheels.Count)];
            List <Configuration> configs = db.Configurations.ToList();

            foreach (var conf in configs)
            {
                List <string> det = new List <string>(conf.DetailsString.Split(','));
                if (det.Contains(colorId.ToString()) || det.Contains(wheelId.ToString()))
                {
                    RecommendCars.Add(db.Cars.Where(x => x.Id == conf.CarId).FirstOrDefault());
                    RecommendConfigs.Add(conf);
                }
            }

            LikesViewModel lvm = new LikesViewModel
            {
                Cars           = RecommendCars,
                Configurations = RecommendConfigs
            };

            while (lvm.Cars.Count() > 4)
            {
                int a = rand.Next(0, lvm.Cars.Count() - 1);
                lvm.Cars.RemoveAt(a);
                lvm.Configurations.RemoveAt(a);
            }

            return(View(lvm));
        }
        public IActionResult Profile(string id)
        {
            //var userid = id;
            //if (userid == null)
            //    userid = _userManager.GetUserId(User);
            //var user = context.Users.Find(userid);
            //if (userid == null)
            //    return RedirectToAction("Home");
            //return View(user);

            //--------------------------------------

            //Profile info
            if (id == null)
            {
                id = _userManager.GetUserId(User);
            }
            var userFromDb = context.Users.Include(u => u.Posts).SingleOrDefault(u => u.Id == id);
            var userVM     = new UserViewModel()
            {
                Nickname  = userFromDb.Nickname,
                Bio       = userFromDb.Bio,
                BirthDate = userFromDb.BirthDate,
                Gender    = userFromDb.Gender,
                Image     = userFromDb.Image,
                Email     = userFromDb.Email,
                UserId    = userFromDb.Id,
            };

            IEnumerable <Post> postsList = context.Posts.Where(p => p.IsDeleted == false);
            var postVMLst = new List <PostsViewModel>();

            foreach (var post in postsList)
            {
                //var currentUserId = _userManager.GetUserId(User);
                if (post.UserId == id)
                {
                    var postVM = new PostsViewModel();
                    postVM.Content     = post.Content;
                    postVM.IsDeleted   = post.IsDeleted;
                    postVM.PostId      = post.Id;
                    postVM.UserId      = post.UserId;
                    postVM.PostingDate = post.PostingDate;

                    IEnumerable <UserLikesPost>      likesList    = context.UserLikesPosts.Where(l => l.PostId == post.Id && l.IsLiked == true);
                    IEnumerable <UserCommentsOnPost> commentsList = context.UserCommentsOnPosts.Where(c => c.PostId == post.Id && c.IsDeleted == false);

                    var likesVMLst    = new List <LikesViewModel>();
                    var commentsVMLst = new List <CommentsViewModel>();

                    foreach (var like in likesList)
                    {
                        var likeVM = new LikesViewModel();
                        likeVM.UserId  = like.UserId;
                        likeVM.PostId  = like.PostId;
                        likeVM.IsLiked = like.IsLiked;
                        likesVMLst.Add(likeVM);
                    }
                    postVM.likesVMList = likesVMLst;

                    foreach (var comment in commentsList)
                    {
                        var commentVM = new CommentsViewModel();
                        commentVM.Content   = comment.Content;
                        commentVM.UserId    = comment.UserId;
                        commentVM.IsDeleted = comment.IsDeleted;
                        commentVM.PostId    = comment.PostId;
                        commentVM.CommentId = comment.CommentId;
                        commentsVMLst.Add(commentVM);
                    }
                    postVM.commentsVMList = commentsVMLst;


                    postVMLst.Add(postVM);
                }
            }

            userVM.Posts = postVMLst;

            //if (userFromDb.Posts != null)
            //    foreach (var item in userFromDb.Posts)
            //    {
            //        userVM.Posts.Add(new PostsViewModel()
            //        {
            //            Content = item.Content,
            //            IsDeleted = item.IsDeleted,
            //            PostingDate = item.PostingDate,
            //            UserId = id,
            //            PostId = item.Id
            //        });
            //    }
            //Get friends of the user
            var friendships = context.UserHasFriends.Where(f => f.UserId == id || f.FriendId == id).ToList();

            if (friendships.Count > 0)
            {
                foreach (var friendship in friendships)
                {
                    if (friendship.Status != FriendRequestStatus.NotFriend)
                    {
                        var trueFriendId     = "";
                        var friendshipStatus = friendship.Status;

                        if (friendship.FriendId == id)
                        {
                            trueFriendId = friendship.UserId;
                            if (friendship.Status == FriendRequestStatus.SentAndPending)
                            {
                                friendshipStatus = FriendRequestStatus.ReceivedAndPending;
                            }
                            else if (friendship.Status == FriendRequestStatus.ReceivedAndPending)
                            {
                                friendshipStatus = FriendRequestStatus.SentAndPending;
                            }
                        }
                        else if (friendship.UserId == id)
                        {
                            trueFriendId = friendship.FriendId;
                        }
                        //var friendFromDb = context.Users.Find(friendship.FriendId ?? friendship.UserId);
                        var friendFromDb = context.Users.Find(trueFriendId);

                        userVM.Friends.Add(new FriendsViewModel()
                        {
                            FriendId = friendFromDb.Id,
                            Nickname = friendFromDb.Nickname,
                            Email    = friendFromDb.Email,
                            Bio      = friendFromDb.Bio,
                            Gender   = friendFromDb.Gender,
                            Image    = friendFromDb.Image,
                            Status   = friendshipStatus
                        });
                    }
                }
            }
            //else
            //    userVM.Status = FriendRequestStatus.NotFriend;
            return(View(userVM));
        }
        public IActionResult Index(int flag)
        {
            ViewBag.flag = flag;
            //return View(context.Posts.OrderByDescending(p => p.PostingDate).ToList());

            var id = _userManager.GetUserId(User);

            var userFromDb = context.Users.Include(u => u.Posts).SingleOrDefault(u => u.Id == id);
            var userVM     = new UserViewModel()
            {
                Nickname  = userFromDb.Nickname,
                Bio       = userFromDb.Bio,
                BirthDate = userFromDb.BirthDate,
                Gender    = userFromDb.Gender,
                Image     = userFromDb.Image,
                Email     = userFromDb.Email,
                UserId    = userFromDb.Id,
            };

            IEnumerable <Post> postsList = context.Posts.Where(p => p.IsDeleted == false);
            var postVMLst = new List <PostsViewModel>();

            foreach (var post in postsList)
            {
                var currentUserId = _userManager.GetUserId(User);
                var isAFriend     = context.UserHasFriends.FirstOrDefault(u => (u.UserId == post.UserId || u.FriendId == post.UserId) && (u.UserId == currentUserId || u.FriendId == currentUserId) && u.Status == FriendRequestStatus.Friend);
                if (isAFriend != null || post.UserId == currentUserId)
                {
                    var postVM = new PostsViewModel();
                    postVM.Content     = post.Content;
                    postVM.IsDeleted   = post.IsDeleted;
                    postVM.PostId      = post.Id;
                    postVM.UserId      = post.UserId;
                    postVM.PostingDate = post.PostingDate;

                    IEnumerable <UserLikesPost>      likesList    = context.UserLikesPosts.Where(l => l.PostId == post.Id && l.IsLiked == true);
                    IEnumerable <UserCommentsOnPost> commentsList = context.UserCommentsOnPosts.Where(c => c.PostId == post.Id && c.IsDeleted == false);

                    var likesVMLst    = new List <LikesViewModel>();
                    var commentsVMLst = new List <CommentsViewModel>();

                    foreach (var like in likesList)
                    {
                        var likeVM = new LikesViewModel();
                        likeVM.UserId  = like.UserId;
                        likeVM.PostId  = like.PostId;
                        likeVM.IsLiked = like.IsLiked;
                        likesVMLst.Add(likeVM);
                    }
                    postVM.likesVMList = likesVMLst;

                    foreach (var comment in commentsList)
                    {
                        var commentVM = new CommentsViewModel();
                        commentVM.Content   = comment.Content;
                        commentVM.UserId    = comment.UserId;
                        commentVM.IsDeleted = comment.IsDeleted;
                        commentVM.PostId    = comment.PostId;
                        commentVM.CommentId = comment.CommentId;
                        commentsVMLst.Add(commentVM);
                    }
                    postVM.commentsVMList = commentsVMLst;


                    postVMLst.Add(postVM);
                }
            }

            userVM.Posts = postVMLst;

            //postVMLst.OrderByDescending(p => p.PostingDate)
            return(View(userVM));
        }
        public IActionResult ProfileDetails(string id)
        {
            #region Profile info
            if (id == null)
            {
                id = _userManager.GetUserId(User);
            }
            var userFromDb = _context.Users.Include(u => u.Posts).SingleOrDefault(u => u.Id == id);
            var userVM     = new UserViewModel()
            {
                Nickname    = userFromDb.Nickname,
                Bio         = userFromDb.Bio,
                BirthDate   = userFromDb.BirthDate,
                Gender      = userFromDb.Gender,
                Image       = userFromDb.Image,
                Email       = userFromDb.Email,
                UserId      = userFromDb.Id,
                isBlocked   = userFromDb.isBlocked,
                PhoneNumber = userFromDb.PhoneNumber
            };
            #endregion

            IEnumerable <Post> postsList = _context.Posts.Where(p => p.IsDeleted == false);
            var postVMLst = new List <PostsViewModel>();
            #region Getting all the posts with their likes and comments
            if (postsList != null)
            {
                foreach (var post in postsList)
                {
                    //var currentUserId = _userManager.GetUserId(User);
                    if (post.UserId == id)
                    {
                        //Posts
                        var postVM = new PostsViewModel()
                        {
                            Content     = post.Content,
                            IsDeleted   = post.IsDeleted,
                            PostId      = post.Id,
                            UserId      = post.UserId,
                            PostingDate = post.PostingDate,
                            Image       = post.Image
                        };
                        IEnumerable <UserLikesPost>      likesList    = _context.UserLikesPosts.Where(l => l.PostId == post.Id && l.IsLiked == true);
                        IEnumerable <UserCommentsOnPost> commentsList = _context.UserCommentsOnPosts.Where(c => c.PostId == post.Id && c.IsDeleted == false);
                        var likesVMLst    = new List <LikesViewModel>();
                        var commentsVMLst = new List <CommentsViewModel>();
                        //Likes
                        foreach (var like in likesList)
                        {
                            var likeVM = new LikesViewModel();
                            likeVM.UserId  = like.UserId;
                            likeVM.PostId  = like.PostId;
                            likeVM.IsLiked = like.IsLiked;
                            likesVMLst.Add(likeVM);
                        }
                        postVM.likesVMList = likesVMLst;
                        //Comments
                        foreach (var comment in commentsList)
                        {
                            var commentVM = new CommentsViewModel();
                            commentVM.Content   = comment.Content;
                            commentVM.UserId    = comment.UserId;
                            commentVM.IsDeleted = comment.IsDeleted;
                            commentVM.PostId    = comment.PostId;
                            commentVM.CommentId = comment.CommentId;
                            commentsVMLst.Add(commentVM);
                        }
                        postVM.commentsVMList = commentsVMLst;
                        postVMLst.Add(postVM);
                    }
                }
            }
            userVM.Posts = postVMLst.OrderByDescending(p => p.PostingDate).ToList();
            #endregion

            #region Getting friends of the current user
            if (id == _userManager.GetUserId(User)) //Friends in the current user profile
            {
                var friendships = _context.UserHasFriends.Where(f => f.UserId == _userManager.GetUserId(User) || f.FriendId == _userManager.GetUserId(User) && f.Status != FriendRequestStatus.NotFriend).ToList();
                if (friendships.Count > 0)
                {
                    foreach (var friendship in friendships)
                    {
                        var trueFriendId     = string.Empty; //reciever user id
                        var friendshipStatus = friendship.Status;

                        if (friendship.FriendId == _userManager.GetUserId(User))
                        {
                            trueFriendId = friendship.UserId;
                        }
                        else if (friendship.UserId == _userManager.GetUserId(User))
                        {
                            trueFriendId = friendship.FriendId;
                        }

                        var friendFromDb = _context.Users.Find(trueFriendId); // Friend that I have sent to him or recieved from him (Not current user)
                        if (friendFromDb != null)
                        {
                            userVM.Friends.Add(new FriendsViewModel()
                            {
                                FriendId   = friendFromDb.Id,
                                Nickname   = friendFromDb.Nickname,
                                Email      = friendFromDb.Email,
                                Bio        = friendFromDb.Bio,
                                Gender     = friendFromDb.Gender,
                                Image      = friendFromDb.Image,
                                Status     = friendshipStatus,
                                SenderId   = friendship.UserId,
                                RecieverId = friendship.FriendId
                            });
                        }

                        userVM.SenderId   = friendship.UserId;
                        userVM.RecieverId = friendship.FriendId;
                    }
                }
            }
            else //friendship between the current user and the profile owner
            {
                var friendship = _context.UserHasFriends.FirstOrDefault(f => f.UserId == id || f.FriendId == id && f.Status != FriendRequestStatus.NotFriend);
                if (friendship != null)                  //there is a friendship between current user and the profile owner
                {
                    var trueFriendId     = string.Empty; //reciever user id
                    var friendshipStatus = friendship.Status;

                    if (friendship.FriendId == _userManager.GetUserId(User))
                    {
                        trueFriendId = friendship.UserId;
                    }
                    else if (friendship.UserId == _userManager.GetUserId(User))
                    {
                        trueFriendId = friendship.FriendId;
                    }

                    var friendFromDb = _context.Users.Find(trueFriendId); // Friend that I have sent to him or recieved from him (Not current user)
                    if (friendFromDb != null)
                    {
                        userVM.Friends.Add(new FriendsViewModel()
                        {
                            FriendId   = friendFromDb.Id,
                            Nickname   = friendFromDb.Nickname,
                            Email      = friendFromDb.Email,
                            Bio        = friendFromDb.Bio,
                            Gender     = friendFromDb.Gender,
                            Image      = friendFromDb.Image,
                            Status     = friendshipStatus,
                            SenderId   = friendship.UserId,
                            RecieverId = friendship.FriendId
                        });
                    }

                    userVM.SenderId   = friendship.UserId;
                    userVM.RecieverId = friendship.FriendId;
                }
            }
            #endregion

            return(PartialView("Details", userVM));
        }
        public IActionResult HomeDetails()
        {
            #region before
            //int FriendsCount = 0;

            //var id = _userManager.GetUserId(User);

            //var userFromDb = _context.Users.Include(u => u.Posts).SingleOrDefault(u => u.Id == id);
            //var userVM = new UserViewModel()
            //{
            //    Nickname = userFromDb.Nickname,
            //    Bio = userFromDb.Bio,
            //    BirthDate = userFromDb.BirthDate,
            //    Gender = userFromDb.Gender,
            //    Image = userFromDb.Image,
            //    Email = userFromDb.Email,
            //    UserId = userFromDb.Id,
            //};

            //IEnumerable<Post> postsList = _context.Posts.Where(p => p.IsDeleted == false);
            //var postVMLst = new List<PostsViewModel>();
            //foreach (var post in postsList)
            //{
            //    var currentUserId = _userManager.GetUserId(User);
            //    var isAFriend = _context.UserHasFriends.FirstOrDefault(u => (u.UserId == post.UserId || u.FriendId == post.UserId) && (u.UserId == currentUserId || u.FriendId == currentUserId) && u.Status == FriendRequestStatus.Friend);
            //    if (isAFriend != null || post.UserId == currentUserId)
            //    {
            //        var postVM = new PostsViewModel();
            //        postVM.Content = post.Content;
            //        postVM.IsDeleted = post.IsDeleted;
            //        postVM.PostId = post.Id;
            //        postVM.UserId = post.UserId;
            //        postVM.PostingDate = post.PostingDate;

            //        IEnumerable<UserLikesPost> likesList = _context.UserLikesPosts.Where(l => l.PostId == post.Id && l.IsLiked == true);
            //        IEnumerable<UserCommentsOnPost> commentsList = _context.UserCommentsOnPosts.Where(c => c.PostId == post.Id && c.IsDeleted == false);

            //        var likesVMLst = new List<LikesViewModel>();
            //        var commentsVMLst = new List<CommentsViewModel>();

            //        foreach (var like in likesList)
            //        {
            //            var likeVM = new LikesViewModel();
            //            likeVM.UserId = like.UserId;
            //            likeVM.PostId = like.PostId;
            //            likeVM.IsLiked = like.IsLiked;
            //            likesVMLst.Add(likeVM);
            //        }
            //        postVM.likesVMList = likesVMLst;

            //        foreach (var comment in commentsList)
            //        {
            //            var commentVM = new CommentsViewModel();
            //            commentVM.Content = comment.Content;
            //            commentVM.UserId = comment.UserId;
            //            commentVM.IsDeleted = comment.IsDeleted;
            //            commentVM.PostId = comment.PostId;
            //            commentVM.CommentId = comment.CommentId;
            //            commentsVMLst.Add(commentVM);
            //        }
            //        postVM.commentsVMList = commentsVMLst;


            //        postVMLst.Add(postVM);
            //    }
            //}

            //userVM.Posts = postVMLst;

            ////Friends
            //FriendsCount = _context.UserHasFriends.Where(u => (u.UserId == _userManager.GetUserId(User) || u.FriendId == _userManager.GetUserId(User)) && u.Status == FriendRequestStatus.Friend).Count();
            //ViewBag.FriendsCount = FriendsCount;
            #endregion

            int FriendsCount = 0;

            var id = _userManager.GetUserId(User);

            var userFromDb = _context.Users.Include(u => u.Posts).SingleOrDefault(u => u.Id == id);
            var userVM     = new UserViewModel()
            {
                Nickname  = userFromDb.Nickname,
                Bio       = userFromDb.Bio,
                BirthDate = userFromDb.BirthDate,
                Gender    = userFromDb.Gender,
                Image     = userFromDb.Image,
                Email     = userFromDb.Email,
                UserId    = userFromDb.Id,
            };

            IEnumerable <Post> postsList = _context.Posts.Where(p => p.IsDeleted == false);
            var postVMLst = new List <PostsViewModel>();
            foreach (var post in postsList)
            {
                var currentUserId = _userManager.GetUserId(User);
                var isAFriend     = _context.UserHasFriends.FirstOrDefault(u => (u.UserId == post.UserId || u.FriendId == post.UserId) && (u.UserId == currentUserId || u.FriendId == currentUserId) && u.Status == FriendRequestStatus.Friend);
                if (isAFriend != null || post.UserId == currentUserId)
                {
                    var postVM = new PostsViewModel();
                    postVM.Content     = post.Content;
                    postVM.IsDeleted   = post.IsDeleted;
                    postVM.PostId      = post.Id;
                    postVM.UserId      = post.UserId;
                    postVM.PostingDate = post.PostingDate;
                    postVM.Image       = post.Image;

                    IEnumerable <UserLikesPost>      likesList    = _context.UserLikesPosts.Where(l => l.PostId == post.Id && l.IsLiked == true);
                    IEnumerable <UserCommentsOnPost> commentsList = _context.UserCommentsOnPosts.Where(c => c.PostId == post.Id && c.IsDeleted == false);

                    var likesVMLst    = new List <LikesViewModel>();
                    var commentsVMLst = new List <CommentsViewModel>();

                    foreach (var like in likesList)
                    {
                        var likeVM = new LikesViewModel();
                        likeVM.UserId  = like.UserId;
                        likeVM.PostId  = like.PostId;
                        likeVM.IsLiked = like.IsLiked;
                        likesVMLst.Add(likeVM);
                    }
                    postVM.likesVMList = likesVMLst;

                    foreach (var comment in commentsList)
                    {
                        var commentVM = new CommentsViewModel();
                        commentVM.Content   = comment.Content;
                        commentVM.UserId    = comment.UserId;
                        commentVM.IsDeleted = comment.IsDeleted;
                        commentVM.PostId    = comment.PostId;
                        commentVM.CommentId = comment.CommentId;
                        commentsVMLst.Add(commentVM);
                    }
                    postVM.commentsVMList = commentsVMLst;


                    postVMLst.Add(postVM);
                }
            }

            userVM.Posts = postVMLst;

            //Friends
            FriendsCount = _context.UserHasFriends.Where(u => (u.UserId == _userManager.GetUserId(User) || u.FriendId == _userManager.GetUserId(User)) && u.Status == FriendRequestStatus.Friend).Count();
            //foreach (var friend in friendsList)
            //{
            //    var pendingFriends = context.UserHasFriends.FirstOrDefault(f => (f.UserId == _userManager.GetUserId(User) || f.FriendId == _userManager.GetUserId(User)) && f.Status == FriendRequestStatus.Pending);
            //    var friendId = (pendingFriends.FriendId != _userManager.GetUserId(User)) ? pendingFriends.FriendId : pendingFriends.UserId;
            //    friendsVMList.Add(new FriendsViewModel()
            //    {
            //        Email = context.Users.FirstOrDefault(f => f.Id == friendId).Email,
            //        //TODO: Image
            //        Image = context.Users.FirstOrDefault(f => f.Id == friendId).Image,
            //    });
            //}
            ViewBag.FriendsCount = FriendsCount;


            return(PartialView("Details", userVM));
        }