Пример #1
0
        public IActionResult Edit(PostEditModel model)
        {
            if (ModelState.IsValid)
            {
                var tagList = string.IsNullOrWhiteSpace(model.Tags)
                    ? new List <string>()
                    : model.Tags.Split(',').ToList();

                var request = new CreateEditPostRequest
                {
                    PostId              = model.PostId,
                    Title               = model.Title.Trim(),
                    Slug                = model.Slug.Trim(),
                    HtmlContent         = model.HtmlContent,
                    EnableComment       = model.EnableComment,
                    ExposedToSiteMap    = model.ExposedToSiteMap,
                    IsFeedIncluded      = model.FeedIncluded,
                    ContentLanguageCode = model.ContentLanguageCode,
                    IsPublished         = model.IsPublished,
                    Tags                = tagList,
                    CategoryIds         = model.SelectedCategoryIds.ToList()
                };

                var response = _postService.EditPost(request);
                if (response.IsSuccess)
                {
                    if (model.IsPublished)
                    {
                        Logger.LogInformation($"Trying to Ping URL for post: {response.Item.Id}");

                        var pubDate = response.Item.PostPublish.PubDateUtc.GetValueOrDefault();
                        var link    = GetPostUrl(_linkGenerator, pubDate, response.Item.Slug);

                        if (AppSettings.EnablePingBackSend)
                        {
                            Task.Run(async() => { await _pingbackSender.TrySendPingAsync(link, response.Item.PostContent); });
                        }
                    }

                    return(RedirectToAction(nameof(Manage)));
                }

                ModelState.AddModelError("", response.Message);
                return(View("CreateOrEdit", model));
            }

            return(View("CreateOrEdit", model));
        }
Пример #2
0
        public Response <Post> EditPost(CreateEditPostRequest request)
        {
            try
            {
                var postModel = _postRepository.Get(request.PostId);
                if (null == postModel)
                {
                    return(new FailedResponse <Post>((int)ResponseFailureCode.PostNotFound));
                }

                postModel.CommentEnabled          = request.EnableComment;
                postModel.PostContent             = HttpUtility.HtmlEncode(request.HtmlContent);
                postModel.ContentAbstract         = Utils.GetPostAbstract(request.HtmlContent, AppSettings.PostSummaryWords);
                postModel.PostPublish.IsPublished = request.IsPublished;
                postModel.Slug  = request.Slug;
                postModel.Title = request.Title;
                postModel.PostPublish.ExposedToSiteMap    = request.ExposedToSiteMap;
                postModel.PostPublish.LastModifiedUtc     = DateTime.UtcNow;
                postModel.PostPublish.IsFeedIncluded      = request.IsFeedIncluded;
                postModel.PostPublish.ContentLanguageCode = request.ContentLanguageCode;

                ++postModel.PostPublish.Revision;

                // from draft
                if (!postModel.PostPublish.PubDateUtc.HasValue)
                {
                    postModel.PostPublish.PubDateUtc = DateTime.UtcNow;
                }

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

                // 2. update tags
                postModel.PostTag.Clear();
                if (request.Tags.Any())
                {
                    request.Tags.ForEach(t =>
                    {
                        var tag = _tagRepository.Get(_ => _.DisplayName == t);
                        if (tag != null)
                        {
                            postModel.PostTag.Add(new PostTag
                            {
                                PostId = postModel.Id,
                                TagId  = tag.Id
                            });
                        }
                    });
                }

                // 3. update categories
                postModel.PostCategory.Clear();
                if (null != request.CategoryIds && request.CategoryIds.Count > 0)
                {
                    request.CategoryIds.ForEach(cid =>
                    {
                        if (_categoryRepository.Any(c => c.Id == cid))
                        {
                            postModel.PostCategory.Add(new PostCategory
                            {
                                PostId     = postModel.Id,
                                CategoryId = cid
                            });
                        }
                    });
                }

                _postRepository.Update(postModel);
                return(new SuccessResponse <Post>(postModel));
            }
            catch (Exception e)
            {
                Logger.LogError(e, $"Error Editing Post, PostId: {request.PostId}");
                return(new FailedResponse <Post>((int)ResponseFailureCode.GeneralException));
            }
        }
Пример #3
0
        public Response <Post> CreateNewPost(CreateEditPostRequest request)
        {
            try
            {
                var postModel = new Post
                {
                    CommentEnabled  = request.EnableComment,
                    Id              = request.PostId,
                    PostContent     = HttpUtility.HtmlEncode(request.HtmlContent),
                    ContentAbstract = Utils.GetPostAbstract(request.HtmlContent, AppSettings.PostSummaryWords),
                    CreateOnUtc     = DateTime.UtcNow,
                    Slug            = request.Slug.Trim(),
                    Title           = request.Title.Trim(),
                    PostPublish     = new PostPublish
                    {
                        IsDeleted           = false,
                        IsPublished         = request.IsPublished,
                        PubDateUtc          = request.IsPublished ? DateTime.UtcNow : (DateTime?)null,
                        ExposedToSiteMap    = request.ExposedToSiteMap,
                        IsFeedIncluded      = request.IsFeedIncluded,
                        Revision            = 0,
                        ContentLanguageCode = request.ContentLanguageCode
                    }
                };

                // add default values if fields are not assigned
                ApplyDefaultValuesOnPost(postModel);

                // check if exist same slug under the same day
                if (_postRepository.Any(p =>
                                        p.Slug == postModel.Slug &&
                                        p.PostPublish.PubDateUtc.GetValueOrDefault().Date == DateTime.UtcNow.Date))
                {
                    var uid = Guid.NewGuid();
                    postModel.Slug += $"-{uid.ToString().Substring(0, 8)}";
                    Logger.LogInformation($"Found conflict for post slug, generated new slug: {postModel.Slug}");
                }

                // add categories
                if (null != request.CategoryIds && request.CategoryIds.Count > 0)
                {
                    request.CategoryIds.ForEach(cid =>
                    {
                        if (_categoryRepository.Any(c => c.Id == cid))
                        {
                            postModel.PostCategory.Add(new PostCategory
                            {
                                CategoryId = cid,
                                PostId     = postModel.Id
                            });
                        }
                    });
                }

                // add tags
                if (null != request.Tags && request.Tags.Count > 0)
                {
                    var tagsList = new List <Tag>();
                    foreach (var item in request.Tags)
                    {
                        var tag = _tagRepository.Get(q => q.DisplayName == item);
                        if (null == tag)
                        {
                            // for new tags
                            var newTag = new Tag
                            {
                                DisplayName    = item,
                                NormalizedName = Utils.NormalizeTagName(item)
                            };

                            tagsList.Add(newTag);
                            _tagRepository.Add(newTag);
                        }
                        else
                        {
                            // existing tags
                            tagsList.Add(tag);
                        }
                    }

                    tagsList.ForEach(t => postModel.PostTag.Add(new PostTag
                    {
                        TagId  = t.Id,
                        PostId = postModel.Id
                    }));
                }

                _postRepository.Add(postModel);
                Logger.LogInformation($"New Post Created Successfully. PostId: {postModel.Id}");
                return(new SuccessResponse <Post>(postModel));
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Error in {nameof(CreateNewPost)}");
                return(new FailedResponse <Post>((int)ResponseFailureCode.GeneralException));
            }
        }