//GetMainCategoryById public MainCategory GetMainCategoryById(long maincategory_id) { DataCacheObject dco = new DataCacheObject(DataCacheType.REFERENCE, DataCacheRegions.CATEGORIES, "GETMAINCATEGORYBYID", new object[] { maincategory_id }, CachingExpirationTime.Hours_01); MainCategory mc = CacheRepository.Get(dco) as MainCategory; if (mc != null) { return(mc); } mc = dataContext.spSelect_MainCategory(maincategory_id).FirstOrDefault(); if (mc != null) { dco.Data = mc; CacheRepository.Add(dco); } return(mc); }
//DeleteMainCategory public JsonExecuteResult DeleteMainCategory(long maincat_id) { try { MainCategory mc = (maincat_id == -1) ? null : GetMainCategory(maincat_id); if (mc == null) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "This category doesn't exists")); } int count = dataContext.Auctions.Where(A => A.EventCategory.MainCategory_ID == maincat_id).Count(); if (count > 0) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "You can't delete this category, because there are auctions in the database which are using this category.")); } dataContext.MainCategories.DeleteOnSubmit(mc); GeneralRepository.SubmitChanges(dataContext); } catch (Exception ex) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message)); } return(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS)); }
//UpdateCategoryMap public JsonExecuteResult UpdateCategoryMap(long catmap_id, int maincat_id, long cat_id, string descr, string intro, int priority, bool isactive, bool isdefault) { try { MainCategory mc = GetMainCategory(maincat_id); Category c = GetCategory(cat_id); if (mc == null || c == null) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "The main category or category doesn't exist.")); } bool newCategory = false; CategoriesMap cat = (catmap_id == -1) ? null : GetCategoryMap(catmap_id); if (cat == null) { if ((cat = dataContext.CategoriesMaps.Where(CM => CM.MainCategory_ID == mc.ID && CM.Category_ID == c.ID).FirstOrDefault()) != null) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "You can't add this record, because the combination of this Main Category and Category already exists - CM# " + cat.ID.ToString())); } cat = new CategoriesMap(); dataContext.CategoriesMaps.InsertOnSubmit(cat); newCategory = true; } cat.MainCategory_ID = maincat_id; cat.Category_ID = cat_id; cat.Descr = String.IsNullOrEmpty(descr) ? mc.Title + " > " + c.Title : descr; cat.Intro = intro; cat.IsActive = isactive; cat.Priority = priority; cat.Owner_ID = AppHelper.CurrentUser.ID; cat.LastUpdate = DateTime.Now; cat.IsDefault = isdefault; if (!newCategory && !cat.IsActive) { List <CategoriesMap> Cat = dataContext.CategoriesMaps.Where(C => C.Category_ID == cat.ID).ToList(); foreach (CategoriesMap cm in Cat) { cm.IsActive = cat.IsActive; } if (dataContext.Auctions.Where(A => A.EventCategory.MainCategory_ID == cat.ID).Count() > 0) { List <string> str = new List <string>(); var res = from A in dataContext.Auctions where A.EventCategory.MainCategory_ID == cat.ID && A.Status != (byte)Consts.AuctionStatus.Locked orderby A.Status group A.Status by A.Status; foreach (var r in res) { if (r.Count() > 0) { str.Add(String.Format("{0} {1} auction{2}", r.Count(), ((Consts.AuctionStatus)r.Key).ToString().ToLower(), (r.Count() > 1) ? "s" : String.Empty)); } } if (str.Count() > 0) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "You can't set this category as unactive, because there are auctions in the database which are using this category.", str.ToArray())); } } } if (newCategory) { cat.ID = dataContext.MainCategories.Max(E => E.ID) + 1; } GeneralRepository.SubmitChanges(dataContext); } catch (Exception ex) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message)); } return(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS)); }
//UpdateMainCategory public JsonExecuteResult UpdateMainCategory(long maincat_id, string title, string description, bool isactive, int priority) { try { bool newCategory = false; MainCategory cat = (maincat_id == -1) ? null : GetMainCategory(maincat_id); if (cat == null) { cat = new MainCategory(); newCategory = true; } MainCategory tmp; if ((tmp = dataContext.MainCategories.SingleOrDefault(C => C.Title.ToLower() == title.ToLower() && C.ID != cat.ID)) != null) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "You can't add this category, because the category with the same name is already exist (MainCategory#" + tmp.ID.ToString() + ")")); } cat.Title = title; cat.Descr = description; cat.IsActive = isactive; cat.Priority = priority; cat.DateEntered = DateTime.Now; cat.Owner_ID = AppHelper.CurrentUser.ID; cat.LastUpdate = DateTime.Now; if (newCategory) { dataContext.MainCategories.InsertOnSubmit(cat); } if (!newCategory && !cat.IsActive) { List <CategoriesMap> Cat = dataContext.CategoriesMaps.Where(C => C.Category_ID == cat.ID).ToList(); foreach (CategoriesMap cm in Cat) { cm.IsActive = cat.IsActive; } if (dataContext.Auctions.Where(A => A.EventCategory.MainCategory_ID == cat.ID).Count() > 0) { List <string> str = new List <string>(); var res = from A in dataContext.Auctions where A.EventCategory.MainCategory_ID == cat.ID && A.Status != (byte)Consts.AuctionStatus.Locked orderby A.Status group A.Status by A.Status; foreach (var r in res) { if (r.Count() > 0) { str.Add(String.Format("{0} {1} auction{2}", r.Count(), ((Consts.AuctionStatus)r.Key).ToString().ToLower(), (r.Count() > 1) ? "s" : String.Empty)); } } if (str.Count() > 0) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "You can't set this category as unactive, because there are auctions in the database which are using this category.", str.ToArray())); } } } if (newCategory) { cat.ID = dataContext.MainCategories.Max(E => E.ID) + 1; } GeneralRepository.SubmitChanges(dataContext); } catch (Exception ex) { return(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message)); } return(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS)); }
//UpdateMainCategory public JsonExecuteResult UpdateMainCategory(long maincat_id, string title, string description, bool isactive, int priority) { try { bool newCategory = false; MainCategory cat = (maincat_id == -1) ? null : GetMainCategory(maincat_id); if (cat == null) { cat = new MainCategory(); newCategory = true; } MainCategory tmp; if ((tmp = dataContext.MainCategories.SingleOrDefault(C => C.Title.ToLower() == title.ToLower() && C.ID != cat.ID)) != null) return new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "You can't add this category, because the category with the same name is already exist (MainCategory#" + tmp.ID.ToString() + ")"); cat.Title = title; cat.Descr = description; cat.IsActive = isactive; cat.Priority = priority; cat.DateEntered = DateTime.Now; cat.Owner_ID = AppHelper.CurrentUser.ID; cat.LastUpdate = DateTime.Now; if (newCategory) dataContext.MainCategories.InsertOnSubmit(cat); if (!newCategory && !cat.IsActive) { List<CategoriesMap> Cat = dataContext.CategoriesMaps.Where(C => C.Category_ID == cat.ID).ToList(); foreach (CategoriesMap cm in Cat) cm.IsActive = cat.IsActive; if (dataContext.Auctions.Where(A => A.EventCategory.MainCategory_ID == cat.ID).Count() > 0) { List<string> str = new List<string>(); var res = from A in dataContext.Auctions where A.EventCategory.MainCategory_ID == cat.ID && A.Status != (byte)Consts.AuctionStatus.Locked orderby A.Status group A.Status by A.Status; foreach (var r in res) { if (r.Count() > 0) str.Add(String.Format("{0} {1} auction{2}", r.Count(), ((Consts.AuctionStatus)r.Key).ToString().ToLower(), (r.Count() > 1) ? "s" : String.Empty)); } if (str.Count() > 0) return new JsonExecuteResult(JsonExecuteResultTypes.ERROR, "You can't set this category as unactive, because there are auctions in the database which are using this category.", str.ToArray()); } } if (newCategory) cat.ID = dataContext.MainCategories.Max(E => E.ID) + 1; GeneralRepository.SubmitChanges(dataContext); } catch (Exception ex) { return new JsonExecuteResult(JsonExecuteResultTypes.ERROR, ex.Message); } return new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS); }