示例#1
0
        public ShopProductsViewModel GetShopProducts(int categoryID)
        {
            ShopProductsViewModel shopProductsViewModel = new ShopProductsViewModel();

            Shop shop = dbContext.Shops.FirstOrDefault(x => x.CategoryID == categoryID);

            if (shop != null)
            {
                shopProductsViewModel.Name = shop.Name;
            }

            IQueryable <Product> shopProducts = dbContext.Products.Where(x => x.CategoryID == categoryID);

            foreach (Product dbProduct in shopProducts)
            {
                Models.DBObjects.Product productModel = new Models.DBObjects.Product();

                productModel.Name        = dbProduct.Name;
                productModel.Description = dbProduct.Description;
                productModel.DailyPrice  = dbProduct.DailyPrice;
                productModel.Stock       = dbProduct.Stock;

                shopProductsViewModel.Products.Add(productModel);
            }
            return(shopProductsViewModel);
        }
 public void DeleteProduct(int ID)
 {
     Models.DBObjects.Product productToDelete = dbContext.Products.FirstOrDefault(x => x.ProductID == ID);
     if (productToDelete != null)
     {
         dbContext.Products.DeleteOnSubmit(productToDelete);
         dbContext.SubmitChanges();
     }
 }
 public void UpdateStock(int productID, int stock)
 {
     Models.DBObjects.Product existingProduct = dbContext.Products.FirstOrDefault(x => x.ProductID == productID);
     if (existingProduct != null)
     {
         existingProduct.Stock = stock;
         dbContext.SubmitChanges();
     }
 }
        public void UpdateProduct(ProductModel productModel)
        {
            Models.DBObjects.Product existingProduct = dbContext.Products.FirstOrDefault(x => x.ProductID == productModel.ProductID);

            if (existingProduct != null)
            {
                existingProduct.ProductID   = productModel.ProductID;
                existingProduct.CategoryID  = productModel.CategoryID;
                existingProduct.Name        = productModel.Name;
                existingProduct.Description = productModel.Description;
                existingProduct.DailyPrice  = productModel.DailyPrice;
                existingProduct.Stock       = productModel.Stock;
                existingProduct.ImageUrl    = productModel.ImageUrl;

                dbContext.SubmitChanges();
            }
        }
        public Models.DBObjects.Product MapModelToDbObject(ProductModel productModel)
        {
            Models.DBObjects.Product dbProduct = new Models.DBObjects.Product();
            if (productModel != null)
            {
                dbProduct.ProductID   = productModel.ProductID;
                dbProduct.CategoryID  = productModel.CategoryID;
                dbProduct.Name        = productModel.Name;
                dbProduct.Description = productModel.Description;
                dbProduct.DailyPrice  = productModel.DailyPrice;
                dbProduct.Stock       = productModel.Stock;
                dbProduct.ImageUrl    = productModel.ImageUrl;


                return(dbProduct);
            }

            return(null);
        }
        public ProductModel MapDbOjectToModel(Models.DBObjects.Product dbProduct)
        {
            ProductModel productModel = new ProductModel();

            if (dbProduct != null)
            {
                productModel.ProductID   = dbProduct.ProductID;
                productModel.CategoryID  = dbProduct.CategoryID;
                productModel.Name        = dbProduct.Name;
                productModel.Description = dbProduct.Description;
                productModel.DailyPrice  = dbProduct.DailyPrice;
                productModel.Stock       = dbProduct.Stock;
                productModel.ImageUrl    = dbProduct.ImageUrl;


                return(productModel);
            }

            return(null);
        }