コード例 #1
0
        public async Task <GetGagDTO> GetGagAsync(string slug)
        {
            try
            {
                Post post = await _repository.Queryable()
                            .Include(p => p.Images)
                            .Include(p => p.Videos)
                            .Include(p => p.Likes.Select(l => l.CreatedUser))
                            .Include(p => p.Comments)
                            .Include(p => p.Tags)
                            .Include(p => p.CreatedUser).FirstOrDefaultAsync(p => p.Slug == slug && p.IsActived);

                GetGagDTO gag = Mapper.Map <GetGagDTO>(post);

                var gagQueryable = _repository.Queryable();

                var backGag = await gagQueryable.OrderByDescending(p => p.PublishedDate).FirstOrDefaultAsync(p => p.IsActived && p.PublishedDate < gag.PublishedDate);

                var forwardGag = await gagQueryable.OrderBy(p => p.PublishedDate).FirstOrDefaultAsync(p => p.IsActived && p.PublishedDate > gag.PublishedDate);

                gag.BackGagSlug    = backGag != null ? backGag.Slug : string.Empty;
                gag.ForwardGagSlug = forwardGag != null ? forwardGag.Slug : string.Empty;

                var tags = post.Tags.Select(t => t.Slug);

                var sameTagsGags = await gagQueryable
                                   .Include(p => p.Images)
                                   .Include(p => p.Videos)
                                   .OrderByDescending(p => p.PublishedDate)
                                   .Where(p => p.IsActived && p.Tags.Count(t => tags.Contains(t.Slug)) > 0 && p.Id != post.Id)
                                   .Take(4).ToListAsync();

                gag.SameTagsGags = Mapper.Map <ICollection <GetGagDTO> >(sameTagsGags);

                var sameUserGags = await gagQueryable
                                   .Include(p => p.Images)
                                   .Include(p => p.Videos)
                                   .OrderByDescending(p => p.PublishedDate)
                                   .Where(p => p.IsActived && p.CreatedUserId == post.CreatedUserId && p.Id != post.Id)
                                   .Take(6).ToListAsync();

                gag.SameUserGags = Mapper.Map <ICollection <GetGagDTO> >(sameUserGags);

                return(gag);
            }
            catch (Exception ex)
            {
                throw new Exception(ResponseCodeString.PostGet_Error, ex.InnerException);
            }
        }
コード例 #2
0
        public GetGagDTO GetGag(string slug)
        {
            try
            {
                Post post = _repository.Queryable()
                            .Include(p => p.Images)
                            .Include(p => p.Videos)
                            .Include(p => p.Likes.Select(l => l.CreatedUser))
                            .Include(p => p.Comments)
                            .Include(p => p.Tags)
                            .Include(p => p.CreatedUser).FirstOrDefault(p => p.Slug == slug && p.IsActived);
                GetGagDTO gag = Mapper.Map <GetGagDTO>(post);

                return(gag);
            }
            catch (Exception ex)
            {
                throw new Exception(ResponseCodeString.PostGet_Error, ex.InnerException);
            }
        }