Ejemplo n.º 1
0
        public void UpVote(ClaimsPrincipal user, int postId)
        {
            var userId     = _userService.GetUserWithRoles(user).User.Id;
            var postRating = _postRatingRepository.Get(x => x.UserId == userId && x.PostId == postId);

            if (postRating != null)
            {
                if (postRating.IsLiked)
                {
                    _postRatingRepository.Remove(postRating);
                }
                else
                {
                    postRating.IsLiked    = true;
                    postRating.UpdateDate = DateTime.Now;
                }
            }
            else
            {
                postRating = new PostRating()
                {
                    PostId     = postId,
                    CreateDate = DateTime.Now,
                    IsLiked    = true,
                    UserId     = userId
                };

                _postRatingRepository.Add(postRating);
            }

            _postRatingRepository.Save();
        }
Ejemplo n.º 2
0
        //ToDo: OK! PageNumber 'pageNumber' olarak değiştirelim.
        public PostHeaderListVM GetHeaderPosts(ClaimsPrincipal contextUser, string headerCode, string categoryCode, int pageNumber = 1)
        {
            var user = _userService.GetUserWithRoles(contextUser);

            var model = new PostHeaderListVM();

            var headerEntity = _headerRepository.Where(x => x.IdCode == headerCode && !user.BlockedUserIds.Contains(x.UserId), new List <string> {
                "Category", "User", "Posts"
            }).FirstOrDefault();

            var header = new PostHeaderVM();

            ApiResultVM apiModel = null;

            if (headerEntity == null)
            {
                model = GetPopularHeaders(contextUser);
                return(model);
            }
            else
            {
                if (headerEntity.IsAdminOnly && !user.IsAdmin) // Admin check
                {
                    model = GetPopularHeaders(contextUser);
                    return(model);
                }

                var title    = headerEntity.Title;
                var isBook   = title.EndsWith("(Kitap)");
                var isAuthor = title.EndsWith("(Yazar)");

                if (isBook || isAuthor)
                {
                    title = title.Remove(title.Length - 7);
                    if (isBook)
                    {
                        apiModel = _openLibraryApiService.Search(title, Common.Enums.ApiSearchTypeEnum.Title);
                    }
                    else if (isAuthor)
                    {
                        apiModel = _openLibraryApiService.Search(title, Common.Enums.ApiSearchTypeEnum.Author);
                    }
                }
            }

            headerEntity.ClickCount++;

            header.CategoryCode = headerEntity.Category.IdCode;
            header.HeaderCode   = headerEntity.IdCode;
            header.HeaderTitle  = headerEntity.Title;
            header.UserId       = headerEntity.UserId;
            header.Username     = headerEntity.User.UserName;
            header.ClickCount   = headerEntity.ClickCount;
            header.HeaderDate   = headerEntity.UpdateDate ?? headerEntity.CreateDate;

            //ToDo: OK! Sadece count çekeceksen where sorgusu yazmana gerek yok single olarak count içerisinde filtre yapabilirsin. Örnek kodu aşağıda paylaşıyorum.
            var postCount = headerEntity.Posts.Count(x => !user.BlockedUserIds.Contains(x.UserId));

            //Örnek Kod:
            //var postCount = headerEntity.Posts.Count(x => !blockedUserIds.Contains(x.UserId));


            var pageCount = (postCount / PageMaxItemCount) + ((postCount % PageMaxItemCount) > 0 ? 1 : 0);

            if (pageNumber > pageCount)
            {
                pageNumber = 1;
            }

            model.CurrentPage = pageNumber;
            model.PageCount   = pageCount;

            if (headerEntity.Posts != null)
            {
                var posts = headerEntity.Posts
                            .Where(x => !user.BlockedUserIds.Contains(x.UserId))
                            .Skip((pageNumber - 1) * PageMaxItemCount)
                            .Take(PageMaxItemCount)
                            .ToList();

                posts.ForEach(post =>
                {
                    var postVM = new PostVM()
                    {
                        PostId       = post.Id,
                        UserId       = post.UserId,
                        Username     = post.User?.UserName ?? _userService.GetUserById(post.UserId).UserName,
                        Content      = post.Content,
                        PostDate     = post.UpdateDate ?? post.CreateDate,
                        LikeCount    = _postService.GetLikeCount(post.Id),
                        DislikeCount = _postService.GetDislikeCount(post.Id)
                    };

                    if (user.Id > 0)
                    {
                        var rating = _postRatingRepository.Get(x => x.UserId == user.Id && x.PostId == post.Id);

                        if (rating != null)
                        {
                            postVM.LikeState = rating.IsLiked ? Common.Enums.PostLikeState.Liked : Common.Enums.PostLikeState.Disliked;
                        }
                    }

                    header.Posts.Add(postVM);
                });
            }

            if (apiModel != null)
            {
                header.IsApiResult = true;

                foreach (var doc in apiModel.ResultModel.Docs.Take(10))
                {
                    DateTime dateValue;
                    if (!DateTime.TryParse(doc.First_publish_year + "-01-01", out dateValue))
                    {
                        dateValue = DateTime.Now;
                    }

                    var post = new PostVM()
                    {
                        Content      = doc.Title,
                        Username     = doc.GetAuthorText(),
                        DislikeCount = 0,
                        LikeCount    = 0,
                        IsApiResult  = true,
                        PostDate     = dateValue
                    };
                    header.Posts.Add(post);
                }
            }

            model.Headers.Add(header);
            _headerRepository.Save();

            return(model);
        }