コード例 #1
0
 public void Update(BlogViewModel blog)
 {
     _blogRepository.Update(Mapper.Map <BlogViewModel, Blog>(blog));
     if (!string.IsNullOrEmpty(blog.Tags))
     {
         string[] tags = blog.Tags.Split(',');
         foreach (string t in tags)
         {
             var tagId = TextHelper.ToUnsignString(t);
             if (!_tagRepository.FindAll(x => x.Id == tagId).Any())
             {
                 Tag tag = new Tag
                 {
                     Id   = tagId,
                     Name = t,
                     Type = CommonConstants.ProductTag
                 };
                 _tagRepository.Add(tag);
             }
             _blogTagRepository.RemoveMultiple(_blogTagRepository.FindAll(x => x.Id == blog.Id).ToList());
             BlogTag blogTag = new BlogTag
             {
                 BlogId = blog.Id,
                 TagId  = tagId
             };
             _blogTagRepository.Add(blogTag);
         }
     }
 }
コード例 #2
0
ファイル: BlogService.cs プロジェクト: hungdang3195/WebTinTuc
        public async Task Update(BlogViewModel blog)
        {
            await _blogRepository.Update(new BlogViewModel().Map(blog));

            if (!string.IsNullOrEmpty(blog.Tags))
            {
                var blogTags = await _blogTagRepository.FindAll(x => x.BlogId == blog.Id);

                await _blogTagRepository.RemoveMultiple(blogTags.ToList());

                string[] tags = blog.Tags.Split(',');
                foreach (string t in tags)
                {
                    var tagId = TextHelper.ToUnsignString(t);
                    if (!(await _tagRepository.FindAll(x => x.Id == tagId)).Any())
                    {
                        Tag tag = new Tag
                        {
                            Id   = tagId,
                            Name = t,
                            Type = CommonConstants.ProductTag
                        };
                        await _tagRepository.Add(tag);
                    }
                    await _blogTagRepository.RemoveMultiple((await _blogTagRepository.FindAll(x => x.Id == blog.Id)).ToList());

                    BlogTag blogTag = new BlogTag
                    {
                        BlogId = blog.Id,
                        TagId  = tagId
                    };
                    await _blogTagRepository.Add(blogTag);
                }
            }
        }
コード例 #3
0
ファイル: BlogService.cs プロジェクト: ThuanNP/teducoreapp
        public List <BlogViewModel> GetListByTag(string tagId, int page, int pageSize, out int totalRow)
        {
            var query = from p in _blogRepository.FindAll()
                        join pt in _blogTagRepository.FindAll()
                        on p.Id equals pt.BlogId
                        where pt.TagId == tagId && p.Status == Status.Active
                        orderby p.DateCreated descending
                        select p;

            totalRow = query.Count();
            query    = query.Skip((page - 1) * pageSize).Take(pageSize);
            return(query.ProjectTo <BlogViewModel>().ToList());
        }
コード例 #4
0
        public BlogViewModel GetById(int id)
        {
            var blog    = _blogRepository.FindById(id);
            var blogTag = _blogTagRepository
                          .FindAll(blTag => blTag.BlogId == id)
                          .Select(x => x.Tag != null ? x.Tag.Name : string.Empty);

            if (blogTag.Any())
            {
                blog.Tags = string.Join(",", blogTag.ToList());
            }
            return(Mapper.Map <Blog, BlogViewModel>(_blogRepository.FindById(id)));
        }
コード例 #5
0
        public List <TagViewModel> GetTagsByBlogId(int blogId)
        {
            var blogTags = _blogTagRepository.FindAll();
            var tags     = _tagRepository.FindAll();
            var query    = from t in tags
                           join bt in blogTags
                           on t.Id equals bt.TagId
                           where bt.BlogId == blogId
                           select t;

            var tagVms = query.ProjectTo <TagViewModel>().ToList();

            return(tagVms);
        }
コード例 #6
0
        public async Task <IActionResult> Index(int id)
        {
            var model = new BlogDetailViewModel();

            model.Blog = await _blogService.GetById(id);

            model.Blog.CountComment = (await _blogComment.FindAll()).Count(x => x.BlogId == id);
            model.Categories        = await _blogCategoryService.GetAll();

            model.PopularPosts = (from k in (await _blogService.GetAll()).OrderByDescending(x => x.DateModified).ToList()
                                  select new BlogViewModel
            {
                Image = k.Image,
                Id = k.Id,
                CountComment = 4,
                DateModified = k.DateModified,
                Description = k.Description,
                SeoAlias = k.SeoAlias
            }).Take(5);
            model.ReLateBlogs = (from b in (await _blogService.GetAll()).Where(x => x.BlogCategoryId == model.Blog.BlogCategoryId && x.Id != id).ToList()
                                 select new BlogViewModel
            {
                Image = b.Image,
                Id = b.Id,
                CountComment = 0,
                //  CountComment =(await _blogComment.FindAll(y => y.BlogId == b.Id)).Count(),
                DateModified = b.DateModified,
                Description = b.Description,
                SeoAlias = b.SeoAlias
            }).Take(5);

            model.BlogTags = (await _blogTagRepository.FindAll()).Take(10);

            HttpContext.Session.Set(CommonConstants.BlogId, id);
            model.Tags = (await _blogTagRepository.FindAll(x => x.BlogId == id)).Select(x => x.TagId).ToList();
            return(View(model));
        }