Beispiel #1
0
        public async Task <CommandResult <UpdateBlogPostCommand> > ExecuteAsync(UpdateBlogPostCommand command)
        {
            try
            {
                var post = await _context.BlogPosts.Where(bp => bp.Id == command.Id).FirstOrDefaultAsync();

                if (post != null)
                {
                    post.Author      = command.NewAuthor;
                    post.Slug        = BlogPostExtensions.CreateSlug(command.NewTitle);
                    post.Title       = command.NewTitle;
                    post.Description = command.NewDescription;
                    post.Content     = command.NewContent;
                    post.ModifiedAt  = command.LastModifiedAt;
                    post.PublishOn   = command.NewPublishOn;
                    post.Public      = command.NewPublic;
                    post.Image       = command.NewImage;

                    await _context.SaveChangesAsync();

                    return(new CommandResult <UpdateBlogPostCommand>(command, true));
                }
                else
                {
                    return(new CommandResult <UpdateBlogPostCommand>(command, false));
                }
            }
            catch (Exception ex)
            {
                return(new CommandResult <UpdateBlogPostCommand>(command, false, ex));
            }
        }
Beispiel #2
0
        public async Task <CommandResult <AddBlogPostCommand> > ExecuteAsync(AddBlogPostCommand command)
        {
            try
            {
                var bp = new BlogPost
                {
                    Slug        = BlogPostExtensions.CreateSlug(command.Title),
                    Author      = command.Author,
                    AuthorId    = command.Author.Id,
                    Title       = command.Title,
                    Content     = command.Content,
                    Description = command.Description,
                    CreatedAt   = command.CreatedAt,
                    ModifiedAt  = command.ModifiedAt,
                    Public      = command.Public,
                    PublishOn   = command.PublishOn,
                    Image       = command.Image,
                    IsHtml      = command.IsHtml
                };

                _context.BlogPosts.Add(bp);

                await _context.SaveChangesAsync();

                command.Id = bp.Id;

                return(new CommandResult <AddBlogPostCommand>(command, true));
            }
            catch (Exception ex)
            {
                return(new CommandResult <AddBlogPostCommand>(command, false, ex));
            }
        }
Beispiel #3
0
        public static async Task UpdateSlugs(IServiceProvider container)
        {
            using (var serviceScope = container.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var _context = serviceScope.ServiceProvider.GetRequiredService <BlogDbContext>();

                foreach (var bp in _context.BlogPosts)
                {
                    bp.Slug = BlogPostExtensions.CreateSlug(bp.Title);
                }

                await _context.SaveChangesAsync();
            }
        }