Exemple #1
0
            public async Task <int> Handle(EditProductCommand request, CancellationToken cancellationToken)
            {
                if (!request.ProductId.HasValue || request.ProductId == 0)
                {
                    var ToAdd = _mapper.Map <Products>(request);
                    await _context.Products.AddAsync(ToAdd);

                    await _context.SaveChangeAsyc();

                    return(ToAdd.ProductId);
                }
                else
                {
                    var toEdit = await _context.Products.SingleOrDefaultAsync(x => x.ProductId == request.ProductId);

                    if (toEdit == null)
                    {
                        throw new ValidationException();
                    }

                    _mapper.Map(request, toEdit);
                    toEdit.ProductName     = request.ProductName;
                    toEdit.SupplierId      = request.SupplierId;
                    toEdit.CategoryId      = request.CategoryId;
                    toEdit.QuantityPerUnit = request.QuantityPerUnit;
                    toEdit.UnitPrice       = request.UnitPrice;
                    toEdit.UnitsInStock    = request.UnitsInStock;
                    toEdit.UnitsOnOrder    = request.UnitsOnOrder;
                    toEdit.ReorderLevel    = request.ReorderLevel;
                    toEdit.Discontinued    = request.Discontinued == true;
                    await _context.SaveChangeAsyc();

                    return(toEdit.ProductId);
                }
            }
Exemple #2
0
            public async Task <int> Handle(EditOrderDetailCommand request, CancellationToken cancellationToken)
            {
                var toEdit = await _context.OrderDetails.Include(x => x.Order).SingleOrDefaultAsync
                                 (x => x.ProductId == request.ProductId && x.OrderId == request.OrderId);

                //better than doing it  at validator level, which would require multiple db requests
                if (toEdit == null)
                {
                    throw new DomainBadRequestException();
                }

                else
                {
                    toEdit.Order.CustomerId     = request.CustomerId;
                    toEdit.Order.EmployeeId     = request.EmployeeId.Value;
                    toEdit.Order.OrderDate      = request.OrderDate.Value;
                    toEdit.Order.RequiredDate   = request.RequiredDate.Value;
                    toEdit.Order.ShippedDate    = request.ShippedDate.Value;
                    toEdit.Order.ShipAddress    = request.ShipAddress;
                    toEdit.Order.ShipPostalCode = request.ShipPostalCode;
                    toEdit.Quantity             = request.Quantity.Value;

                    return(await _context.SaveChangeAsyc());
                }
            }
Exemple #3
0
            public async Task <int> Handle(DeleteOrderDetailCommand request, CancellationToken cancellationToken)
            {
                var toDelete = await _context.OrderDetails.SingleOrDefaultAsync
                                   (x => x.ProductId == request.ProductID && x.OrderId == request.OrderID);

                if (toDelete == null)
                {
                    throw new DomainBadRequestException();
                }

                else
                {
                    _context.OrderDetails.Remove(toDelete);
                    return(await _context.SaveChangeAsyc());
                }
            }
            public async Task <int> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
            {
                if (request.Ids.Length == 0)
                {
                    throw new DomainBadRequestException();
                }
                else
                {
                    var toRemove = await _context.Products.Where(x => request.Ids.Contains(x.ProductId)).ToListAsync();

                    if (toRemove.Count != request.Ids.Length)
                    {
                        throw new DomainBadRequestException();
                    }
                    else
                    {
                        _context.Products.RemoveRange(toRemove);
                        return(await _context.SaveChangeAsyc());
                    }
                }
            }