public async Task <Response <int> > Handle(AddPostCommand command, CancellationToken cancellationToken)
        {
            var post = _mapper.Map <Domain.Persistence.Entities.Post>(command);

            try
            {
                post = await _persistenceUnitOfWork.Post.AddAsync(post);

                await _persistenceUnitOfWork.CommitAsync();
            }
            catch (Exception e)
            {
                _persistenceUnitOfWork.Dispose();
                _logger.LogError(e, "Failed to save new post in database");
            }
            return(Response <int> .Success(post.Id, "Successfully saved post"));
        }
Example #2
0
        public async Task <Response <int> > Handle(AddCategoryCommand command, CancellationToken cancellationToken)
        {
            if (await _persistenceUnitOfWork.Category.Entity.Where(x => x.Slug == command.Slug)
                .AnyAsync(cancellationToken: cancellationToken))
            {
                return(Response <int> .Fail("The slug already exists. Please try a different one"));
            }
            var category = _mapper.Map <Domain.Persistence.Entities.Category>(command);

            try
            {
                await _persistenceUnitOfWork.Category.AddAsync(category);

                await _persistenceUnitOfWork.CommitAsync();

                return(Response <int> .Success(category.Id, "Successfully added the category"));
            }
            catch (Exception e)
            {
                _persistenceUnitOfWork.Dispose();
                _logger.LogError(e, "Failed to add category: {category} ", command.Title);
                return(Response <int> .Fail("Failed to add the category"));
            }
        }