Exemplo n.º 1
0
        public Task <string> AddPostAsync(string blogid, string username, string password, Post post, bool publish)
        {
            EnsureUser(username, password);

            return(TryExecuteAsync(async() =>
            {
                var cids = await GetCatIds(post.categories);
                if (cids.Length == 0)
                {
                    throw new ArgumentOutOfRangeException(nameof(post.categories));
                }

                var req = new UpdatePostRequest
                {
                    Title = post.title,
                    Slug = post.wp_slug ?? ToSlug(post.title),
                    EditorContent = post.description,
                    Tags = post.mt_keywords?.Split(','),
                    CategoryIds = cids,
                    ContentLanguageCode = "en-us",
                    IsPublished = publish,
                    EnableComment = true,
                    IsFeedIncluded = true,
                    ExposedToSiteMap = true,
                    PublishDate = DateTime.UtcNow
                };

                var p = await _postManageService.CreateAsync(req);
                return p.Id.ToString();
            }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateOrEdit(
            [FromForm] MagicWrapper <PostEditModel> temp, [FromServices] LinkGenerator linkGenerator)
        {
            try
            {
                if (temp.ViewModel.CategoryList.All(p => !p.IsChecked))
                {
                    ModelState.AddModelError(nameof(temp.ViewModel.CategoryList), "Please select at least one category.");
                }

                if (!temp.ViewModel.IsOriginal && string.IsNullOrWhiteSpace(temp.ViewModel.OriginLink))
                {
                    ModelState.AddModelError(nameof(temp.ViewModel.OriginLink), "Please enter the origin link.");
                }

                if (!ModelState.IsValid)
                {
                    return(Conflict(ModelState.CombineErrorMessages()));
                }

                // temp solution
                var model = temp.ViewModel;

                var tags = string.IsNullOrWhiteSpace(model.Tags)
                    ? Array.Empty <string>()
                    : model.Tags.Split(',').ToArray();

                var catIds = model.CategoryList.Where(p => p.IsChecked).Select(p => p.Id).ToArray();

                var request = new UpdatePostRequest
                {
                    Title               = model.Title.Trim(),
                    Slug                = model.Slug.Trim(),
                    Author              = model.Author?.Trim(),
                    EditorContent       = model.EditorContent,
                    EnableComment       = model.EnableComment,
                    ExposedToSiteMap    = model.ExposedToSiteMap,
                    IsFeedIncluded      = model.FeedIncluded,
                    ContentLanguageCode = model.LanguageCode,
                    Abstract            = model.Abstract,
                    IsPublished         = model.IsPublished,
                    IsFeatured          = model.Featured,
                    IsOriginal          = model.IsOriginal,
                    OriginLink          = string.IsNullOrWhiteSpace(model.OriginLink) ? null : model.OriginLink,
                    HeroImageUrl        = string.IsNullOrWhiteSpace(model.HeroImageUrl) ? null : model.HeroImageUrl,
                    Tags                = tags,
                    CategoryIds         = catIds
                };

                var tzDate = _timeZoneResolver.NowOfTimeZone;
                if (model.ChangePublishDate &&
                    model.PublishDate.HasValue &&
                    model.PublishDate <= tzDate &&
                    model.PublishDate.GetValueOrDefault().Year >= 1975)
                {
                    request.PublishDate = _timeZoneResolver.ToUtc(model.PublishDate.Value);
                }

                var postEntity = model.PostId == Guid.Empty ?
                                 await _postManageService.CreateAsync(request) :
                                 await _postManageService.UpdateAsync(model.PostId, request);

                if (model.IsPublished)
                {
                    _logger.LogInformation($"Trying to Ping URL for post: {postEntity.Id}");

                    var pubDate = postEntity.PubDateUtc.GetValueOrDefault();

                    var link = linkGenerator.GetUriByPage(HttpContext, "/Post", null,
                                                          new
                    {
                        year  = pubDate.Year,
                        month = pubDate.Month,
                        day   = pubDate.Day,
                        postEntity.Slug
                    });

                    if (_blogConfig.AdvancedSettings.EnablePingBackSend)
                    {
                        _ = Task.Run(async() => { await _pingbackSender.TrySendPingAsync(link, postEntity.PostContent); });
                    }
                }

                return(Ok(new { PostId = postEntity.Id }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error Creating New Post.");
                return(Conflict(ex.Message));
            }
        }