Exemple #1
0
        public async Task <PostEntity> CreateAsync(CreatePostRequest request)
        {
            var abs = ContentProcessor.GetPostAbstract(
                request.EditorContent, _settings.PostAbstractWords,
                _settings.Editor == EditorChoice.Markdown);

            var post = new PostEntity
            {
                CommentEnabled      = request.EnableComment,
                Id                  = Guid.NewGuid(),
                PostContent         = request.EditorContent,
                ContentAbstract     = abs,
                CreateOnUtc         = DateTime.UtcNow,
                Slug                = request.Slug.ToLower().Trim(),
                Title               = request.Title.Trim(),
                ContentLanguageCode = request.ContentLanguageCode,
                ExposedToSiteMap    = request.ExposedToSiteMap,
                IsFeedIncluded      = request.IsFeedIncluded,
                PubDateUtc          = request.IsPublished ? DateTime.UtcNow : (DateTime?)null,
                IsDeleted           = false,
                IsPublished         = request.IsPublished,
                PostExtension       = new()
                {
                    Hits  = 0,
                    Likes = 0
                }
            };

            // check if exist same slug under the same day
            // linq to sql fix:
            // cannot write "p.PubDateUtc.GetValueOrDefault().Date == DateTime.UtcNow.Date"
            // it will not blow up, but can result in select ENTIRE posts and evaluated in memory!!!
            // - The LINQ expression 'where (Convert([p]?.PubDateUtc?.GetValueOrDefault(), DateTime).Date == DateTime.UtcNow.Date)' could not be translated and will be evaluated locally
            // Why EF Core this diao yang?
            if (_postRepo.Any(p =>
                              p.Slug == post.Slug &&
                              p.PubDateUtc != null &&
                              p.PubDateUtc.Value.Year == DateTime.UtcNow.Date.Year &&
                              p.PubDateUtc.Value.Month == DateTime.UtcNow.Date.Month &&
                              p.PubDateUtc.Value.Day == DateTime.UtcNow.Date.Day))
            {
                var uid = Guid.NewGuid();
                post.Slug += $"-{uid.ToString().ToLower().Substring(0, 8)}";
                _logger.LogInformation($"Found conflict for post slug, generated new slug: {post.Slug}");
            }

            // add categories
            if (request.CategoryIds is not null and {
                Length : > 0
            })
Exemple #2
0
        public async Task <PostEntity> CreateAsync(UpdatePostRequest request)
        {
            var abs = ContentProcessor.GetPostAbstract(
                string.IsNullOrEmpty(request.Abstract) ? request.EditorContent : request.Abstract.Trim(),
                _settings.PostAbstractWords,
                _settings.Editor == EditorChoice.Markdown);

            var post = new PostEntity
            {
                CommentEnabled      = request.EnableComment,
                Id                  = Guid.NewGuid(),
                PostContent         = request.EditorContent,
                ContentAbstract     = abs,
                CreateTimeUtc       = DateTime.UtcNow,
                LastModifiedUtc     = DateTime.UtcNow, // Fix draft orders
                Slug                = request.Slug.ToLower().Trim(),
                Author              = request.Author?.Trim(),
                Title               = request.Title.Trim(),
                ContentLanguageCode = request.ContentLanguageCode,
                ExposedToSiteMap    = request.ExposedToSiteMap,
                IsFeedIncluded      = request.IsFeedIncluded,
                PubDateUtc          = request.IsPublished ? DateTime.UtcNow : null,
                IsDeleted           = false,
                IsPublished         = request.IsPublished,
                IsFeatured          = request.IsFeatured,
                IsOriginal          = request.IsOriginal,
                OriginLink          = string.IsNullOrWhiteSpace(request.OriginLink) ? null : Helper.SterilizeLink(request.OriginLink),
                HeroImageUrl        = string.IsNullOrWhiteSpace(request.HeroImageUrl) ? null : Helper.SterilizeLink(request.HeroImageUrl),
                PostExtension       = new()
                {
                    Hits  = 0,
                    Likes = 0
                }
            };

            // check if exist same slug under the same day
            var todayUtc = DateTime.UtcNow.Date;

            if (_postRepo.Any(new PostSpec(post.Slug, todayUtc)))
            {
                var uid = Guid.NewGuid();
                post.Slug += $"-{uid.ToString().ToLower()[..8]}";
        public async Task <PostEntity> CreateAsync(UpdatePostRequest request)
        {
            var abs = ContentProcessor.GetPostAbstract(
                request.EditorContent, _settings.PostAbstractWords,
                _settings.Editor == EditorChoice.Markdown);

            var post = new PostEntity
            {
                CommentEnabled      = request.EnableComment,
                Id                  = Guid.NewGuid(),
                PostContent         = request.EditorContent,
                ContentAbstract     = abs,
                CreateTimeUtc       = DateTime.UtcNow,
                Slug                = request.Slug.ToLower().Trim(),
                Title               = request.Title.Trim(),
                ContentLanguageCode = request.ContentLanguageCode,
                ExposedToSiteMap    = request.ExposedToSiteMap,
                IsFeedIncluded      = request.IsFeedIncluded,
                PubDateUtc          = request.IsPublished ? DateTime.UtcNow : (DateTime?)null,
                IsDeleted           = false,
                IsPublished         = request.IsPublished,
                PostExtension       = new()
                {
                    Hits  = 0,
                    Likes = 0
                }
            };

            // check if exist same slug under the same day
            var todayUtc = DateTime.UtcNow.Date;

            if (_postRepo.Any(new PostSpec(post.Slug, todayUtc)))
            {
                var uid = Guid.NewGuid();
                post.Slug += $"-{uid.ToString().ToLower().Substring(0, 8)}";
                _logger.LogInformation($"Found conflict for post slug, generated new slug: {post.Slug}");
            }

            // add categories
            if (request.CategoryIds is not null and {
                Length : > 0
            })
Exemple #4
0
        public async Task <CommentReply> AddReply(Guid commentId, string replyContent)
        {
            var cmt = await _commentRepo.GetAsync(commentId);

            if (cmt is null)
            {
                throw new InvalidOperationException($"Comment {commentId} is not found.");
            }

            var id    = Guid.NewGuid();
            var model = new CommentReplyEntity
            {
                Id           = id,
                ReplyContent = replyContent,
                ReplyTimeUtc = DateTime.UtcNow,
                CommentId    = commentId
            };

            await _commentReplyRepo.AddAsync(model);

            var reply = new CommentReply
            {
                CommentContent   = cmt.CommentContent,
                CommentId        = commentId,
                Email            = cmt.Email,
                Id               = model.Id,
                PostId           = cmt.PostId,
                PubDateUtc       = cmt.Post.PubDateUtc.GetValueOrDefault(),
                ReplyContent     = model.ReplyContent,
                ReplyContentHtml = ContentProcessor.MarkdownToContent(model.ReplyContent, ContentProcessor.MarkdownConvertType.Html),
                ReplyTimeUtc     = model.ReplyTimeUtc,
                Slug             = cmt.Post.Slug,
                Title            = cmt.Post.Title
            };

            await _audit.AddAuditEntry(EventType.Content, AuditEventId.CommentReplied, $"Replied comment id '{commentId}'");

            return(reply);
        }
Exemple #5
0
 private string FormatPostContent(string rawContent)
 {
     return(_settings.Editor == EditorChoice.Markdown ?
            ContentProcessor.MarkdownToContent(rawContent, ContentProcessor.MarkdownConvertType.Html, false) :
            rawContent);
 }