Ejemplo n.º 1
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;

            if (!string.IsNullOrWhiteSpace(model.InlineCss))
            {
                var uglifyTest = Uglify.Css(model.InlineCss);
                if (uglifyTest.HasErrors)
                {
                    foreach (var err in uglifyTest.Errors)
                    {
                        ModelState.AddModelError(model.InlineCss, err.ToString());
                    }
                    return(BadRequest(ModelState.CombineErrorMessages()));
                }

                model.InlineCss = uglifyTest.Code;
            }

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

            var postEntity = model.PostId == Guid.Empty ?
                             await _mediator.Send(new CreatePostCommand(model)) :
                             await _mediator.Send(new UpdatePostCommand(model.PostId, model));

            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));
        }
    }
Ejemplo 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));
            }
        }