예제 #1
0
            public async Task <int> Handle(CreateBlogCommand request, CancellationToken cancellationToken)
            {
                var blog = new Blog();

                if (request.BlogDto == null)
                {
                    throw new ArgumentNullException("Please fill BlogDto Values");
                }
                if (request.BlogDto.CreatedBy == null)
                {
                    throw new ArgumentNullException("Please fill CreatedBy Value");
                }
                if (request.BlogDto.Post == null)
                {
                    throw new ArgumentNullException("Please fill Post Value");
                }
                blog.Post        = request.BlogDto.Post;
                blog.Author      = request.BlogDto.Author;
                blog.CreatedBy   = request.BlogDto.CreatedBy;
                blog.CreatedDate = DateTime.Now;
                blog.UpdatedBy   = request.BlogDto.CreatedBy;
                blog.UpdatedDate = DateTime.Now;
                _context.Blogs.Add(blog);
                await _context.SaveChangesAsync(CancellationToken.None);

                return(blog.Id);
            }
            public async Task <Unit> Handle(DeleteBlogCommand request, CancellationToken cancellationToken)
            {
                var blog = await _context.Blogs
                           .FirstOrDefaultAsync(l => l.Id == request.Id);

                if (blog == null)
                {
                    throw new ArgumentException(nameof(Blog), request.Id.ToString());
                }

                _context.Blogs.Remove(blog);

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            public async Task <int> Handle(UpdateBlogCommand request, CancellationToken cancellationToken)
            {
                var blog = await _context.Blogs.FindAsync(request.BlogDto.Id);

                if (blog == null)
                {
                    throw new ArgumentException(nameof(Blog), request.BlogDto.Id.ToString());
                }

                blog.Post        = request.BlogDto.Post;
                blog.Author      = request.BlogDto.Author;
                blog.UpdatedDate = DateTime.Now;

                await _context.SaveChangesAsync(cancellationToken);

                return(blog.Id);
            }