forked from BlogEngine/BlogEngine.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagRepository.cs
More file actions
125 lines (115 loc) · 4.15 KB
/
TagRepository.cs
File metadata and controls
125 lines (115 loc) · 4.15 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
using BlogEngine.Core.Data.Contracts;
using BlogEngine.Core.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
namespace BlogEngine.Core.Data
{
/// <summary>
/// Tag repository
/// </summary>
public class TagRepository : ITagRepository
{
/// <summary>
/// Get tag list
/// </summary>
/// <param name="take">Items per page, default 10, 0 to return all</param>
/// <param name="skip">Items to skip</param>
/// <param name="postId">Post id</param>
/// <param name="order">Sort order, for example order=DateCreated,desc</param>
/// <returns>List of tags</returns>
public IEnumerable<Data.Models.TagItem> Find(int take = 10, int skip = 0, string postId = "", string order = "TagName")
{
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.ViewPublicPosts))
throw new System.UnauthorizedAccessException();
var tags = new List<TagItem>();
foreach (var p in BlogEngine.Core.Post.ApplicablePosts)
{
// if blogId passed in, only for this blog
if (!string.IsNullOrEmpty(postId))
{
Guid gId;
if (Guid.TryParse(postId, out gId))
if (p.Id != gId) continue;
}
foreach (var t in p.Tags)
{
// for every tag either add to collection
// or increment counter if already added
var tmp = tags.FirstOrDefault(tag => tag.TagName == t);
if (tmp == null)
tags.Add(new TagItem { TagName = t, TagCount = 1 });
else
tmp.TagCount++;
}
}
var query = tags.AsQueryable();
if (take == 0)
take = tags.Count;
if (string.IsNullOrEmpty(order))
order = "TagName";
return query.OrderBy(order).Skip(skip).Take(take);
}
/// <summary>
/// Updates tag
/// </summary>
/// <param name="updateFrom">Value from</param>
/// <param name="updateTo">Value to</param>
/// <returns>True on success</returns>
public bool Save(string updateFrom, string updateTo)
{
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages))
throw new System.UnauthorizedAccessException();
try
{
foreach (var p in BlogEngine.Core.Post.Posts.ToArray())
{
var tg = p.Tags.FirstOrDefault(tag => tag == updateFrom);
if (tg != null)
{
p.Tags.Remove(tg);
p.Tags.Add(updateTo);
p.DateModified = DateTime.Now;
p.Save();
}
}
return true;
}
catch (Exception ex)
{
Utils.Log($"TagRepository.Update: {ex.Message}");
return false;
}
}
/// <summary>
/// Delete tag in all posts
/// </summary>
/// <param name="id">Tag</param>
/// <returns>True on success</returns>
public bool Delete(string id)
{
if (!Security.IsAuthorizedTo(BlogEngine.Core.Rights.AccessAdminPages))
throw new System.UnauthorizedAccessException();
try
{
foreach (var p in BlogEngine.Core.Post.Posts.ToArray())
{
var tg = p.Tags.FirstOrDefault(tag => tag == id);
if (tg != null)
{
p.Tags.Remove(tg);
p.DateModified = DateTime.Now;
p.Save();
}
}
return true;
}
catch (Exception ex)
{
Utils.Log($"Tags.Delete: {ex.Message}");
return false;
}
}
}
}