public async Task <Result> Handle(CreateProductCommand request, CancellationToken cancellationToken)
            {
                try
                {
                    var supplier = await _context.Supplier.FindAsync(request.SupplierId);

                    var category = _context.ProductCategory.Single(c => c.Id == request.CategoryId);
                    var product  = _mapper.Map <Product>(request);
                    product.Category = category;

                    supplier.AddProduct(product);
                    await _context.SaveChangesAsync();

                    return(new Result(request.SupplierId));
                }
                catch (DbUpdateException ex)
                {
                    if (ex.IsDuplicateKeyError())
                    {
                        throw new PurchaseOrderTrackerException("A duplicate product already exists");
                    }

                    throw;
                }
            }
            protected override async Task Handle(EditStatusCommand request, CancellationToken cancellationToken)
            {
                var shipment = await _context.Shipment
                               .Include(s => s.PurchaseOrders)
                               .SingleAsync(s => s.Id == request.ShipmentId);

                switch (request.UpdatedStatus)
                {
                case ShipmentStatus.Trigger.AwaitingShipping:
                    shipment.UpdateStatus(ShipmentStatus.Trigger.AwaitingShipping);
                    break;

                case ShipmentStatus.Trigger.Shipped:
                    shipment.UpdateStatus(ShipmentStatus.Trigger.Shipped);
                    break;

                case ShipmentStatus.Trigger.Delivered:
                    shipment.UpdateStatus(ShipmentStatus.Trigger.Delivered);
                    break;

                default:
                    throw new PurchaseOrderTrackerException($"Unexpected update status value '{request.UpdatedStatus}'");
                }

                await _context.SaveChangesAsync();
            }
Exemple #3
0
            public async Task <Result> Handle(EditCommand request, CancellationToken cancellationToken)
            {
                var order = await _context.PurchaseOrder
                            .Include(p => p.Supplier)
                            .Include(p => p.Status)
                            .Include(p => p.Shipment)
                            .ThenInclude(s => s.Status)
                            .SingleAsync(p => p.Id == request.PurchaseOrderId);

                order.OrderNo = request.OrderNo;
                await UpdateShipmentIfChanged(request, order);
                await UpdateSupplierIfChanged(request, order);

                await _context.SaveChangesAsync();

                var result    = _mapper.Map <Domain.Models.PurchaseOrderAggregate.PurchaseOrder, Result>(order);
                var suppliers = await _context.Supplier.ToListAsync();

                var shipments = (await _context.Shipment.ToListAsync()).Where(s => s.CanBeAssignedToPurchaseOrder).ToList();

                result.SupplierOptions = suppliers.ToDictionary(s => s.Id, c => c.Name.Value);
                result.ShipmentOptions = shipments.ToDictionary(s => s.Id, c => c.TrackingId);

                return(result);
            }
            protected override async Task Handle(DeleteCommand request, CancellationToken cancellationToken)
            {
                var shipment = await _context.Shipment.SingleAsync(s => s.Id == request.ShipmentId);

                ThrowExceptionIfCannotBeDeleted(shipment);
                _context.Shipment.Remove(shipment);

                await _context.SaveChangesAsync();
            }
            public async Task <Result> Handle(EditCommand request, CancellationToken cancellationToken)
            {
                var supplier = await _context.Supplier.FindAsync(request.SupplierId);

                _mapper.Map(request, supplier);
                await _context.SaveChangesAsync();

                return(new Result(_mapper.Map <Result.SupplierViewModel>(supplier)));
            }
            public async Task <Result> Handle(EditCommand request, CancellationToken cancellationToken)
            {
                var shipment = await _context.Shipment.SingleAsync(s => s.Id == request.ShipmentId);

                _mapper.Map(request, shipment);
                await _context.SaveChangesAsync();

                return(_mapper.Map <Result>(shipment));
            }
Exemple #7
0
            protected override async Task Handle(EditProductCategoryCommand request, CancellationToken cancellationToken)
            {
                var supplier = await _context.Supplier.FindAsync(request.SupplierId);

                var category = await _context.Entry(supplier).Collection(s => s.ProductCategories).Query()
                               .Where(c => c.Id == request.CategoryId).SingleAsync();

                category.Name = request.Name;
                await _context.SaveChangesAsync();
            }
            protected override async Task Handle(DeleteCommand request, CancellationToken cancellationToken)
            {
                var supplier = await _context.Supplier.SingleAsync(s => s.Id == request.SupplierId);

                // TODO
                // if (!supplier.CanBeDeleted)
                //    throw new PurchaseOrderTrackerException("Supplier cannot be deleted");

                _context.Supplier.Remove(supplier);
                await _context.SaveChangesAsync();
            }
            public async Task <Result> Handle(CreateCommand request, CancellationToken cancellationToken)
            {
                var supplier = await _context.Supplier.FindAsync(request.SupplierId);

                var purchaseOrder = new Domain.Models.PurchaseOrderAggregate.PurchaseOrder(request.OrderNo, supplier);

                _context.PurchaseOrder.Add(purchaseOrder);
                await _context.SaveChangesAsync();

                return(new Result(purchaseOrder.Id));
            }
Exemple #10
0
            public async Task <Result> Handle(DeleteLineItemCommand request, CancellationToken cancellationToken)
            {
                var purchaseOrder = await _context.PurchaseOrder.Include(o => o.LineItems).SingleAsync(o => o.Id == request.PurchaseOrderId);

                var lineItem = purchaseOrder.LineItems.Single(li => li.Id == request.LineItemId);

                purchaseOrder.RemoveLineItem(lineItem);
                await _context.SaveChangesAsync();

                return(new Result(request.PurchaseOrderId));
            }
