Ejemplo n.º 1
0
        /// <summary>
        /// Updates the blog post.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public BlogPost UpdateBlogPost(UpdateBlogPostRequest request)
        {
            ValidateRequest(request);

            if (request.ExpirationDate.HasValue && request.ExpirationDate < request.ActivationDate)
            {
                var message = string.Format("Expiration date must be greater that activation date.");
                Logger.Error(message);
                throw new CmsApiValidationException(message);
            }

            try
            {
                UnitOfWork.BeginTransaction();
                var blog = Repository
                           .AsQueryable <BlogPost>(b => b.Id == request.Id)
                           .FirstOne();

                blog.ActivationDate = request.ActivationDate;
                blog.ExpirationDate = request.ExpirationDate;

                Repository.Save(blog);
                UnitOfWork.Commit();

                Events.OnBlogUpdated(blog);

                return(blog);
            }
            catch (Exception inner)
            {
                var message = string.Format("Failed to update blog post {0}.", request.Id);
                Logger.Error(message, inner);
                throw new CmsApiException(message, inner);
            }
        }
Ejemplo n.º 2
0
    public Result <UpdateBlogPostResponse> UpdateBlogPost(Guid reference, UpdateBlogPostRequest request)
    {
        using var session     = _apiDatabase.SessionFactory().OpenSession();
        using var transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);

        var blogPost = session
                       .Query <BlogPostRecord>()
                       .SingleOrDefault(x => x.Reference == reference);

        if (blogPost == null)
        {
            return(Result <UpdateBlogPostResponse> .Failure($"Unable to find blog post with reference: {reference}."));
        }

        var urlSlugResult = SlugService.FromText(request.Title, request.UrlSlug);

        if (urlSlugResult.IsFailure)
        {
            return(Result <UpdateBlogPostResponse> .From(urlSlugResult));
        }

        blogPost.Title   = request.Title;
        blogPost.Title   = request.Title;
        blogPost.UrlSlug = urlSlugResult.Value;
        blogPost.Summary = request.Summary;
        blogPost.Content = request.Content;

        session.Update(blogPost);

        transaction.Commit();

        return(Result <UpdateBlogPostResponse> .Of(new UpdateBlogPostResponse
        {
            Reference = blogPost.Reference,
            Title = blogPost.Title,
            UrlSlug = blogPost.UrlSlug,
            PostedAt = blogPost.PostedAt,
            Summary = blogPost.Summary,
            Content = blogPost.Content
        }));
    }
Ejemplo n.º 3
0
 public async Task <IActionResult> UpdateBlogPost([FromBody] UpdateBlogPostRequest request)
 {
     return(Ok(await mediator.Send(request)));
 }
Ejemplo n.º 4
0
    public IActionResult UpdateBlogPost([FromRoute] Guid reference, [FromBody] UpdateBlogPostRequest request)
    {
        var result = _blogService.UpdateBlogPost(reference, request);

        return(ToApiResponse(result));
    }