public async Task <CustomersListViewModel> Handle(GetCustomersListQuery request, CancellationToken cancellationToken) { return(new CustomersListViewModel { Customers = await _context.Set <Customer>().ProjectTo <CustomerLookupModel>(_mapper.ConfigurationProvider).ToListAsync(cancellationToken) }); }
public async Task <CustomerDetailModel> Handle(GetCustomerDetailQuery request, CancellationToken cancellationToken) { var entity = await _context.Set <Customer>() .FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Customer), request.Id); } return(CustomerDetailModel.Create(entity)); }
public async Task <Unit> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken) { var entity = await _context.Set <Customer>() .FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Customer), request.Id); } var hasOrders = _context.Set <Order>().Any(o => o.CustomerId == entity.CustomerId); if (hasOrders) { // TODO: Add functional test for this behaviour. throw new DeleteFailureException(nameof(Customer), request.Id, "There are existing orders associated with this customer."); } _context.Set <Customer>().Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <CustomersListViewModel> Handle(FindCustomersByName request, CancellationToken cancellationToken) { return(new CustomersListViewModel { Customers = await _context.Set <Customer>() .Where(c => c.CompanyName.Contains(request.Name)) //.Where(c => c.CompanyName.StartsWith(request.Name)) //.Where(c => c.CompanyName.Equals(request.Name)) .Select(c => new CustomerLookupModel() { Name = c.CompanyName, Id = c.CustomerId }) .ToListAsync(cancellationToken) }); }
public async Task <IEnumerable <CustomersMostPurchasedViewModel> > Handle(GetCustomersMostPurchasedProductsQuery request, CancellationToken cancellationToken) { return(await _context.Set <CustomersMostPurchasedProducts>() .OrderByDescending(c => c.QuantityPurchased) .Skip(request.PageIndex * request.PageSize) .Take(request.PageSize) .Select(r => new CustomersMostPurchasedViewModel { CompanyName = r.CompanyName, CustomerId = r.CustomerID, ProductId = r.ProductID, ProductName = r.ProductName, QuantityPurchased = r.QuantityPurchased }) .ToListAsync(cancellationToken)); }
public async Task <IEnumerable <CustomerOrdersModel> > Handle(GetCustomerOrders request, CancellationToken cancellationToken) { var efQuery = _context.Set <Customer>().Where(c => true); efQuery = ApplyFilters(efQuery, request); efQuery = ApplyPaging(efQuery, request); return(await SimpleSelect(efQuery)); // return await ToListSelect(efQuery); // return await SelectAnon(efQuery); // return await ReusableProjection(efQuery); // return await ExtensionMethod(efQuery); // return await AutoMapper(efQuery); // Note: Show Linqkit predicate builder // http://www.albahari.com/nutshell/predicatebuilder.aspx }
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.Set <Customer>().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.Set <Customer>() .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); }