public Task <Response <Guid> > CreatePageAsync(CreateCustomPageRequest request) { return(TryExecuteAsync <Guid>(async() => { var uid = Guid.NewGuid(); var customPage = new CustomPageEntity { Id = uid, Title = request.Title.Trim(), RouteName = request.RouteName.ToLower().Trim(), CreateOnUtc = DateTime.UtcNow, HtmlContent = _htmlCodec.HtmlEncode(request.HtmlContent), CssContent = request.CssContent, HideSidebar = request.HideSidebar }; await _customPageRepository.AddAsync(customPage); return new SuccessResponse <Guid>(uid); })); }
public async Task <Response <PostEntity> > CreateNewPost(CreatePostRequest request) { return(await TryExecuteAsync <PostEntity>(async() => { var postModel = new PostEntity { CommentEnabled = request.EnableComment, Id = Guid.NewGuid(), PostContent = AppSettings.Editor == EditorChoice.Markdown ? request.EditorContent : _htmlCodec.HtmlEncode(request.EditorContent), ContentAbstract = Utils.GetPostAbstract( request.EditorContent, AppSettings.PostAbstractWords, AppSettings.Editor == EditorChoice.Markdown), CreateOnUtc = DateTime.UtcNow, Slug = request.Slug.ToLower().Trim(), Title = request.Title.Trim(), PostPublish = new PostPublishEntity { IsDeleted = false, IsPublished = request.IsPublished, PubDateUtc = request.IsPublished ? DateTime.UtcNow : (DateTime?)null, ExposedToSiteMap = request.ExposedToSiteMap, IsFeedIncluded = request.IsFeedIncluded, Revision = 0, ContentLanguageCode = request.ContentLanguageCode, PublisherIp = request.RequestIp }, PostExtension = new PostExtensionEntity { Hits = 0, Likes = 0 } }; // check if exist same slug under the same day // linq to sql fix: // cannot write "p.PostPublish.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.PostPublish]?.PubDateUtc?.GetValueOrDefault(), DateTime).Date == DateTime.UtcNow.Date)' could not be translated and will be evaluated locally // Why EF Core this diao yang? if (_postRepository.Any(p => p.Slug == postModel.Slug && p.PostPublish.PubDateUtc != null && p.PostPublish.PubDateUtc.Value.Year == DateTime.UtcNow.Date.Year && p.PostPublish.PubDateUtc.Value.Month == DateTime.UtcNow.Date.Month && p.PostPublish.PubDateUtc.Value.Day == DateTime.UtcNow.Date.Day)) { var uid = Guid.NewGuid(); postModel.Slug += $"-{uid.ToString().ToLower().Substring(0, 8)}"; Logger.LogInformation($"Found conflict for post slug, generated new slug: {postModel.Slug}"); } // add categories if (null != request.CategoryIds && request.CategoryIds.Length > 0) { foreach (var cid in request.CategoryIds) { if (_categoryRepository.Any(c => c.Id == cid)) { postModel.PostCategory.Add(new PostCategoryEntity { CategoryId = cid, PostId = postModel.Id }); } } } // add tags if (null != request.Tags && request.Tags.Length > 0) { foreach (var item in request.Tags) { if (!Utils.ValidateTagName(item)) { continue; } var tag = _tagRepository.Get(q => q.DisplayName == item); if (null == tag) { var newTag = new TagEntity { DisplayName = item, NormalizedName = Utils.NormalizeTagName(item) }; tag = _tagRepository.Add(newTag); await _moongladeAudit.AddAuditEntry(EventType.Content, EventId.TagCreated, $"Tag '{tag.NormalizedName}' created."); } postModel.PostTag.Add(new PostTagEntity { TagId = tag.Id, PostId = postModel.Id }); } } await _postRepository.AddAsync(postModel); Logger.LogInformation($"New Post Created Successfully. PostId: {postModel.Id}"); await _moongladeAudit.AddAuditEntry(EventType.Content, EventId.PostCreated, $"Post created, id: {postModel.Id}"); return new SuccessResponse <PostEntity>(postModel); })); }
public Task <Response <CommentListItem> > AddCommentAsync(NewCommentRequest request) { return(TryExecuteAsync <CommentListItem>(async() => { // 1. Check comment enabled or not if (!_blogConfig.ContentSettings.EnableComments) { return new FailedResponse <CommentListItem>((int)ResponseFailureCode.CommentDisabled); } // 2. Check user email domain var bannedDomains = _blogConfig.EmailSettings.BannedMailDomain; if (bannedDomains.Any()) { var address = new MailAddress(request.Email); if (bannedDomains.Contains(address.Host)) { return new FailedResponse <CommentListItem>((int)ResponseFailureCode.EmailDomainBlocked); } } // 3. Encode HTML request.Username = _htmlCodec.HtmlEncode(request.Username); // 4. Harmonize banned keywords if (_blogConfig.ContentSettings.EnableWordFilter) { var dw = _blogConfig.ContentSettings.DisharmonyWords; var maskWordFilter = new MaskWordFilter(new StringWordSource(dw)); request.Username = maskWordFilter.FilterContent(request.Username); request.Content = maskWordFilter.FilterContent(request.Content); } var model = new CommentEntity { Id = Guid.NewGuid(), Username = request.Username, CommentContent = request.Content, PostId = request.PostId, CreateOnUtc = DateTime.UtcNow, Email = request.Email, IPAddress = request.IpAddress, IsApproved = false, UserAgent = request.UserAgent }; await _commentRepository.AddAsync(model); var spec = new PostSpec(request.PostId, false); var postTitle = _postRepository.SelectFirstOrDefault(spec, p => p.Title); var item = new CommentListItem { Id = model.Id, CommentContent = model.CommentContent, CreateOnUtc = model.CreateOnUtc, Email = model.Email, IpAddress = model.IPAddress, IsApproved = model.IsApproved, PostTitle = postTitle, Username = model.Username }; return new SuccessResponse <CommentListItem>(item); })); }