/// <summary> /// The update. /// </summary> /// <param name="oldCatID"> /// The old cat id. /// </param> /// <param name="category"> /// The category. /// </param> public static void Update(string oldCatID, Category category) { if (oldCatID != category.CatID) { foreach (PostInfo post in BlogComp.GetPosts()) { BlogComp.ChangeCategory(post.FileID, oldCatID, category.CatID); PostComp.ChangeCategory(post.FileID, oldCatID, category.CatID); } } ConfigHelper.DataContext.CategoryData.Update(oldCatID, category); }
/// <summary> /// The insert. /// </summary> /// <param name="catID"> /// The cat id. /// </param> /// <param name="catName"> /// The cat name. /// </param> public static void Insert(string catID, string catName) { Category category = new Category(); category.CatID = catID; category.Name = catName; category.Count = 0; category.Order = 0; ConfigHelper.DataContext.CategoryData.Insert(category); }
public ActionResult Edit(string id, CatModel model) { if (this.ModelState.IsValid) { if (!string.IsNullOrEmpty(id)) { Category oldCategory = CategoryComp.GetCategory(id); // get old category if (oldCategory.Name != model.Name) { if (!CategoryComp.IsUniqueName(model.Name)) { this.ModelState.AddModelError("Name", "分类名称不能为空"); } } if (id != model.CatID) { if (!CategoryComp.IsUniqueId(model.CatID)) { this.ModelState.AddModelError("CatID", "分类ID不能为空"); } } if (this.ModelState.IsValid) { Category cat2 = new Category(); cat2.CatID = model.CatID; cat2.Name = model.Name; cat2.Count = oldCategory.Count; cat2.Order = oldCategory.Order; CategoryComp.Update(oldCategory.CatID, cat2); } } } if (this.ModelState.IsValid) { this.TempData["Message"] = "分类信息已更新"; return this.RedirectToAction("Manage"); } else { this.ViewData.Model = model; return this.View(); } }
/// <summary> /// Updates the category node given the old category id /// Called by category management /// </summary> /// <param name="id"> /// Old ID /// </param> /// <param name="category"> /// The Category /// </param> public void Update(string id, Category category) { XElement root = null; try { root = XElement.Load(this._path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } var qry = from elem in root.Elements("Category") where elem.Attribute("ID").Value == id select elem; if (qry.Count<XElement>() == 0) { string msg = string.Format(NO_CAT_ERROR, id); Logger.Log(msg); return; } XElement catElem = qry.First<XElement>(); try { catElem.SetAttributeValue("ID", category.CatID); catElem.SetAttributeValue("Name", category.Name); catElem.SetAttributeValue("Order", category.Order); // does not update count } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } root.Save(this._path); }
/// <summary> /// Insert a category node into XML /// Called when a new category is added in category management /// </summary> /// <param name="category"> /// The Category /// </param> public void Insert(Category category) { XElement root = null; try { root = XElement.Load(this._path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); throw new ApplicationException(NO_FILE_ERROR, ex); } var qry = from elem in root.Elements("Category") where elem.Attribute("ID").Value == category.CatID select elem; if (qry.Count<XElement>() > 0) { string msg = string.Format(DUP_CAT_ERROR, category.CatID); Logger.Log(msg); throw new ApplicationException(msg); } try { XElement catElem = new XElement( "Category", new XAttribute("ID", category.CatID), new XAttribute("Name", category.Name), new XAttribute("Count", category.Count), new XAttribute("Order", category.Order)); int count = root.Elements("Category").Count<XElement>(); // Usually order is zero if (category.Order == 0 || category.Order > count) { category.Order = count + 1; root.Add(catElem); } else { var qry2 = from elem in root.Elements("Category") where (int)elem.Attribute("Order") > category.Order select elem; if (qry2.Count<XElement>() > 0) { // This is a rare case (does not happen) XElement refElem = qry2.First<XElement>(); refElem.AddBeforeSelf(catElem); } } root.Save(this._path); } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } }
/// <summary> /// Gets list of categories /// Called by Category user control /// Called by Category Management page /// </summary> /// <returns>List of Category Object</returns> public List<Category> GetCategories() { XElement root = null; List<Category> categories = new List<Category>(); try { root = XElement.Load(this._path); } catch (Exception ex) { Logger.Log(NO_FILE_ERROR, ex); return categories; } try { foreach (XElement catElem in root.Elements("Category")) { Category cat = new Category(); cat.CatID = catElem.Attribute("ID").Value; cat.Name = catElem.Attribute("Name").Value; try { cat.Count = (int)catElem.Attribute("Count"); cat.Order = (int)catElem.Attribute("Order"); } catch (Exception ex) { cat.Count = 0; Logger.Log(FORMAT_ERROR, ex); } categories.Add(cat); } } catch (Exception ex) { Logger.Log(XML_FORMAT_ERROR, ex); throw new ApplicationException(XML_FORMAT_ERROR, ex); } return categories; }