Example #1
0
        public async Task <IActionResult> CreateOrEdit(PageEditModel model)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(model.CssContent))
                {
                    var uglifyTest = Uglify.Css(model.CssContent);
                    if (uglifyTest.HasErrors)
                    {
                        foreach (var err in uglifyTest.Errors)
                        {
                            ModelState.AddModelError(model.CssContent, err.ToString());
                        }
                        return(BadRequest(ModelState.CombineErrorMessages()));
                    }
                }

                var req = new UpdatePageRequest
                {
                    HtmlContent     = model.RawHtmlContent,
                    CssContent      = model.CssContent,
                    HideSidebar     = model.HideSidebar,
                    Slug            = model.Slug,
                    MetaDescription = model.MetaDescription,
                    Title           = model.Title,
                    IsPublished     = model.IsPublished
                };

                var uid = model.Id == Guid.Empty ?
                          await _pageService.CreateAsync(req) :
                          await _pageService.UpdateAsync(model.Id, req);

                _cache.Remove(CacheDivision.Page, req.Slug.ToLower());

                return(Ok(new { PageId = uid }));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error Create or Edit CustomPage.");
                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }
        }
Example #2
0
    private async Task <IActionResult> CreateOrEdit(EditPageRequest model, Func <EditPageRequest, Task <Guid> > pageServiceAction)
    {
        if (!string.IsNullOrWhiteSpace(model.CssContent))
        {
            var uglifyTest = Uglify.Css(model.CssContent);
            if (uglifyTest.HasErrors)
            {
                foreach (var err in uglifyTest.Errors)
                {
                    ModelState.AddModelError(model.CssContent, err.ToString());
                }
                return(BadRequest(ModelState.CombineErrorMessages()));
            }
        }

        var uid = await pageServiceAction(model);

        _cache.Remove(CacheDivision.Page, model.Slug.ToLower());
        return(Ok(new { PageId = uid }));
    }
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            base.OnActionExecuted(context);

            try
            {
                _cache.Remove(CacheDivision.General, "sitemap");
            }
            catch (Exception e)
            {
                Logger.LogError(e, "Error Delete sitemap cache");
            }
        }
Example #4
0
        public async Task CreateAsync(CreateCategoryRequest createCategoryRequest)
        {
            var exists = _categoryRepository.Any(c => c.RouteName == createCategoryRequest.RouteName);

            if (exists)
            {
                return;
            }

            var category = new CategoryEntity
            {
                Id          = Guid.NewGuid(),
                RouteName   = createCategoryRequest.RouteName.Trim(),
                Note        = createCategoryRequest.Note.Trim(),
                DisplayName = createCategoryRequest.DisplayName.Trim()
            };

            await _categoryRepository.AddAsync(category);

            _cache.Remove(CacheDivision.General, "allcats");

            await _blogAudit.AddAuditEntry(EventType.Content, AuditEventId.CategoryCreated, $"Category '{category.RouteName}' created");
        }
    public async Task <Unit> Handle(PurgeRecycledCommand request, CancellationToken cancellationToken)
    {
        var spec  = new PostSpec(true);
        var posts = await _postRepo.GetAsync(spec);

        await _postRepo.DeleteAsync(posts);

        foreach (var guid in posts.Select(p => p.Id))
        {
            _cache.Remove(CacheDivision.Post, guid.ToString());
        }

        return(Unit.Value);
    }
Example #6
0
        public async Task CreateAsync(string displayName, string routeName, string note = null)
        {
            var exists = _catRepo.Any(c => c.RouteName == routeName);

            if (exists)
            {
                return;
            }

            var category = new CategoryEntity
            {
                Id          = Guid.NewGuid(),
                RouteName   = routeName.Trim(),
                Note        = note?.Trim(),
                DisplayName = displayName.Trim()
            };

            await _catRepo.AddAsync(category);

            _cache.Remove(CacheDivision.General, "allcats");

            await _audit.AddEntry(BlogEventType.Content, BlogEventId.CategoryCreated, $"Category '{category.RouteName}' created");
        }
    public async Task <Unit> Handle(RestorePostCommand request, CancellationToken cancellationToken)
    {
        var pp = await _postRepo.GetAsync(request.Id);

        if (null == pp)
        {
            return(Unit.Value);
        }

        pp.IsDeleted = false;
        await _postRepo.UpdateAsync(pp);

        _cache.Remove(CacheDivision.Post, request.Id.ToString());
        return(Unit.Value);
    }
Example #8
0
    public async Task <OperationCode> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
    {
        var cat = await _catRepo.GetAsync(request.Id);

        if (cat is null)
        {
            return(OperationCode.ObjectNotFound);
        }

        cat.RouteName   = request.Payload.RouteName.Trim();
        cat.DisplayName = request.Payload.DisplayName.Trim();
        cat.Note        = request.Payload.Note?.Trim();

        await _catRepo.UpdateAsync(cat);

        _cache.Remove(CacheDivision.General, "allcats");

        return(OperationCode.Done);
    }