Exemple #11
0
            protected override async Task Handle(DeleteCommand request, CancellationToken cancellationToken)
            {
                var order = await _context.PurchaseOrder.Include(p => p.Status).SingleAsync(p => p.Id == request.PurchaseOrderId);

                if (!order.CanBeDeleted)
                {
                    throw new PurchaseOrderTrackerException("Shipment cannot be deleted");
                }

                _context.PurchaseOrder.Remove(order);
                await _context.SaveChangesAsync();
            }
            public async Task <Result> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
            {
                var supplier = await _context.Supplier.FindAsync(request.SupplierId);

                var product = await _context.Entry(supplier).Collection(s => s.Products).Query()
                              .Where(p => p.Id == request.ProductId).SingleAsync();

                supplier.RemoveProduct(product);
                await _context.SaveChangesAsync();

                return(new Result(request.SupplierId));
            }
Exemple #13
0
            public async Task <Result> Handle(EditProductCommand request, CancellationToken cancellationToken)
            {
                var supplier = await _context.Supplier.FindAsync(request.SupplierId);

                var product = await _context.Entry(supplier).Collection(s => s.Products).Query()
                              .Where(p => p.Id == request.ProductId).SingleAsync();

                product.ProductCode = request.ProdCode;
                product.Name        = request.ProdName;
                product.Price       = request.Price;
                product.Category    = await DetermineNewCategory(_context, supplier, product.Category, request.CategoryId);

                await _context.SaveChangesAsync();

                return(new Result(request.SupplierId));
            }
Exemple #14
0
            public async Task <Result> Handle(CreateLineItemCommand request, CancellationToken cancellationToken)
            {
                var purchaseOrder = await _context.PurchaseOrder
                                    .Include(p => p.Supplier)
                                    .SingleAsync(o => o.Id == request.PurchaseOrderId);

                // TODO: throw exception is product is null?
                var product = await _context.Product.FindAsync(request.ProductId);

                var lineItem = new PurchaseOrderLine(product, request.PurchasePrice.Value, request.PurchaseQty);

                purchaseOrder.AddLineItem(lineItem);
                await _context.SaveChangesAsync();

                return(new Result(request.PurchaseOrderId));
            }
Exemple #15
0
            public async Task <Result> Handle(EditLineItemCommand request, CancellationToken cancellationToken)
            {
                var purchaseOrder = await _context.PurchaseOrder
                                    .Include(p => p.Supplier)
                                    .Include(p => p.LineItems)
                                    .ThenInclude(li => li.Product)
                                    .SingleAsync(o => o.Id == request.PurchaseOrderId);

                var lineItem = purchaseOrder.LineItems.Single(li => li.Id == request.LineItemId);

                await UpdateProductIfChanged(lineItem, request);

                lineItem.PurchasePrice = request.PurchasePrice;
                lineItem.PurchaseQty   = request.PurchaseQty;
                await _context.SaveChangesAsync();

                return(new Result(request.PurchaseOrderId));
            }
            protected override async Task Handle(CreateProductCategoryCommand request, CancellationToken cancellationToken)
            {
                try
                {
                    var supplier = await _context.Supplier.FindAsync(request.SupplierId);

                    supplier.AddCategory(new ProductCategory(request.Name));

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException ex)
                {
                    if (ex.IsDuplicateKeyError())
                    {
                        throw new PurchaseOrderTrackerException("A duplicate product category already exists");
                    }

                    throw;
                }
            }
Exemple #17
0
            public async Task <Result> Handle(CreateCommand request, CancellationToken cancellationToken)
            {
                try
                {
                    var shipment = _mapper.Map <Domain.Models.ShipmentAggregate.Shipment>(request);
                    _context.Shipment.Add(shipment);
                    await _context.SaveChangesAsync();

                    return(new Result(shipment.Id));
                }
                catch (DbUpdateException ex)
                {
                    if (ex.IsDuplicateKeyError())
                    {
                        // TODO: throw a new type of exception which is handled in the request pipeline
                        // with a generic "duplicate record" error message
                        throw new PurchaseOrderTrackerException("A duplicate shipment already exists");
                    }

                    throw;
                }
            }
            public async Task <Result> Handle(CreateCommand request, CancellationToken cancellationToken)
            {
                try
                {
                    var supplier = _mapper.Map <Domain.Models.SupplierAggregate.Supplier>(request);
                    _context.Supplier.Add(supplier);
                    await _context.SaveChangesAsync();

                    await _mediator.Publish(new Notification(supplier.Id));

                    return(new Result(supplier.Id));
                }
                catch (DbUpdateException ex)
                {
                    if (ex.IsDuplicateKeyError())
                    {
                        throw new PurchaseOrderTrackerException($"A supplier with the name {request.Name} already exists");
                    }

                    throw;
                }
            }
            protected override async Task Handle(EditStatusCommand request, CancellationToken cancellationToken)
            {
                var order = await _context.PurchaseOrder.Include(p => p.Status).SingleAsync(p => p.Id == request.PurchaseOrderId);

                switch (request.UpdatedStatus)
                {
                case PurchaseOrderStatus.Trigger.PendingApproval:
                    order.UpdateStatus(PurchaseOrderStatus.Trigger.PendingApproval);
                    break;

                case PurchaseOrderStatus.Trigger.Approved:
                    order.UpdateStatus(PurchaseOrderStatus.Trigger.Approved);
                    break;

                case PurchaseOrderStatus.Trigger.Cancelled:
                    order.UpdateStatus(PurchaseOrderStatus.Trigger.Cancelled);
                    break;

                default:
                    throw new PurchaseOrderTrackerException($"Unexpected update status value '{request.UpdatedStatus}'");
                }

                await _context.SaveChangesAsync();
            }