public ActionResult EditBrand(int id) { AppDbContext dbContext = new AppDbContext(); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == id); return(View(brand)); }
public void Delete(int brandModelId) { BrandModels brandModel = GetById(brandModelId); _brandModelRepository.Delete(brandModel); _unitOfWork.Complete(); }
public ActionResult Index(int?page) { AppDbContext dbContext = new AppDbContext(); ListProductViewModel listProduct = new ListProductViewModel(); listProduct.Brands = dbContext.getBrands.ToList(); List <ProductModels> products = dbContext.getProducts.ToList(); List <GetProductViewModel> getProducts = new List <GetProductViewModel>(); /*Get products details*/ foreach (var item in products) { ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == item.ModelId); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId); CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId); GetProductViewModel getProduct = new GetProductViewModel { Id = item.Id, Name = model.Name, Brand = brand.Brand, Category = category.Category, Price = item.Price, Color = item.Color, Storage = item.Storage, Processor = item.Processor, Memory = item.Memory, Display = item.Display, Details = item.Details, CreatedBy = item.CreatedBy, CreatedDate = item.CreatedDate }; getProduct.Photos = dbContext.getProductPhotos(item.Id).ToList(); getProducts.Add(getProduct); } /*Search product and sort by date*/ listProduct.Products = getProducts.OrderByDescending(p => p.CreatedDate).ToList().ToPagedList(page ?? 1, 20); /*Get the number of each product in a brand*/ listProduct.EachProductsOfBrands = new List <int>(); foreach (var brand in listProduct.Brands) { List <ProductModels> productsOfBrands = dbContext.getProductBrands(brand.BrandId).ToList(); int numberOfBrand = productsOfBrands.Count; listProduct.EachProductsOfBrands.Add(numberOfBrand); } /*Create Index view with product details*/ return(View(listProduct)); }
//------------------------------------------------------ //数据库字段 englisgName 改为englishName // //--------------------------------------------------------- #region Add public virtual void Add(BrandModels brand) { string sqlStr = "insert into m_brands values (@chineseName,@englishName,@logo)"; SqlParameter[] parm = { new SqlParameter("@chineseName", brand.chineseName), new SqlParameter("@englishName", brand.englishName), new SqlParameter("@logo", brand.logo) }; var result = SqlSeverProvider.ExecuteNonQuery(sqlStr); }
public ActionResult Products(int id) { ViewBag.ModelId = id; AppDbContext dbContext = new AppDbContext(); List <ProductModels> products = dbContext.getProductModels(id).ToList(); if (products.Count > 0) { ProductViewModel productView = new ProductViewModel(); ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == id); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId); CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId); foreach (var item in products.ToList()) { ProductModels product = new ProductModels { Id = item.Id, Price = item.Price, Color = item.Color, Storage = item.Storage, Processor = item.Processor, Memory = item.Memory, Display = item.Display, Details = item.Details, BrandId = item.BrandId, ModelId = item.ModelId, CreatedBy = item.CreatedBy, CreatedDate = item.CreatedDate }; List <ProductPhoto> photos = dbContext.getProductPhotos(item.Id).ToList(); product.Photos = photos; productView.Name = model.Name; productView.Brand = brand.Brand; productView.Category = category.Category; productView.Products = products; } return(View(productView)); } ViewBag.Result = "No product created"; return(View()); }
public ActionResult AddBrand(BrandModels model) { if (ModelState.IsValid) { AppDbContext dbContext = new AppDbContext(); BrandModels brand = new BrandModels { Brand = model.Brand, BrandDetails = model.BrandDetails + "_create by " + User.Identity.Name + " on " + DateTime.Now }; dbContext.addBrand(brand); return(RedirectToAction("Index")); } return(View()); }
public virtual BrandModels GetById(int id) { string sqlStr = string.Format("select * from m_brands where b_brandsId={0}", id); BrandModels brand = null; DataTable dt = SqlSeverProvider.ExecuteQuery(sqlStr); foreach (DataRow row in dt.Rows) { brand = new BrandModels { brandsID = (int)row["b_brandsId"], chineseName = (string)(row["b_chineseName"] == System.DBNull.Value ?string.Empty : row["b_chineseName"]), englishName = (string)(row["b_englisgName"] == System.DBNull.Value ? string.Empty : row["b_englisgName"]), logo = (string)(row["b_logo"] == System.DBNull.Value ? string.Empty : row["b_logo"]) }; } return(brand); }
public ActionResult DeleteProduct(int id) { AppDbContext dbContext = new AppDbContext(); ProductModels product = dbContext.getProducts.SingleOrDefault(p => p.Id == id); ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == product.ModelId); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId); CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId); EditProductModels toBeDeleted = new EditProductModels { Id = Guid.NewGuid(), Model = model.Name, Brand = brand.Brand, Category = category.Category, Price = product.Price, Color = product.Color, Storage = product.Storage, Processor = product.Processor, Memory = product.Memory, Display = product.Display, Details = product.Details, EditedBy = User.Identity.Name, EditedDate = DateTime.Now, ProductId = product.Id }; dbContext.addDeletedProduct(toBeDeleted); List <ProductPhoto> photos = dbContext.getProductPhotos(id).ToList(); if (photos.Count > 0) { foreach (var photo in photos) { System.IO.File.Delete(photo.Src); dbContext.deleteProductPhoto(photo.Id); } } dbContext.deleteProduct(id); return(RedirectToAction("Index")); }
public virtual List <BrandModels> GetAll() { string sqlStr = "select * from m_brands"; List <BrandModels> brands = new List <BrandModels>(); DataTable dt = SqlSeverProvider.ExecuteQuery(sqlStr); BrandModels brand = null; foreach (DataRow row in dt.Rows) { brand = new BrandModels { brandsID = (int)row["b_brandsId"], chineseName = (string)(row["b_chineseName"] == System.DBNull.Value ? string.Empty : row["b_chineseName"]), englishName = (string)(row["b_englishName"] == System.DBNull.Value ? string.Empty : row["b_englishName"]), logo = (string)row["b_logo"] }; brands.Add(brand); } return(brands); }
/*Pendding*/ /*Adding multiple product*/ public PartialViewResult AddProducts(int id) { AppDbContext dbContext = new AppDbContext(); ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == id); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId); CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId); AddProductViewModel addProductView = new AddProductViewModel { Brand = brand.Brand, BrandId = brand.BrandId, Model = model.Name, ModelId = model.Id, Category = category.Category, CategoryId = category.CategoryId }; return(PartialView("_AddProducts", addProductView)); }
public ActionResult EditBrand(FormCollection collection) { if (ModelState.IsValid) { int Id = Convert.ToInt32(collection["BrandId"]); string Brand = collection["Brand"]; string Details = collection["BrandDetails"]; AppDbContext dbContext = new AppDbContext(); BrandModels brand = new BrandModels { BrandId = Id, Brand = Brand, BrandDetails = Details }; dbContext.updateBrand(brand); return(RedirectToAction("Brands")); } return(View(collection)); }
public ActionResult Search(string search, int?page) { AppDbContext dbContext = new AppDbContext(); List <ProductModels> products = dbContext.getProducts.ToList(); List <GetProductViewModel> searchProducts = new List <GetProductViewModel>(); foreach (var item in products) { ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == item.ModelId); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId); CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId); List <ProductPhoto> photos = dbContext.getProductPhotos(item.Id).ToList(); GetProductViewModel searchProduct = new GetProductViewModel { Id = item.Id, Name = model.Name, Brand = brand.Brand, Category = category.Category, Color = item.Color, Price = item.Price, Processor = item.Processor, Memory = item.Memory, Storage = item.Storage, Display = item.Display, Details = item.Details, Photos = photos }; searchProducts.Add(searchProduct); } var result = searchProducts.Where(p => p.Name.StartsWith(search ?? "", StringComparison.OrdinalIgnoreCase)).ToList().ToPagedList(page ?? 1, 50); return(View(result)); }
public ActionResult FilterByPrice(int min, int max) { AppDbContext dbContext = new AppDbContext(); List <GetProductViewModel> viewModels = new List <GetProductViewModel>(); List <ProductModels> products = dbContext.getProductByPrice(min, max).OrderByDescending(p => p.CreatedDate).ToList(); foreach (var product in products) { ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == product.ModelId); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == product.BrandId); CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == product.CategoryId); GetProductViewModel getProduct = new GetProductViewModel { Id = product.Id, Name = model.Name, Brand = brand.Brand, Category = category.Category, Price = product.Price, Color = product.Color, Storage = product.Storage, Processor = product.Processor, Memory = product.Memory, Display = product.Display, Details = product.Details, CreatedBy = product.CreatedBy, CreatedDate = product.CreatedDate }; getProduct.Photos = dbContext.getProductPhotos(product.Id).ToList(); viewModels.Add(getProduct); } return(Json(viewModels, JsonRequestBehavior.AllowGet)); }
public virtual void Update(int id, BrandModels newBrand) { string sqlStr = string.Format("update m_brands set b_chineseName={0},b_englisgName={1},b_logo={3}", newBrand.chineseName, newBrand.englishName, newBrand.logo); var result = SqlSeverProvider.ExecuteNonQuery(sqlStr); }
public void Update(BrandModels brandModel) { _brandModelRepository.Update(brandModel); _unitOfWork.Complete(); }
public void Save(BrandModels brandModel) { _brandModelRepository.Add(brandModel); _unitOfWork.Complete(); }
public ActionResult EditProduct(List <HttpPostedFileBase> Photo, EditProductViewModel product) { if (ModelState.IsValid) { AppDbContext dbContext = new AppDbContext(); ModelModels model = dbContext.getModels.SingleOrDefault(m => m.Id == product.ModelId); ProductModels oldProduct = dbContext.getProducts.SingleOrDefault(p => p.Id == product.Id); BrandModels brand = dbContext.getBrands.SingleOrDefault(b => b.BrandId == model.BrandId); CategoryModels category = dbContext.getCategories.SingleOrDefault(c => c.CategoryId == model.CategoryId); EditProductModels editProduct = new EditProductModels { Id = Guid.NewGuid(), Model = model.Name, Brand = brand.Brand, Category = category.Category, Price = oldProduct.Price, Color = oldProduct.Color, Storage = oldProduct.Storage, Processor = oldProduct.Processor, Memory = oldProduct.Memory, Display = oldProduct.Display, Details = oldProduct.Details, EditedBy = User.Identity.Name, EditedDate = DateTime.Now, ProductId = product.Id }; dbContext.addEditedProduct(editProduct); ProductModels productModels = new ProductModels(); productModels.Id = product.Id; productModels.Price = product.Price; productModels.Color = product.Color; productModels.Storage = product.Storage; productModels.Processor = product.Processor; productModels.Memory = product.Memory; productModels.Display = product.Display; productModels.Details = product.Details; productModels.ModelId = product.ModelId; productModels.Photos = new List <ProductPhoto>(); List <ProductPhoto> photos = new List <ProductPhoto>(); foreach (var photo in Photo) { if (photo != null) { List <ProductPhoto> Photos = dbContext.getProductPhotos(product.Id).ToList(); if (Photos.Count > 0) { foreach (var item in Photos) { System.IO.File.Delete(item.Src); dbContext.deleteProductPhoto(item.Id); } } var fileName = Path.GetFileName(photo.FileName); var path = "/Public/Products/" + model.Name; var photoUrl = Server.MapPath(path); var photoTitle = Path.GetFileNameWithoutExtension(photo.FileName); var uniqName = Guid.NewGuid().ToString() + "_" + fileName; if (!Directory.Exists(photoUrl)) { Directory.CreateDirectory(photoUrl); } var photoPath = Path.Combine(photoUrl, uniqName); ProductPhoto newPhoto = new ProductPhoto { Path = path, Src = photoPath, Title = uniqName, ProductId = product.Id }; photos.Add(newPhoto); photo.SaveAs(photoPath); } } dbContext.addProductPhoto(photos); dbContext.updateProduct(productModels); return(RedirectToAction("Index")); } return(View(product)); }