Example #9
0
    public async Task <Unit> Handle(DeletePostCommand request, CancellationToken cancellationToken)
    {
        var post = await _postRepo.GetAsync(request.Id);

        if (null == post)
        {
            return(Unit.Value);
        }

        if (request.SoftDelete)
        {
            post.IsDeleted = true;
            await _postRepo.UpdateAsync(post);
        }
        else
        {
            await _postRepo.DeleteAsync(post);
        }

        _cache.Remove(CacheDivision.Post, request.Id.ToString());
        return(Unit.Value);
    }
    public async Task <OperationCode> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken)
    {
        var exists = _catRepo.Any(c => c.Id == request.Id);

        if (!exists)
        {
            return(OperationCode.ObjectNotFound);
        }

        var pcs = await _postCatRepo.GetAsync(pc => pc.CategoryId == request.Id);

        if (pcs is not null)
        {
            await _postCatRepo.DeleteAsync(pcs);
        }

        await _catRepo.DeleteAsync(request.Id);

        _cache.Remove(CacheDivision.General, "allcats");

        return(OperationCode.Done);
    }
    public async Task <Unit> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
    {
        var exists = _catRepo.Any(c => c.RouteName == request.Payload.RouteName);

        if (exists)
        {
            return(Unit.Value);
        }

        var category = new CategoryEntity
        {
            Id          = Guid.NewGuid(),
            RouteName   = request.Payload.RouteName.Trim(),
            Note        = request.Payload.Note?.Trim(),
            DisplayName = request.Payload.DisplayName.Trim()
        };

        await _catRepo.AddAsync(category);

        _cache.Remove(CacheDivision.General, "allcats");

        return(Unit.Value);
    }
Example #12
0
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     base.OnActionExecuted(context);
     _cache.Remove(_division, _cacheKey);
 }
Example #13
0
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     base.OnActionExecuted(context);
     _cache.Remove(CacheDivision.General, "sitemap");
 }
Example #14
0
        public async Task <PostEntity> UpdateAsync(EditPostRequest request)
        {
            var post = await _postRepository.GetAsync(request.Id);

            if (null == post)
            {
                throw new InvalidOperationException($"Post {request.Id} is not found.");
            }

            post.CommentEnabled  = request.EnableComment;
            post.PostContent     = request.EditorContent;
            post.ContentAbstract = GetPostAbstract(
                request.EditorContent,
                AppSettings.PostAbstractWords,
                AppSettings.Editor == EditorChoice.Markdown);

            // Address #221: Do not allow published posts back to draft status
            // postModel.IsPublished = request.IsPublished;
            // Edit draft -> save and publish, ignore false case because #221
            bool isNewPublish = false;

            if (request.IsPublished && !post.IsPublished)
            {
                post.IsPublished = true;
                post.PubDateUtc  = DateTime.UtcNow;

                isNewPublish = true;
            }

            // #325: Allow changing publish date for published posts
            if (request.PublishDate != null && post.PubDateUtc.HasValue)
            {
                var tod          = post.PubDateUtc.Value.TimeOfDay;
                var adjustedDate = _dateTimeResolver.ToUtc(request.PublishDate.Value);
                post.PubDateUtc = adjustedDate.AddTicks(tod.Ticks);
            }

            post.Slug                = request.Slug;
            post.Title               = request.Title;
            post.ExposedToSiteMap    = request.ExposedToSiteMap;
            post.LastModifiedUtc     = DateTime.UtcNow;
            post.IsFeedIncluded      = request.IsFeedIncluded;
            post.ContentLanguageCode = request.ContentLanguageCode;

            // 1. Add new tags to tag lib
            foreach (var item in request.Tags.Where(item => !_tagRepository.Any(p => p.DisplayName == item)))
            {
                await _tagRepository.AddAsync(new TagEntity
                {
                    DisplayName    = item,
                    NormalizedName = TagService.NormalizeTagName(item)
                });

                await _blogAudit.AddAuditEntry(EventType.Content, AuditEventId.TagCreated,
                                               $"Tag '{item}' created.");
            }

            // 2. update tags
            post.PostTag.Clear();
            if (request.Tags.Any())
            {
                foreach (var tagName in request.Tags)
                {
                    if (!TagService.ValidateTagName(tagName))
                    {
                        continue;
                    }

                    var tag = await _tagRepository.GetAsync(t => t.DisplayName == tagName);

                    if (tag != null)
                    {
                        post.PostTag.Add(new PostTagEntity
                        {
                            PostId = post.Id,
                            TagId  = tag.Id
                        });
                    }
                }
            }

            // 3. update categories
            post.PostCategory.Clear();
            if (null != request.CategoryIds && request.CategoryIds.Length > 0)
            {
                foreach (var cid in request.CategoryIds)
                {
                    if (_categoryRepository.Any(c => c.Id == cid))
                    {
                        post.PostCategory.Add(new PostCategoryEntity
                        {
                            PostId     = post.Id,
                            CategoryId = cid
                        });
                    }
                }
            }

            await _postRepository.UpdateAsync(post);

            await _blogAudit.AddAuditEntry(
                EventType.Content,
                isNewPublish?AuditEventId.PostPublished : AuditEventId.PostUpdated,
                $"Post updated, id: {post.Id}");

            _cache.Remove(CacheDivision.Post, request.Id.ToString());
            return(post);
        }