Esempio n. 1
0
            /// <summary>
            /// Handle the UpdatePostCommand request.
            /// </summary>
            /// <param name="request">The UpdatePostCommand request.</param>
            /// <param name="cancellationToken">A cancellation token.</param>
            public async Task <Result <Post> > Handle(UpdatePostCommand request, CancellationToken cancellationToken)
            {
                var entity = await _uow.Posts.SingleOrDefaultAsync(e => e.Id.Equals(request.Id), cancellationToken);

                if (entity == null)
                {
                    return(Result <Post> .Fail(new NotFoundException <Post>($"Could not find post, id: \"{request.Id}\"")));
                }

                entity.Title        = request.Title;
                entity.Slug         = request.Title.ToPostSlug();
                entity.Description  = request.Description;
                entity.Content      = request.Content;
                entity.Categories   = request.Categories;
                entity.Published    = request.Published;
                entity.CoverUrl     = request.CoverUrl;
                entity.CoverCaption = request.CoverCaption;
                entity.CoverLink    = request.CoverLink;

                entity = _postUrlHelper.ReplaceBaseUrlWithUrlFormat(entity);

                _uow.Posts.Update(entity);
                var result = await _uow.SaveChangesAsync(cancellationToken);

                if (!result.IsSuccess)
                {
                    return(Result <Post> .Fail(result.Exception));
                }

                return(Result <Post> .Success(entity));
            }
Esempio n. 2
0
            /// <summary>
            /// Handle the AddPostCommand request.
            /// </summary>
            /// <param name="request">The AddPostCommand request.</param>
            /// <param name="cancellationToken">A cancellation token.</param>
            public async Task <Result <Post> > Handle(AddPostCommand request, CancellationToken cancellationToken)
            {
                var author = await _uow.Authors.SingleOrDefaultAsync(a => a.UserName.Equals(request.UserName), cancellationToken);

                if (author == null)
                {
                    return(Result <Post> .Fail(new NotFoundException($"Could not find author for \"{request.UserName}\".")));
                }

                var entity = new Post
                {
                    AuthorId     = author.Id,
                    Title        = request.Title,
                    Slug         = request.Title.ToPostSlug(),
                    Description  = request.Description,
                    Content      = request.Content,
                    Categories   = request.Categories,
                    Published    = request.Published,
                    CoverUrl     = request.CoverUrl,
                    CoverCaption = request.CoverCaption,
                    CoverLink    = request.CoverLink
                };

                entity = _postUrlHelper.ReplaceBaseUrlWithUrlFormat(entity);

                _uow.Posts.Add(entity);
                var result = await _uow.SaveChangesAsync(cancellationToken);

                if (!result.IsSuccess)
                {
                    return(Result <Post> .Fail(result.Exception));
                }

                return(Result <Post> .Success(entity));
            }
            /// <summary>
            /// Handle the UpdateBlogSettingsCommand request.
            /// </summary>
            /// <param name="request">The UpdateBlogSettingsCommand request.</param>
            /// <param name="cancellationToken">A cancellation token.</param>
            public async Task <Result <BlogSettings> > Handle(UpdateBlogSettingsCommand request, CancellationToken cancellationToken)
            {
                var entity = await _uow.BlogSettings.SingleOrDefaultAsync(cancellationToken);

                if (entity == null)
                {
                    entity = new BlogSettings();
                }

                entity.Title        = request.Title;
                entity.Description  = request.Description;
                entity.CoverUrl     = _fileUrlHelper.ReplaceBaseUrlWithUrlFormat(request.CoverUrl);
                entity.CoverCaption = request.CoverCaption;
                entity.CoverLink    = request.CoverLink;

                if (entity.Created == DateTime.MinValue)
                {
                    _uow.BlogSettings.Add(entity);
                }
                else
                {
                    _uow.BlogSettings.Update(entity);
                }

                var result = await _uow.SaveChangesAsync(cancellationToken);

                if (!result.IsSuccess)
                {
                    return(Result <BlogSettings> .Fail(result.Exception));
                }

                return(Result <BlogSettings> .Success(entity));
            }
Esempio n. 4
0
        public async Task Add_Should_AddBlogSettings()
        {
            var blogSettings = new BlogSettings
            {
                Title        = "blog title",
                Description  = "blog description",
                CoverCaption = "blog cover caption",
                CoverLink    = "blog cover link",
                CoverUrl     = "blog cover url"
            };

            _blogSettingsRepository.Add(blogSettings);
            await _uow.SaveChangesAsync(CancellationToken.None);

            var result = _dbContext.BlogSettings.SingleOrDefault();

            result.Should().NotBeNull();
            result.Title.Should().Be("blog title");
            result.Description.Should().Be("blog description");
        }
Esempio n. 5
0
        public async Task Add_Should_AddPost()
        {
            var post = new Post
            {
                AuthorId    = _authorId,
                Title       = "Post title 999",
                Slug        = "post-title-999",
                Categories  = "cat999",
                Description = "Description",
                Content     = "Content"
            };

            _postRepository.Add(post);
            await _uow.SaveChangesAsync(CancellationToken.None);

            var result = PostCollection.Find(p => p.Title.Equals("Post title 999")).SingleOrDefault();

            result.Should().NotBeNull();
            result.Title.Should().Be("Post title 999");
            result.Description.Should().Be("Description");
        }
Esempio n. 6
0
            /// <summary>
            /// Handle the DeletePostCommand request.
            /// </summary>
            /// <param name="request">The DeletePostCommand request.</param>
            /// <param name="cancellationToken">A cancellation token.</param>
            public async Task <Result> Handle(DeletePostCommand request, CancellationToken cancellationToken)
            {
                var entity = await _uow.Posts.SingleOrDefaultAsync(e => e.Id.Equals(request.Id), cancellationToken);

                if (entity == null)
                {
                    return(Result.Fail(new NotFoundException <Post>($"Could not find post, id: \"{request.Id}\"")));
                }

                entity.Published = null;

                _uow.Posts.Remove(entity);
                var result = await _uow.SaveChangesAsync(cancellationToken);

                if (!result.IsSuccess)
                {
                    return(Result.Fail(result.Exception));
                }

                return(Result.Success());
            }
Esempio n. 7
0
        public async Task AddCommentAsync(Comment entity)
        {
            await _blogUnitOfWork.CommentRepository.AddAsync(entity);

            await _blogUnitOfWork.SaveChangesAsync();
        }