forked from BlogEngine/BlogEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlogRepository.cs
More file actions
169 lines (150 loc) · 6.1 KB
/
BlogRepository.cs
File metadata and controls
169 lines (150 loc) · 6.1 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web.Hosting;
namespace BlogEngine.Core.Data
{
/// <summary>
/// Blog repository
/// </summary>
public class BlogRepository : IBlogRepository
{
/// <summary>
/// Get blog list
/// </summary>
/// <param name="take">Number of items to take</param>
/// <param name="skip">Number of items to skip</param>
/// <param name="filter">Filter expression</param>
/// <param name="order">Order expression</param>
/// <returns>List of blogs</returns>
public IEnumerable<Models.Blog> Find(int take = 10, int skip = 0, string filter = "", string order = "")
{
// sub-blogs not allowed to see other blogs
if (!(Blog.CurrentInstance.IsPrimary && Security.IsAdministrator))
throw new UnauthorizedAccessException();
if (take == 0) take = Blog.Blogs.Count;
if (string.IsNullOrEmpty(filter)) filter = "1==1";
if (string.IsNullOrEmpty(order)) order = "Name";
var items = new List<Models.Blog>();
var query = Blog.Blogs.AsQueryable().Where(filter);
foreach (var item in query.OrderBy(order).Skip(skip).Take(take))
items.Add(ToJson((Blog)item));
return items;
}
/// <summary>
/// Find blog by id
/// </summary>
/// <param name="id">Id</param>
/// <returns>Blog</returns>
public Models.Blog FindById(Guid id)
{
// sub-blogs not allowed to see other blogs
if (!(Blog.CurrentInstance.IsPrimary && Security.IsAdministrator))
throw new UnauthorizedAccessException();
var blog = Blog.Blogs.FirstOrDefault(b => b.Id == id);
return ToJson(blog);
}
/// <summary>
/// Add new blog
/// </summary>
/// <param name="item">Blog item</param>
/// <returns>Saved blog with new ID</returns>
public Models.Blog Add(BlogItem item)
{
// has to be on primary blog and be an admin
// or blog allows create new on self registration
if (!(Blog.CurrentInstance.IsPrimary && (Security.IsAdministrator || BlogSettings.Instance.CreateBlogOnSelfRegistration)))
throw new UnauthorizedAccessException();
string message;
if (!BlogGenerator.ValidateProperties(item.Name, item.UserName, item.Email, out message))
throw new ApplicationException(message);
var coreBlog = BlogGenerator.CreateNewBlog(item.Name, item.UserName, item.Email, item.Password, out message);
if (coreBlog == null || !string.IsNullOrWhiteSpace(message))
throw new ApplicationException("Failed to create the new blog.");
return ToJson(coreBlog);
}
/// <summary>
/// Update blog
/// </summary>
/// <param name="blog">Blog to update</param>
/// <returns>True on success</returns>
public bool Update(Models.Blog blog)
{
// sub-blogs not allowed to see other blogs
if (!(Blog.CurrentInstance.IsPrimary && Security.IsAdministrator))
throw new UnauthorizedAccessException();
try
{
var coreBlog = Blog.Blogs.FirstOrDefault(b => b.Id == blog.Id);
return Save(coreBlog, blog);
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Delete item
/// </summary>
/// <param name="id">ID</param>
/// <returns>True on success</returns>
public bool Remove(Guid id)
{
// sub-blogs not allowed to see other blogs
if (!(Blog.CurrentInstance.IsPrimary && Security.IsAdministrator))
throw new UnauthorizedAccessException();
try
{
var blog = Blog.Blogs.FirstOrDefault(b => b.Id == id);
blog.Delete();
blog.Save();
return true;
}
catch (Exception)
{
return false;
}
}
#region Private methods
static bool Save(Blog coreBlog, BlogEngine.Core.Data.Models.Blog jsonBlog)
{
coreBlog.Id = jsonBlog.Id;
coreBlog.Name = jsonBlog.Name;
coreBlog.StorageContainerName = jsonBlog.StorageContainerName;
coreBlog.Hostname = jsonBlog.Hostname;
coreBlog.IsAnyTextBeforeHostnameAccepted = jsonBlog.IsAnyTextBeforeHostnameAccepted;
coreBlog.VirtualPath = jsonBlog.VirtualPath;
coreBlog.IsActive = jsonBlog.IsActive;
coreBlog.IsSiteAggregation = jsonBlog.IsSiteAggregation;
coreBlog.IsPrimary = jsonBlog.IsPrimary;
//coreBlog.StorageLocation = PhysicalStorageLocation = HostingEnvironment.MapPath(blog.StorageLocation);
coreBlog.Save();
return true;
}
static BlogEngine.Core.Data.Models.Blog ToJson(Blog blog)
{
var jb = new BlogEngine.Core.Data.Models.Blog
{
Id = blog.Id,
Name = blog.Name,
StorageContainerName = blog.StorageContainerName,
Hostname = blog.Hostname,
IsAnyTextBeforeHostnameAccepted = blog.IsAnyTextBeforeHostnameAccepted,
VirtualPath = blog.VirtualPath,
IsActive = blog.IsActive,
IsSiteAggregation = blog.IsSiteAggregation,
IsPrimary = blog.IsPrimary,
RelativeWebRoot = blog.RelativeWebRoot,
AbsoluteWebRoot = blog.AbsoluteWebRoot,
PhysicalStorageLocation = HostingEnvironment.MapPath(blog.StorageLocation),
CanUserEdit = blog.CanUserEdit,
CanUserDelete = blog.CanUserDelete
};
return jb;
}
#endregion
}
}