public async Task <Unit> Handle(DeleteEmployeeCommand request, CancellationToken cancellationToken) { var entity = await _context.Employees .FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Employee), request.Id); } if (entity.UserId == _currentUser.UserId) { throw new BadRequestException("Employees cannot delete their own account."); } if (entity.UserId != null) { await _userManager.DeleteUserAsync(entity.UserId); } // TODO: Update this logic, this will only work if the employee has no associated territories or orders.Emp _context.Employees.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
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), key: request.Id); } var hasOrders = _context.Orders.Any(o => o.CustomerId == entity.CustomerId); if (hasOrders) { // TODO: Add functional test for this behaviour. throw new DeleteFailureException(nameof(Customer), key: request.Id, message: "There are existing orders associated with this customer."); } _context.Customers.Remove(entity: entity); await _context.SaveChangesAsync(cancellationToken : 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: entity); await _context.SaveChangesAsync(cancellationToken : cancellationToken); await _mediator.Publish(new CustomerCreated { CustomerId = entity.CustomerId }, cancellationToken : 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; _context.Customers.Update(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(CreateProductCommand request, CancellationToken cancellationToken) { var productEntity = _mapper.Map <Product>(request); await _dbContext.Products.AddAsync(productEntity, cancellationToken); await _dbContext.SaveChangesAsync(cancellationToken); return(productEntity.ProductId); }
public async Task <Unit> Handle(DeleteProductCommand request, CancellationToken cancellationToken) { var entity = await _dbContext.Products.FindAsync(request.ProductId); if (entity == null) { throw new NotFoundException(nameof(Product), request.ProductId); } _dbContext.Products.Remove(entity); await _dbContext.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(DeleteCategoryCommand request, CancellationToken cancellationToken) { var entity = await _context.Categories .FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Category), request.Id); } _context.Categories.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CreateCustomerCommand request, CancellationToken cancellationToken) { var entity = new Domain.Entities.Customer { CustomerId = request.Id, ContactName = request.ContactName }; _context.Customers.Add(entity); await _context.SaveChangesAsync(cancellationToken); await _mediator.Publish(new CustomerCreated { CustomerId = entity.CustomerId }, cancellationToken); return(Unit.Value); }
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: entity); await _context.SaveChangesAsync(cancellationToken : cancellationToken); return(entity.ProductId); }
public async Task <Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { var entity = await _dbContext.Categories.FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Product), request.Id); } entity.CategoryName = request.Name ?? entity.CategoryName; entity.Description = request.Description ?? entity.Description; entity.Picture = request.Picture ?? entity.Picture; await _dbContext.SaveChangesAsync(cancellationToken); return(Unit.Value); }
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 <int> Handle(Command command, CancellationToken token) { Supplier entity; if (command.Id.HasValue) { entity = await _db.Suppliers.FindAsync(command.Id.Value); } else { entity = new Supplier(); await _db.Suppliers.AddAsync(entity, token); } entity.CompanyName = command.CompanyName; await _db.SaveChangesAsync(token); return(entity.SupplierId); }
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 <int> Handle(UpsertCategoryCommand request, CancellationToken cancellationToken) { Category entity; if (request.Id.HasValue) { entity = await _context.Categories.FindAsync(request.Id.Value); } else { entity = new Category(); _context.Categories.Add(entity); } entity.CategoryName = request.Name; entity.Description = request.Description; entity.Picture = request.Picture; await _context.SaveChangesAsync(cancellationToken); return(entity.CategoryId); }
public async Task<int> Handle(UpsertEmployeeCommand request, CancellationToken cancellationToken) { Employee entity; if (request.Id.HasValue) { entity = await _context.Employees.FindAsync(request.Id.Value); } else { entity = new Employee(); _context.Employees.Add(entity); } entity.TitleOfCourtesy = request.Title; entity.FirstName = request.FirstName; entity.LastName = request.LastName; entity.BirthDate = request.BirthDate; entity.Address = request.Address; entity.City = request.City; entity.Region = request.Region; entity.PostalCode = request.PostalCode; entity.Country = request.Country; entity.HomePhone = request.HomePhone; entity.Title = request.Position; entity.Extension = request.Extension; entity.HireDate = request.HireDate; entity.Notes = request.Notes; entity.Photo = request.Photo; entity.ReportsTo = request.ManagerId; await _context.SaveChangesAsync(cancellationToken); return entity.EmployeeId; }
public async Task <Unit> Handle(Command command, CancellationToken token) { var entity = await _db.Customers .FindAsync(command.Id); // if (entity == null) // { // throw new NotFoundException(nameof(Customer), command.Id); // } var hasOrders = _db.Orders.Any(o => o.CustomerId == entity.CustomerId); if (hasOrders) { // throw new DeleteFailureException(nameof(Customer), command.Id, // "There are existing orders associated with this customer."); } _db.Customers.Remove(entity); await _db.SaveChangesAsync(token); return(Unit.Value); }