/// <summary> /// Save to collection (add or replace) /// </summary> /// <param name="setting">Setting</param> public void Save(QuickSetting setting) { var idx = Settings.FindIndex(s => s.Author == setting.Author && s.SettingName == setting.SettingName); if(idx < 0) { Settings.Add(setting); } else { Settings[idx] = setting; } }
/// <summary> /// Save settings /// </summary> /// <param name="category">Default post category id</param> /// <param name="tags">Default tags</param> public void SaveSettings(string category, string tags) { var cat = new QuickSetting { Author = author, SettingName = "category", SettingValue = category }; var tag = new QuickSetting { Author = author, SettingName = "tags", SettingValue = tags }; // save to collection this.Settings.Save(cat); this.Settings.Save(tag); // save to storage BlogService.SaveQuickSetting(cat); BlogService.SaveQuickSetting(tag); // reset cache Blog.CurrentInstance.Cache.Remove(cacheKeySettings); }
/// <summary> /// Save quick setting /// </summary> /// <param name="setting">Quick setting</param> public override void SaveQuickSetting(QuickSetting setting) { var fileName = Path.Combine(this.Folder, settingsFileName); var settings = FillQuickSettings(setting.Author) ?? new List<QuickSetting>(); int idx = -1; for (int index = 0; index < settings.Count; index++) { var s = settings[index]; if (s.Author == setting.Author && s.SettingName == setting.SettingName) { idx = index; break; } } if (idx >= 0) settings.RemoveAt(idx); settings.Add(setting); using (var writer = new XmlTextWriter(fileName, Encoding.UTF8)) { writer.Formatting = Formatting.Indented; writer.Indentation = 4; writer.WriteStartDocument(true); writer.WriteStartElement("QuickSettings"); foreach (var s in settings) { writer.WriteStartElement("item"); writer.WriteAttributeString("blogid", Blog.CurrentInstance.Id.ToString()); writer.WriteAttributeString("author", s.Author); writer.WriteAttributeString("name", s.SettingName); writer.WriteAttributeString("value", s.SettingValue); writer.WriteEndElement(); } writer.WriteEndElement(); } }
/// <summary> /// Fill quick settings /// </summary> /// <param name="userId">User ID</param> /// <returns>List of user settings</returns> public override List<QuickSetting> FillQuickSettings(string userId) { var settings = new List<QuickSetting>(); using (var conn = this.CreateConnection()) { if (conn.HasConnection) { using (var cmd = conn.CreateTextCommand(string.Format("SELECT SettingName, SettingValue FROM {0}QuickSettings where UserName = '******' and BlogId = '{2}'", tablePrefix, userId, Blog.CurrentInstance.Id.ToString()))) { using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var s = new QuickSetting() { SettingName = rdr.GetString(0), SettingValue = rdr.GetString(1) }; settings.Add(s); } } } } } return settings; }
/// <summary> /// Save quick setting /// </summary> /// <param name="setting">Quick setting</param> public override void SaveQuickSetting(QuickSetting setting) { using (var conn = this.CreateConnection()) { if (conn.HasConnection) { var sqlQuery = string.Format("select count(*) from {0}QuickSettings where BlogId = {1}blogid and UserName = {1}username and SettingName = {1}settingname", this.tablePrefix, this.parmPrefix); object cnt; using (var cmd = conn.CreateTextCommand(sqlQuery)) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("settingname"), setting.SettingName)); p.Add(conn.CreateParameter(FormatParamName("blogid"), Blog.CurrentInstance.Id.ToString())); p.Add(conn.CreateParameter(FormatParamName("username"), setting.Author)); cnt = cmd.ExecuteScalar(); } if (int.Parse(cnt.ToString()) > 0) sqlQuery = string.Format("update {0}QuickSettings set SettingValue = {1}settingvalue where BlogId = {1}blogid and UserName = {1}username and SettingName = {1}settingname", this.tablePrefix, this.parmPrefix); else sqlQuery = string.Format("insert into {0}QuickSettings (BlogId, UserName, SettingName, SettingValue) values ({1}blogid, {1}username, {1}settingname, {1}settingvalue)", this.tablePrefix, this.parmPrefix); using (var cmd = conn.CreateTextCommand(sqlQuery)) { var p = cmd.Parameters; p.Add(conn.CreateParameter(FormatParamName("blogid"), Blog.CurrentInstance.Id.ToString())); p.Add(conn.CreateParameter(FormatParamName("username"), setting.Author)); p.Add(conn.CreateParameter(FormatParamName("settingname"), setting.SettingName)); p.Add(conn.CreateParameter(FormatParamName("settingvalue"), setting.SettingValue)); cmd.ExecuteNonQuery(); } } } }