public IActionResult AddLike([FromQuery] int postId)
        {
            if (postId == 0)
            {
                return(Json(new { statusCode = ResponseStatus.ValidationError }));
            }
            User user = userData.GetUser(HttpContext);
            Like alreadyExsistLike = facebookDataContext.Likes.Include("User.ProfilePhotos").FirstOrDefault(x => x.UserId == user.Id && x.PostId == postId);

            if (alreadyExsistLike != null)
            {
                alreadyExsistLike.IsDeleted = !alreadyExsistLike.IsDeleted;
                try { facebookDataContext.SaveChanges(); }
                catch { return(Json(new { statusCode = ResponseStatus.Error })); }
                return(Json(new { statusCode = ResponseStatus.Success, responseMessage = HomePageDtoMapper.Map(alreadyExsistLike, hostingEnvironment), IsLike = alreadyExsistLike.IsDeleted, LikeId = alreadyExsistLike.Id }));
            }
            Like like = new Like()
            {
                PostId = postId, CreatedAt = DateTime.Now, IsDeleted = false, ReactionStatusId = (int)ReactionStatuses.Like, UserId = user.Id
            };

            facebookDataContext.Likes.Add(like);
            try { facebookDataContext.SaveChanges(); }
            catch { return(Json(new { statusCode = ResponseStatus.Error })); }
            like = facebookDataContext.Likes.Where(x => x.Id == like.Id).Include("User.ProfilePhotos").FirstOrDefault();
            return(Json(new { statusCode = ResponseStatus.Success, responseMessage = HomePageDtoMapper.Map(like, hostingEnvironment), IsLike = like.IsDeleted, LikeId = like.Id }));
        }
        public IActionResult AddComment([FromBody] CommentPostDto commentPostDto)
        {
            if (commentPostDto == null || commentPostDto.CommentContent == null)
            {
                return(Json(new { statusCode = ResponseStatus.ValidationError, responseMessage = ValidationMessages.EmptyComment }));
            }
            User    user    = userData.GetUser(HttpContext);
            Comment comment = CommentDtoMapper.Map(commentPostDto, user.Id);

            facebookDataContext.Comments.Add(comment);
            try { facebookDataContext.SaveChanges(); }
            catch { return(Json(new { statusCode = ResponseStatus.Error })); }
            comment = facebookDataContext.Comments.Where(x => x.Id == comment.Id).Include("User.ProfilePhotos").FirstOrDefault();
            return(Json(new { statusCode = ResponseStatus.Success, responseMessage = HomePageDtoMapper.Map(comment, hostingEnvironment, user.Id), postId = comment.PostId }));
        }
Example #3
0
        public IActionResult Index()
        {
            ViewData["LayoutData"] = userData.GetLayoutData(HttpContext);
            int  userId       = userData.GetUser(HttpContext).Id;
            User userFullData = facebookDataContext.Users.Where(x => x.Id == userId)
                                .Include("UserRelationsDesider.Initiator.UsersPosts.Post.Comments.User.ProfilePhotos")
                                .Include("UserRelationsDesider.Initiator.UsersPosts.Post.Likes.User.ProfilePhotos")
                                .Include("UserRelationsDesider.Initiator.UsersPosts.Post.PostPhotos")
                                .Include("UserRelationsInitiator.Desider.UsersPosts.Post.Comments.User.ProfilePhotos")
                                .Include("UserRelationsInitiator.Desider.UsersPosts.Post.Likes.User.ProfilePhotos")
                                .Include("UserRelationsInitiator.Desider.UsersPosts.Post.PostPhotos")
                                .Include("UsersPosts.Post.Comments.User.ProfilePhotos")
                                .Include("UsersPosts.Post.Likes.User.ProfilePhotos")
                                .Include("UsersPosts.Post.PostPhotos")
                                .Include("ProfilePhotos")
                                .FirstOrDefault();
            HomePageDto homePageDto = HomePageDtoMapper.Map(userFullData, hostingEnvironment);

            return(View(homePageDto));
        }