示例#1
0
            public async Task <Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
            {
                var props = GetProperties(request);

                if (props.Count > 0)
                {
                    Console.WriteLine("-------------------------");
                }

                foreach (var prop in props)
                {
                    Console.Write(prop.Key);
                    Console.Write(": ");
                    Console.WriteLine(prop.Value);
                }

                var entity = await _context.Users.FindAsync(request.UserId);

                if (entity == null)
                {
                    throw new NotFoundException(nameof(User), request.UserId);
                }

                entity.FullName   = request.FullName;
                entity.Age        = request.Age;
                entity.Occupation = request.Occupation;

                _context.Users.Update(entity);
                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
        public async Task <Product> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var product = new Product
            {
                UnitPrice   = request.UnitPrice,
                ProductName = request.ProductName
            };

            _context.Products.Add(product);
            await _context.SaveChangesAsync(cancellationToken);

            return(product);
        }
        public async Task <Unit> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Products.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Product), request.Id);
            }

            _context.Products.Remove(entity);
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <User> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var user = new User
            {
                FullName   = request.FullName,
                Occupation = request.Occupation,
                Age        = request.Age,
                Email      = request.Email,
                UserName   = request.Email.Split("@")[0]
            };

            _context.Users.Add(user);
            await _context.SaveChangesAsync(cancellationToken);

            return(user);
        }
示例#5
0
        public async Task <Unit> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Products.FindAsync(request.ProductId);

            if (entity == null)
            {
                throw new NotFoundException(nameof(Product), request.ProductId);
            }

            entity.ProductId   = request.ProductId;
            entity.ProductName = request.ProductName;
            entity.UnitPrice   = request.UnitPrice;
            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }