public DTOProduct GetByIdDto(int id) { Product product = _context.Products.FirstOrDefault(x => x.IsDeleted == false && x.Id == id); if (product == null) { return(null); } List <ProductAttribute> productAttributes = _context.ProductAttributes.Include(x => x.Attribute).Where(x => x.ProductId == product.Id).ToList(); List <ProductShelf> productShelves = _context.ProductShelves.Where(x => x.ProductId == product.Id) .Include(x => x.Shelf) .ThenInclude(x => x.Department) .ThenInclude(x => x.Inventory) .ToList(); ProductPrice productPrice = _context.ProductPrices.FirstOrDefault(x => x.ProductId == product.Id && x.IsDeleted == false); CategoryBrand categoryBrand = _context.CategoriesBrands.Include(x => x.Category) .Include(x => x.Brand) .FirstOrDefault(x => x.Id == product.CategoryBrandId); DTOProduct model = _mapper.Map <DTOProduct>(product); model.Attributes = _mapper.Map <List <DTOProductAttribute> >(productAttributes); model.ProductPrice = _mapper.Map <DTOProductPrice>(productPrice); model.ProductShelves = _mapper.Map <List <DTOProductShelf> >(productShelves); model.CategoryBrand = _mapper.Map <DTOCategoryBrand>(categoryBrand); return(model); }
public async Task <IActionResult> Add(BrandModel model) { int langId = HttpContext.GetLanguage("adminLangId"); if (ModelState.IsValid && Checker.CheckList(model.Categories)) { #region Create Brand and Some CategoryBrands Brand brand = new Brand(); brand.Name = model.BrandName; db.Brands.Add(brand); for (var i = 0; i < model.Categories.Count; i++) { CategoryBrand categoryBrand = new CategoryBrand(); categoryBrand.BrandId = brand.Id; categoryBrand.CategoryId = model.Categories[i]; db.CategoryBrands.Add(categoryBrand); } await db.SaveChangesAsync(); #endregion return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError("", "Make sure that you fill al required blanks"); return(RedirectToAction(nameof(Add))); } }
public void Edit(DTOBrand dtoBrand) { Brand brand = _mapper.Map <Brand>(dtoBrand); _context.Update(brand); List <int> itemsToAdd = dtoBrand.SelectedCategoryBrandsIds; List <int> itemsToDelete = new List <int>(); List <CategoryBrand> categoryBrands = _context.CategoriesBrands.Where(x => x.BrandId == brand.Id).ToList(); foreach (var item in categoryBrands) { bool exist = itemsToAdd.Any(x => x == item.CategoryId); if (!exist) { itemsToDelete.Add(item.Id); } else { itemsToAdd.Remove(item.CategoryId); } } if (itemsToDelete != null && itemsToDelete.Any()) { foreach (var item in itemsToDelete) { _unitOfWork.CategoriesBrands.Delete(item); //TODO : Ubaciti provjeru da li je povezan sa nekim postojecim PROIZVODOM } } if (itemsToAdd != null && itemsToAdd.Any()) { CategoryBrand categoryBrand = null; foreach (var item in itemsToAdd) { categoryBrand = new CategoryBrand { BrandId = brand.Id, CategoryId = item }; _context.CategoriesBrands.Add(categoryBrand); } } _context.SaveChanges(); }
public IActionResult AddCB([FromForm] IFormCollection data) { string customMessage = ""; //create Student object CategoryBrand newCB = new CategoryBrand(); //Passing data into newStudent newCB.catId = Convert.ToInt32(data["catId"]); newCB.brandId = Convert.ToInt32(data["brandId"]); try { //Add newStudent object Database.Add(newCB); //Save changess Database.SaveChanges(); } catch (Exception ex) { if (ex.InnerException != null) { if (ex.InnerException.Message.Contains("CategoryBrand_cbId_UniqueConstraint") == true) { customMessage = "Unable to save Category Brand record due to another record having the same id: " + data["cbId"]; return(BadRequest(new { message = customMessage })); } else { customMessage = "Unable to save Category Brand record due to internal server errors"; return(BadRequest(new { message = customMessage })); } } else { Debug.WriteLine("THE EXCEPTION IS " + ex); customMessage = "Unable to save Category Brand record due to internal server errors"; return(BadRequest(new { message = customMessage })); } } return(Ok(new { message = "Saved Brand Record" })); }
public void Add(DTOCategory dtoCategory) { Category category = _mapper.Map <Category>(dtoCategory); _context.Add(category); _context.SaveChanges(); CategoryBrand categoryBrand = null; foreach (var cb in dtoCategory.SelectedCategoryBrandsIds) { categoryBrand = new CategoryBrand { CategoryId = category.Id, BrandId = cb }; _context.CategoriesBrands.Add(categoryBrand); } _context.SaveChanges(); }
public void Add(DTOBrand dtoBrand) { Brand brand = _mapper.Map <Brand>(dtoBrand); _context.Add(brand); _context.SaveChanges(); if (dtoBrand.SelectedCategoryBrandsIds != null && dtoBrand.SelectedCategoryBrandsIds.Any()) { CategoryBrand categoryBrand = null; foreach (var cb in dtoBrand.SelectedCategoryBrandsIds) { categoryBrand = new CategoryBrand { BrandId = brand.Id, CategoryId = cb, }; _context.CategoriesBrands.Add(categoryBrand); } } _context.SaveChanges(); }
/// <summary> /// 获得所有的品牌 /// 如果一级分类下没有任何品牌则此一级分类不显示 /// 如果一级分类下的所有品牌都没有LOGO图片则不显示 /// </summary> /// <returns></returns> public static List <CategoryBrand> QueryCategoryBrandInfos() { var categories = QueryCategoryInfos(); var cates = QueryCategoryInfosForHomePage(); var brands = ProductDA.GetAllBrands(); var result = new List <CategoryBrand>(); categories.ForEach(p => { if (p.CategoryType == CategoryType.TabStore) { var item = new CategoryBrand(); item.Category = p; item.BrandInfos = GetBrands(p.CategoryID, cates, brands); if (item.BrandInfos.Count > 0) { result.Add(item); } } }); return(result); }
public string SaveBrand(BrandInfo model) { if (string.IsNullOrWhiteSpace(model.BrandName)) { return(MessageContent.Submit_Params_InvalidError); } if (string.IsNullOrWhiteSpace(model.BrandCode)) { return(MessageContent.Submit_Params_InvalidError); } Guid categoryId = Guid.Empty; Guid.TryParse(model.CategoryId.ToString(), out categoryId); Guid gId = Guid.Empty; Guid.TryParse(model.Id.ToString(), out gId); model.Id = gId; Guid parentId = Guid.Empty; Guid.TryParse(model.ParentId.ToString(), out parentId); model.ParentId = parentId; model.LastUpdatedDate = DateTime.Now; CategoryBrand cbBll = new CategoryBrand(); Brand bll = new Brand(); int effect = -1; if (!gId.Equals(Guid.Empty)) { effect = bll.Update(model); CategoryBrandInfo cbModel = cbBll.GetModel(gId); if (!categoryId.Equals(Guid.Empty)) { if (cbModel == null) { cbModel = new CategoryBrandInfo(); cbModel.BrandId = gId; cbModel.CategoryId = categoryId; cbBll.Insert(cbModel); } else { if (!cbModel.CategoryId.Equals(categoryId)) { cbModel.CategoryId = categoryId; cbBll.Update(cbModel); } } } else { if (cbModel != null) { cbBll.Delete(gId, cbModel.CategoryId); } } } else { if (!categoryId.Equals(Guid.Empty)) { Guid brandId = bll.InsertAndGetId(model); CategoryBrandInfo cbModel = new CategoryBrandInfo(); cbModel.BrandId = brandId; cbModel.CategoryId = categoryId; cbBll.Insert(cbModel); effect = 1; } else { effect = bll.Insert(model); } } if (effect == 110) { return(MessageContent.Submit_Exist); } if (effect > 0) { return("1"); } else { return(MessageContent.Submit_Error); } }
public async Task <IActionResult> Edit(BrandModel model) { if (ModelState.IsValid && Checker.CheckList(model.Categories)) { #region Brand and CategoryBrand edit Brand brand = await db.Brands.Where(b => b.Id == model.BrandId).FirstOrDefaultAsync(); brand.Name = model.BrandName; List <CategoryBrand> categoryBrands = await db.CategoryBrands.Where(cb => cb.BrandId == model.BrandId).ToListAsync(); if (model.Categories.Count > categoryBrands.Count) { for (int i = 0; i < model.Categories.Count; i++) { if (i + 1 > categoryBrands.Count) { CategoryBrand categoryBrand = new CategoryBrand(); categoryBrand.BrandId = brand.Id; categoryBrand.CategoryId = model.Categories[i]; db.CategoryBrands.Add(categoryBrand); } else { categoryBrands[i].CategoryId = model.Categories[i]; } } } else if (model.Categories.Count == categoryBrands.Count) { for (int i = 0; i < model.Categories.Count; i++) { categoryBrands[i].CategoryId = model.Categories[i]; } } else { for (int i = 0; i < categoryBrands.Count; i++) { if (i + 1 <= model.Categories.Count) { categoryBrands[i].CategoryId = model.Categories[i]; } else { CategoryBrand categoryBrand = db.CategoryBrands .Where(cb => cb.CategoryId == categoryBrands[i].CategoryId) .FirstOrDefault(); db.CategoryBrands.Remove(categoryBrand); } } } #endregion await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError("", "Fill all required blanks"); return(RedirectToAction(nameof(Edit))); } }
public IActionResult Post([FromForm] IFormCollection data) { string customMessage = ""; //create Student object Brand newBrand = new Brand() { categoryBrands = new List <CategoryBrand>() }; //Create a new category brand object CategoryBrand categoryBrand; //Passing data into newStudent newBrand.brandName = data["brandName"]; newBrand.createdAt = DateTime.Now; newBrand.updatedAt = DateTime.Now; Debug.WriteLine("What's in selected cats : " + data["selectedCats"]); //For each catId in array add foreach (string catId in data["selectedCats"]) { categoryBrand = new CategoryBrand(); categoryBrand.catId = Convert.ToInt32(catId); newBrand.categoryBrands.Add(categoryBrand); } try { //Add newStudent object Database.Add(newBrand); //Save changess Database.SaveChanges(); Debug.WriteLine("CHANGES TO BRANDS TABLE SAVED"); } catch (Exception ex) { if (ex.InnerException != null) { if (ex.InnerException.Message.Contains("Brand_brandId_UniqueConstraint") == true) { customMessage = "Unable to save Brand record due to another record having the same id: " + data["brandId"]; return(BadRequest(new { message = customMessage })); } else { customMessage = "Unable to save Brand record due to internal server errors"; return(BadRequest(new { message = customMessage })); } } else { Debug.WriteLine("THE EXCEPTION IS " + ex); } } return(Ok(new { message = "Saved Brand Record" })); }