PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Json/JsonCustomFilterList.cs

#
C# | 83 lines | 64 code | 6 blank | 13 comment | 5 complexity | 7343b0ac4a2e97ff5e416d59a288253f MD5 | raw file
 1namespace BlogEngine.Core.Json
 2{
 3    using System;
 4    using System.Data;
 5    using System.Collections.Generic;
 6    using Web.Extensions;
 7
 8    ///<summary>
 9    /// Handle json-friendly list of custom filters
10    ///</summary>
11    public class JsonCustomFilterList
12    {
13        static protected ExtensionSettings CustomFilters;
14
15        /// <summary>
16        /// Get list of custom filters
17        /// </summary>
18        /// <returns>List of filters</returns>
19        public static List<JsonCustomFilter> GetCustomFilters()
20        {
21            var filterList = new List<JsonCustomFilter>();
22            try
23            {
24                CustomFilters = ExtensionManager.GetSettings("MetaExtension", "BeCustomFilters");
25                DataTable dt = CustomFilters.GetDataTable();
26
27                foreach (DataRow row in dt.Rows)
28                {
29                    var f = new JsonCustomFilter
30                    {
31                        Name = row["Name"].ToString(),
32                        FullName = row["FullName"].ToString(),
33                        Checked = int.Parse(row["Checked"].ToString()),
34                        Spam = int.Parse(row["Cought"].ToString()),
35                        Mistakes = int.Parse(row["Reported"].ToString())
36                    };
37
38                    var ext = ExtensionManager.GetExtension(f.Name);
39                    f.Enabled = ext == null ? true : ext.Enabled;
40
41                    filterList.Add(f);
42                }
43            }
44            catch (Exception ex)
45            {
46                Utils.Log("JsonCustomFilterList.GetCustomFilters(): " + ex.Message);
47            }
48            return filterList;
49        }
50        
51        /// <summary>
52        /// Reset counters for custom filter
53        /// </summary>
54        /// <param name="filterName">Filter name</param>
55        /// <returns>Json response</returns>
56        public static JsonResponse ResetCounters(string filterName)
57        {
58            try
59            {
60                if (!string.IsNullOrEmpty(filterName))
61                {
62                    // reset statistics for this filter
63                    for (int i = 0; i < CustomFilters.Parameters[0].Values.Count; i++)
64                    {
65                        if (CustomFilters.Parameters[1].Values[i] == filterName)
66                        {
67                            CustomFilters.Parameters[2].Values[i] = "0";
68                            CustomFilters.Parameters[3].Values[i] = "0";
69                            CustomFilters.Parameters[4].Values[i] = "0";
70                        }
71                    }
72                    ExtensionManager.SaveSettings("MetaExtension", CustomFilters);
73                }
74                return new JsonResponse() { Success = true, Message = string.Format("Counters for {0} reset", filterName) };
75            }
76            catch (Exception ex)
77            {
78                Utils.Log(string.Format("JsonCustomFilterList.ResetCounters: {0}", ex.Message));
79                return new JsonResponse() { Message = "Error resetting counters" };
80            }
81        }
82    }
83}