コード例 #1
0
        private async Task SeedVisualizationsAsync(CancellationToken cancellationToken)
        {
            var visualizations = new Visualization[]
            {
                new Visualization()
                {
                    Name = "Visualization 1", CustomerId = Customers.First().Key
                },
                new Visualization()
                {
                    Name = "Visualization 2", CustomerId = Customers.Last().Key
                }
            };

            _context.Visualizations.AddRange(visualizations);

            await _context.SaveChangesAsync(cancellationToken);
        }
コード例 #2
0
            public async Task <Unit> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
            {
                var entity = new Customer
                {
                    CustomerId = request.Id,
                    Name       = request.Name,
                };

                _context.Customers.Add(entity);

                await _context.SaveChangesAsync(cancellationToken);

                await _mediator.Publish(new CustomerCreated { CustomerId = entity.CustomerId }, cancellationToken);

                return(Unit.Value);
            }
コード例 #3
0
            public async Task <Unit> Handle(UpdateCustomerCommand request, CancellationToken cancellationToken)
            {
                var entity = await _context.Customers
                             .SingleOrDefaultAsync(c => c.CustomerId == request.Id, cancellationToken);

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

                entity.Name = request.Name;

                await _context.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
コード例 #4
0
        public async Task <Unit> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Customers
                         .FindAsync(request.Id);

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

            var hasVisualizations = _context.Visualizations.Any(o => o.CustomerId == entity.CustomerId);

            if (hasVisualizations)
            {
                throw new DeleteFailureException(nameof(Customer), request.Id, "There are existing visualizations associated with this customer.");
            }

            _context.Customers.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }