Repository navigation
Expand file tree
/
Copy pathTopicService.cs
More file actions
208 lines (180 loc) · 6.95 KB
/
Copy pathTopicService.cs
File metadata and controls
208 lines (180 loc) · 6.95 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using Grand.Core;
using Grand.Core.Caching;
using Grand.Core.Data;
using Grand.Core.Domain.Catalog;
using Grand.Core.Domain.Security;
using Grand.Core.Domain.Topics;
using Grand.Services.Customers;
using Grand.Services.Events;
using Grand.Services.Stores;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Grand.Services.Topics
{
/// <summary>
/// Topic service
/// </summary>
public partial class TopicService : ITopicService
{
#region Constants
/// <summary>
/// Key for caching
/// </summary>
/// <remarks>
/// {0} : store ID
/// {1} : ignore ACL?
/// </remarks>
private const string TOPICS_ALL_KEY = "Grand.topics.all-{0}-{1}";
/// <summary>
/// Key for caching
/// </summary>
/// <remarks>
/// {0} : topic ID
/// </remarks>
private const string TOPICS_BY_ID_KEY = "Grand.topics.id-{0}";
/// <summary>
/// Key pattern to clear cache
/// </summary>
private const string TOPICS_PATTERN_KEY = "Grand.topics.";
#endregion
#region Fields
private readonly IRepository<Topic> _topicRepository;
private readonly IWorkContext _workContext;
private readonly IRepository<AclRecord> _aclRepository;
private readonly IStoreMappingService _storeMappingService;
private readonly CatalogSettings _catalogSettings;
private readonly IEventPublisher _eventPublisher;
private readonly ICacheManager _cacheManager;
#endregion
#region Ctor
public TopicService(IRepository<Topic> topicRepository,
IRepository<AclRecord> aclRepository,
IWorkContext workContext,
IStoreMappingService storeMappingService,
CatalogSettings catalogSettings,
IEventPublisher eventPublisher,
ICacheManager cacheManager)
{
this._topicRepository = topicRepository;
this._aclRepository = aclRepository;
this._workContext = workContext;
this._storeMappingService = storeMappingService;
this._catalogSettings = catalogSettings;
this._eventPublisher = eventPublisher;
this._cacheManager = cacheManager;
}
#endregion
#region Methods
/// <summary>
/// Deletes a topic
/// </summary>
/// <param name="topic">Topic</param>
public virtual async Task DeleteTopic(Topic topic)
{
if (topic == null)
throw new ArgumentNullException("topic");
await _topicRepository.DeleteAsync(topic);
//cache
_cacheManager.RemoveByPattern(TOPICS_PATTERN_KEY);
//event notification
await _eventPublisher.EntityDeleted(topic);
}
/// <summary>
/// Gets a topic
/// </summary>
/// <param name="topicId">The topic identifier</param>
/// <returns>Topic</returns>
public virtual Task<Topic> GetTopicById(string topicId)
{
string key = string.Format(TOPICS_BY_ID_KEY, topicId);
return _cacheManager.Get(key, () => _topicRepository.GetByIdAsync(topicId));
}
/// <summary>
/// Gets a topic
/// </summary>
/// <param name="systemName">The topic system name</param>
/// <param name="storeId">Store identifier; pass 0 to ignore filtering by store and load the first one</param>
/// <returns>Topic</returns>
public virtual async Task<Topic> GetTopicBySystemName(string systemName, string storeId = "")
{
if (String.IsNullOrEmpty(systemName))
return null;
var query = _topicRepository.Table;
query = query.Where(t => t.SystemName.ToLower() == systemName.ToLower());
query = query.OrderBy(t => t.Id);
var topics = await query.ToListAsync();
if (!String.IsNullOrEmpty(storeId))
{
topics = topics.Where(x => _storeMappingService.Authorize(x, storeId)).ToList();
}
return topics.FirstOrDefault();
}
/// <summary>
/// Gets all topics
/// </summary>
/// <param name="storeId">Store identifier; pass "" to load all records</param>
/// <returns>Topics</returns>
public virtual async Task<IList<Topic>> GetAllTopics(string storeId, bool ignorAcl = false)
{
string key = string.Format(TOPICS_ALL_KEY, storeId, ignorAcl);
return await _cacheManager.Get(key, () =>
{
var query = _topicRepository.Table;
query = query.OrderBy(t => t.DisplayOrder).ThenBy(t => t.SystemName);
if ((!String.IsNullOrEmpty(storeId) && !_catalogSettings.IgnoreStoreLimitations) ||
(!ignorAcl && !_catalogSettings.IgnoreAcl))
{
if (!ignorAcl && !_catalogSettings.IgnoreAcl)
{
var allowedCustomerRolesIds = _workContext.CurrentCustomer.GetCustomerRoleIds();
query = from p in query
where !p.SubjectToAcl || allowedCustomerRolesIds.Any(x => p.CustomerRoles.Contains(x))
select p;
}
//Store mapping
if (!String.IsNullOrEmpty(storeId) && !_catalogSettings.IgnoreStoreLimitations)
{
query = from p in query
where !p.LimitedToStores || p.Stores.Contains(storeId)
select p;
query = query.OrderBy(t => t.SystemName);
}
}
return query.ToListAsync();
});
}
/// <summary>
/// Inserts a topic
/// </summary>
/// <param name="topic">Topic</param>
public virtual async Task InsertTopic(Topic topic)
{
if (topic == null)
throw new ArgumentNullException("topic");
await _topicRepository.InsertAsync(topic);
//cache
_cacheManager.RemoveByPattern(TOPICS_PATTERN_KEY);
//event notification
await _eventPublisher.EntityInserted(topic);
}
/// <summary>
/// Updates the topic
/// </summary>
/// <param name="topic">Topic</param>
public virtual async Task UpdateTopic(Topic topic)
{
if (topic == null)
throw new ArgumentNullException("topic");
await _topicRepository.UpdateAsync(topic);
//cache
_cacheManager.RemoveByPattern(TOPICS_PATTERN_KEY);
//event notification
await _eventPublisher.EntityUpdated(topic);
}
#endregion
}
}