/// <summary> /// Prepare new model for creating category view /// </summary> /// <param name="parentName">Parent Name of Category in what we want to create new</param> /// <returns>CategoryView model prepared for enter all needed data</returns> public static CategoryView CreateNewModel(string parentName) { var parId = GetIdFromName(parentName); var model = new CategoryView() { ParentId = parId, ParentName = parentName, Level = GetCurrentLevelFromParentId(parId), HasSubCategories = false }; App.Mapper.MapLinksForCategory(ref model); return model; }
public ActionResult EditSomeCategory(CategoryView model) { if (ModelState.IsValid) { if (model.ImgFile != null && model.ImgFile.ContentLength > 0) { CategoryManager.SaveNewImage(model); } CategoryManager.UpdateCategory(model); return RedirectToAction("EditCategories", new RouteValueDictionary( new { parentId = model.ParentId })); } return View(model); }
/// <summary> /// Save All checked Category links from Add or Edit Forms by Category Id /// </summary> /// <param name="model">CategoryView model with links and Category Id</param> private static void SaveProperties(CategoryView model) { if (model != null && model.Id != 0) { foreach (var link in model.Properties) { if (!link.IsChanged && !link.Checked) { var linkCat = App.Rep.Select<LinkCategories>() .FirstOrDefault(lc => lc.Category_Cat_Id == model.Id && lc.Link_Link_Id == link.Id); if (linkCat != null) App.Rep.Delete<LinkCategories>(linkCat, false); } else if (link.Checked && link.IsChanged) { App.Rep.Insert<LinkCategories>(new LinkCategories() { Category_Cat_Id = model.Id, Link_Link_Id = link.Id }, false); } } App.Rep.Save(); } }
/// <summary> /// If parent Category has some products these products will be moved into new Category /// </summary> /// <param name="model">New model with could take new </param> internal static void MoveProductsIfExist(CategoryView model) { if (model != null && model.Id != 0 && model.ParentId != DefaultParentCategoryId) { App.Rep.Select<Product>() .Where(p => p.Pr_Cat_Id == model.ParentId) .ForEach(p => ProductManager.SetNewCategoryIdAndUpdate(p, model.Id)); App.Rep.Save(); } }
/// <summary> /// Update category in DB with your changed model from View /// </summary> /// <param name="model">CategoryView model with was changed by user</param> public static void UpdateCategory(CategoryView model) { //todo save links if (model.ImagePath != null && model.ImagePath.Length != 0) { var img = App.Rep.Select<Image>() .FirstOrDefault(i => i.Img_Path == model.ImagePath); if (img != null) { model.ImageId = img.Img_Id; } else { model.ImageId = null; } } SaveProperties(model); App.Rep.Update<Category>((Category) App.Mapper.Map(model, typeof(CategoryView), typeof(Category)), true); }
/// <summary> /// Save Selected Image /// </summary> /// <param name="model">Our model from View</param> public static void SaveNewImage(CategoryView model) { if (model.ImgFile != null && model.ImgFile.ContentLength > 0) { var path = Res.ImagesDirectory + Res.CategoryImagesDirectory; var fileName = Guid.NewGuid().ToString() + model.ImgFile.FileName; model.ImagePath = path + fileName; byte[] data; using (Stream inputStream = model.ImgFile.InputStream) { MemoryStream memoryStream = inputStream as MemoryStream; if (memoryStream == null) { memoryStream = new MemoryStream(); inputStream.CopyTo(memoryStream); } data = memoryStream.ToArray(); } if (!ImageManager.ImageManager.UploadFile(data, fileName, path)) { model.ImagePath = null; model.ImageId = null; model.ImgFile = null; return; } App.Rep.Insert<Image> ((Image)App.Mapper.Map(model, typeof(CategoryView), typeof(Image)), true); } }
/// <summary> /// This function save our new category into DB /// </summary> /// <param name="model">Category with will be saved</param> public static void SaveNewCategory(CategoryView model) { if (model.ParentId != DefaultParentCategoryId) { ChangeParentCategory(model.ParentId); } if (model.ImagePath != null && model.ImagePath.Length != 0) { model.ImageId = App.Rep.Select<Image>() .FirstOrDefault(i => i.Img_Path == model.ImagePath).Img_Id; } var cat = App.Rep.Insert<Category>((Category) App.Mapper.Map(model, typeof(CategoryView), typeof(Category)), true); if (cat != null) { model.Id = cat.Cat_Id; MoveProductsIfExist(model); SaveProperties(model); } }
/// <summary> /// Get all links from DB and links with already added to our Category /// </summary> /// <param name="model">CategoryView model where we need to add our links</param> public void MapLinksForCategory(ref CategoryView model) { if (model != null) { var catId = model.Id; var linkCats = App.Rep.Select<LinkCategories>() .Where(lc => lc.Category_Cat_Id == catId).ToList(); var myProp = new List<Link>(); if (linkCats != null && linkCats.Count != 0) { foreach (var linkCat in linkCats) { var link = App.Rep.Select<Link>() .FirstOrDefault(l => l.Link_Id == linkCat.Link_Link_Id); if (link != null) { myProp.Add(link); } } } var allLinks = App.Rep.Select<Link>().ToList(); if (allLinks != null && allLinks.Count != 0) { foreach (var link in allLinks) { var linkView = (LinkView)App.Mapper.Map(link, typeof(Link), typeof(LinkView)); if (myProp.Any(l => l.Link_Id == linkView.Id)) { linkView.Checked = true; } else { linkView.IsChanged = true; } model.Properties.Add(linkView); } } model.Properties.Sort(); } }
/// <summary> /// Get Image details or current category /// </summary> /// <param name="model">category with needed to get image details</param> public void MapImageForCategory(ref CategoryView model) { if (model != null && model.ImageId != null && model.ImageId.Value != 0) { var imgId = model.ImageId.Value; var img = App.Rep.Select<Image>() .FirstOrDefault(i => i.Img_Id == imgId); if (img != null) { model.ImagePath = img.Img_Path; } else { model.ImageId = null; } } }