public async Task <int> Handle(CreateProductCommand request, CancellationToken cancellationToken) { var entity = new Product { ProductName = request.ProductName, CategoryId = request.CategoryId, SupplierId = request.SupplierId, UnitPrice = request.UnitPrice, Discontinued = request.Discontinued }; _context.Products.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(entity.ProductId); }
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.CategoryId = request.CategoryId; entity.SupplierId = request.SupplierId; entity.UnitPrice = request.UnitPrice; entity.Discontinued = request.Discontinued; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
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); } var hasOrders = _context.OrderDetails.Any(od => od.ProductId == entity.ProductId); if (hasOrders) { // TODO: Add functional test for this behaviour. throw new DeleteFailureException(nameof(Product), request.Id, "There are existing orders associated with this product."); } _context.Products.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CreateCustomerCommand request, CancellationToken cancellationToken) { var entity = new Customer { CustomerId = request.Id, Address = request.Address, City = request.City, CompanyName = request.CompanyName, ContactName = request.ContactName, ContactTitle = request.ContactTitle, Country = request.Country, Fax = request.Fax, Phone = request.Phone, PostalCode = request.PostalCode }; _context.Customers.Add(entity); await _context.SaveChangesAsync(cancellationToken); await _mediator.Publish(new CustomerCreated { CustomerId = entity.CustomerId }, cancellationToken); return(Unit.Value); }
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.Address = request.Address; entity.City = request.City; entity.CompanyName = request.CompanyName; entity.ContactName = request.ContactName; entity.ContactTitle = request.ContactTitle; entity.Country = request.Country; entity.Fax = request.Fax; entity.Phone = request.Phone; entity.PostalCode = request.PostalCode; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }