/// <summary> /// 添加一个大分类 /// </summary> public BigCategory AddBigCategory(String name, CategoryStateType state) { BigCategory bigCate = (from item in Dc.BigCategory where item.Name.Equals(name) select item).FirstOrDefault(); if (bigCate == null) { bigCate = Dc.BigCategory.Create(); bigCate.Name = name; bigCate.State = state; bigCate.ID = SqlDataHelper.CreateID(); bigCate.CreateTime = DateTime.Now; Dc.BigCategory.Add(bigCate); try { Dc.SaveChanges(); } catch (Exception) { throw new DbSaveException(); } } else { throw new ExistedCategoryNameException(); } return bigCate; }
/// <summary> /// 添加一个小分类 /// </summary> public SubCategory AddSubCategory(String id, String name, CategoryStateType state) { SubCategory subC = new SubCategory(); subC.BigCategoryID = id; subC.Name = name; int count = (from item in Dc.BigCategory where item.ID.Equals(subC.BigCategoryID) select item).Count(); if (count > 0) { count = (from item in Dc.SubCategory where item.Name.Equals(subC.Name) && item.BigCategoryID.Equals(id) select item).Count(); if (count <= 0) { subC.ID = SqlDataHelper.CreateID(); subC.NewsTotal = 0; subC.State = Model.Enums.CategoryStateType.Enabled; Dc.SubCategory.Add(subC); try { Dc.SaveChanges(); } catch (InvalidOperationException) { throw new DbSaveException(); } } else { throw new ExistedCategoryNameException(); } } else { throw new DbNotFoundException(); } return subC; }
/// <summary> /// 修改子类 /// </summary> public void EditSubCategory(String id, String name, String parentid, CategoryStateType state) { SubCategory subC = GetSubCategoryByID(id); if (subC == null) { throw new DbNotFoundException(); } if (parentid != null) { BigCategory bigCate = Dc.BigCategory.Find(parentid); if (bigCate == null) { throw new DbNotFoundException(); } } if (name != null) { subC.Name = name; } if (parentid != null) { subC.BigCategoryID = parentid; } subC.State = state; try { Dc.SaveChanges(); } catch (Exception) { throw new DbSaveException(); } }
/// <summary> /// 修改父类 /// </summary> public void EditBigCategory(String id, String name, CategoryStateType state) { BigCategory bigC = GetBigCategoryByID(id); if (bigC == null) { throw new DbNotFoundException(); } int count = (from item in Dc.BigCategory where !item.ID.Equals(id) && item.Name.Equals(name) select item).Count(); if (count > 0) { throw new ExistedCategoryNameException(); } else { bigC.State = state; if (name != null) { bigC.Name = name; } try { Dc.SaveChanges(); } catch (InvalidOperationException) { throw new DbSaveException(); } } }