-
Notifications
You must be signed in to change notification settings - Fork 577
Expand file tree
/
Copy pathCustomFieldRepository.cs
More file actions
169 lines (151 loc) · 5.42 KB
/
Copy pathCustomFieldRepository.cs
File metadata and controls
169 lines (151 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using BlogEngine.Core.Data.Contracts;
using BlogEngine.Core.Data.Models;
using BlogEngine.Core.Data.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
namespace BlogEngine.Core.Data
{
/// <summary>
/// Custom field repository
/// </summary>
public class CustomFieldRepository : ICustomFieldRepository
{
#region Intreface
/// <summary>
/// List of items
/// </summary>
/// <param name="filter">Filter expression</param>
/// <returns>List of posts</returns>
public IEnumerable<CustomField> Find(string filter)
{
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
throw new UnauthorizedAccessException();
if (string.IsNullOrEmpty(filter))
filter = "1 == 1";
var items = new List<CustomField>();
var query = CustomFieldsParser.CachedFields.AsQueryable().Where(filter);
foreach (var item in query)
items.Add(item);
return items;
}
/// <summary>
/// Single item
/// </summary>
/// <param name="type">Type (theme, post etc)</param>
/// <param name="id">Id, for example "standard" for a theme</param>
/// <param name="key">Key in the key/value for a field</param>
/// <returns></returns>
public CustomField FindById(string type, string id, string key)
{
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
throw new UnauthorizedAccessException();
var cf = Find("").FirstOrDefault(f => f.CustomType == type && f.ObjectId == id && f.Key == key);
return cf;
}
/// <summary>
/// Add new item
/// </summary>
/// <param name="item">Custom field</param>
/// <returns>Added field</returns>
public CustomField Add(CustomField item)
{
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
throw new UnauthorizedAccessException();
try
{
//if (AlreadyExists(item))
// throw new ApplicationException("Custom field already exists");
Providers.BlogService.SaveCustomField(item);
CustomFieldsParser.ClearCache();
return item;
}
catch (Exception ex)
{
Utils.Log("Error adding custom field", ex);
throw;
}
}
/// <summary>
/// Update item
/// </summary>
/// <param name="item">Custom field</param>
/// <returns>True on success</returns>
public bool Update(CustomField item)
{
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
throw new UnauthorizedAccessException();
try
{
item.BlogId = Blog.CurrentInstance.Id;
Providers.BlogService.SaveCustomField(item);
CustomFieldsParser.ClearCache();
return true;
}
catch (Exception ex)
{
Utils.Log("Error updaging custom field", ex);
return false;
}
}
/// <summary>
/// Delete item
/// </summary>
/// <param name="type">Type (theme, post etc)</param>
/// <param name="id">Id, for example "standard" for a theme</param>
/// <param name="key">Key in the key/value for a field</param>
/// <returns>True on success</returns>
public bool Remove(string type, string id, string key)
{
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
throw new UnauthorizedAccessException();
try
{
var item = new CustomField
{
CustomType = type,
BlogId = Blog.CurrentInstance.BlogId,
ObjectId = id,
Key = key
};
Providers.BlogService.DeleteCustomField(item);
CustomFieldsParser.ClearCache();
return true;
}
catch (Exception ex)
{
Utils.Log("Error updaging custom field", ex);
return false;
}
}
/// <summary>
/// Crear fields for object type (post, theme etc)
/// </summary>
/// <param name="type">Custom type</param>
/// <param name="id">Object id</param>
public void ClearCustomFields(string type, string id)
{
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
throw new UnauthorizedAccessException();
try
{
Providers.BlogService.ClearCustomFields(Blog.CurrentInstance.BlogId.ToString(), type, id);
CustomFieldsParser.ClearCache();
}
catch (Exception ex)
{
Utils.Log("Error updaging custom field", ex);
}
}
bool AlreadyExists(CustomField item)
{
var field = CustomFieldsParser.CachedFields.FirstOrDefault(f => f.BlogId == item.BlogId
&& f.CustomType == item.CustomType
&& f.ObjectId == item.ObjectId
&& f.Key == item.Key);
return field != null;
}
#endregion
}
}