forked from Farfetch/rules-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDbProviderRulesDataSource.cs
170 lines (140 loc) · 8.98 KB
/
MongoDbProviderRulesDataSource.cs
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
namespace Rules.Framework.Providers.MongoDb
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MongoDB.Driver;
using Rules.Framework.Core;
using Rules.Framework.Providers.MongoDb.DataModel;
/// <summary>
/// The rules data source implementation for usage backed with a Mongo DB database.
/// </summary>
/// <typeparam name="TContentType">The type of the content type.</typeparam>
/// <typeparam name="TConditionType">The type of the condition type.</typeparam>
/// <seealso cref="Rules.Framework.IRulesDataSource{TContentType, TConditionType}" />
public class MongoDbProviderRulesDataSource<TContentType, TConditionType> : IRulesDataSource<TContentType, TConditionType>
{
private readonly IMongoDatabase mongoDatabase;
private readonly MongoDbProviderSettings mongoDbProviderSettings;
private readonly IRuleFactory<TContentType, TConditionType> ruleFactory;
internal MongoDbProviderRulesDataSource(
IMongoClient mongoClient,
MongoDbProviderSettings mongoDbProviderSettings,
IRuleFactory<TContentType, TConditionType> ruleFactory)
{
if (mongoClient is null)
{
throw new ArgumentNullException(nameof(mongoClient));
}
this.mongoDbProviderSettings = mongoDbProviderSettings ?? throw new ArgumentNullException(nameof(mongoDbProviderSettings));
this.ruleFactory = ruleFactory ?? throw new ArgumentNullException(nameof(ruleFactory));
this.mongoDatabase = mongoClient.GetDatabase(this.mongoDbProviderSettings.DatabaseName);
}
/// <summary>
/// Adds a new rule to data source.
/// </summary>
/// <param name="rule">The rule.</param>
public async Task AddRuleAsync(Rule<TContentType, TConditionType> rule)
{
IMongoCollection<RuleDataModel> rulesCollection = this.mongoDatabase.GetCollection<RuleDataModel>(this.mongoDbProviderSettings.RulesCollectionName);
RuleDataModel ruleDataModel = this.ruleFactory.CreateRule(rule);
await rulesCollection.InsertOneAsync(ruleDataModel).ConfigureAwait(false);
}
/// <summary>
/// Gets the rules categorized with specified <paramref name="contentType" /> between <paramref name="dateBegin" /> and <paramref name="dateEnd" />.
/// </summary>
/// <param name="contentType">the content type categorization.</param>
/// <param name="dateBegin">the filtering begin date.</param>
/// <param name="dateEnd">the filtering end date.</param>
/// <returns></returns>
public async Task<IEnumerable<Rule<TContentType, TConditionType>>> GetRulesAsync(TContentType contentType, DateTime dateBegin, DateTime dateEnd)
{
FilterDefinition<RuleDataModel> getRulesByContentTypeAndDatesInterval = MongoDbProviderRulesDataSource<TContentType, TConditionType>
.BuildFilterByContentTypeAndDatesInterval(contentType, dateBegin, dateEnd);
return await this.GetRulesAsync(getRulesByContentTypeAndDatesInterval).ConfigureAwait(false);
}
/// <summary>
/// Gets the rules filtered by specified arguments.
/// </summary>
/// <param name="rulesFilterArgs">The rules filter arguments.</param>
/// <returns></returns>
public Task<IEnumerable<Rule<TContentType, TConditionType>>> GetRulesByAsync(RulesFilterArgs<TContentType> rulesFilterArgs)
{
if (rulesFilterArgs is null)
{
throw new ArgumentNullException(nameof(rulesFilterArgs));
}
FilterDefinition<RuleDataModel> filterDefinition = MongoDbProviderRulesDataSource<TContentType, TConditionType>
.BuildFilterFromRulesFilterArgs(rulesFilterArgs);
return this.GetRulesAsync(filterDefinition);
}
/// <summary>
/// Updates the existent rule on data source.
/// </summary>
/// <param name="rule">The rule.</param>
public async Task UpdateRuleAsync(Rule<TContentType, TConditionType> rule)
{
IMongoCollection<RuleDataModel> rulesCollection = this.mongoDatabase.GetCollection<RuleDataModel>(this.mongoDbProviderSettings.RulesCollectionName);
RuleDataModel ruleDataModel = this.ruleFactory.CreateRule(rule);
FilterDefinition<RuleDataModel> filterDefinition = Builders<RuleDataModel>.Filter.Eq(x => x.Name, ruleDataModel.Name);
FieldDefinition<RuleDataModel, object> contentField = "Content";
IEnumerable<UpdateDefinition<RuleDataModel>> updateDefinitions = new UpdateDefinition<RuleDataModel>[]
{
Builders<RuleDataModel>.Update.Set(contentField, (object)ruleDataModel.Content),
Builders<RuleDataModel>.Update.Set(r => r.ContentType, ruleDataModel.ContentType),
Builders<RuleDataModel>.Update.Set(r => r.DateBegin, ruleDataModel.DateBegin),
Builders<RuleDataModel>.Update.Set(r => r.DateEnd, ruleDataModel.DateEnd),
Builders<RuleDataModel>.Update.Set(r => r.Name, ruleDataModel.Name),
Builders<RuleDataModel>.Update.Set(r => r.Priority, ruleDataModel.Priority),
Builders<RuleDataModel>.Update.Set(r => r.RootCondition, ruleDataModel.RootCondition),
};
UpdateDefinition<RuleDataModel> updateDefinition = Builders<RuleDataModel>.Update.Combine(updateDefinitions);
await rulesCollection.UpdateOneAsync(filterDefinition, updateDefinition).ConfigureAwait(false);
}
private static FilterDefinition<RuleDataModel> BuildFilterByContentTypeAndDatesInterval(TContentType contentType, DateTime dateBegin, DateTime dateEnd)
{
FilterDefinition<RuleDataModel> contentTypeFilter = Builders<RuleDataModel>.Filter.Eq(x => x.ContentType, contentType.ToString());
// To fetch rules that begin during filtered interval but end after it.
FilterDefinition<RuleDataModel> dateBeginFilter = Builders<RuleDataModel>.Filter.And(
Builders<RuleDataModel>.Filter.Gte(x => x.DateBegin, dateBegin),
Builders<RuleDataModel>.Filter.Lt(x => x.DateBegin, dateEnd));
// To fetch rules that begun before filtered interval but end during it.
FilterDefinition<RuleDataModel> dateEndFilter = Builders<RuleDataModel>.Filter.And(
Builders<RuleDataModel>.Filter.Ne(x => x.DateEnd, null),
Builders<RuleDataModel>.Filter.Gte(x => x.DateEnd, dateBegin),
Builders<RuleDataModel>.Filter.Lt(x => x.DateEnd, dateEnd));
// To fetch rules that begun before and end after filtered interval.
FilterDefinition<RuleDataModel> slicingFilter = Builders<RuleDataModel>.Filter.And(
Builders<RuleDataModel>.Filter.Lt(x => x.DateBegin, dateBegin),
Builders<RuleDataModel>.Filter.Or(
Builders<RuleDataModel>.Filter.Eq(x => x.DateEnd, null),
Builders<RuleDataModel>.Filter.Gt(x => x.DateEnd, dateEnd)));
return Builders<RuleDataModel>.Filter.And(contentTypeFilter, Builders<RuleDataModel>.Filter.Or(dateBeginFilter, dateEndFilter, slicingFilter));
}
private static FilterDefinition<RuleDataModel> BuildFilterFromRulesFilterArgs(RulesFilterArgs<TContentType> rulesFilterArgs)
{
List<FilterDefinition<RuleDataModel>> filtersToApply = new List<FilterDefinition<RuleDataModel>>(3);
if (!object.Equals(rulesFilterArgs.ContentType, default(TContentType)))
{
filtersToApply.Add(Builders<RuleDataModel>.Filter.Eq(x => x.ContentType, rulesFilterArgs.ContentType.ToString()));
}
if (!string.IsNullOrWhiteSpace(rulesFilterArgs.Name))
{
filtersToApply.Add(Builders<RuleDataModel>.Filter.Eq(x => x.Name, rulesFilterArgs.Name));
}
if (rulesFilterArgs.Priority.HasValue)
{
filtersToApply.Add(Builders<RuleDataModel>.Filter.Eq(x => x.Priority, rulesFilterArgs.Priority.GetValueOrDefault()));
}
return filtersToApply.Any() ? Builders<RuleDataModel>.Filter.And(filtersToApply) : Builders<RuleDataModel>.Filter.Empty;
}
private async Task<IEnumerable<Rule<TContentType, TConditionType>>> GetRulesAsync(FilterDefinition<RuleDataModel> getRulesByContentTypeAndDatesInterval)
{
IMongoCollection<RuleDataModel> rulesCollection = this.mongoDatabase.GetCollection<RuleDataModel>(this.mongoDbProviderSettings.RulesCollectionName);
IAsyncCursor<RuleDataModel> fetchedRulesCursor = await rulesCollection.FindAsync(getRulesByContentTypeAndDatesInterval).ConfigureAwait(false);
List<RuleDataModel> fetchedRules = await fetchedRulesCursor.ToListAsync().ConfigureAwait(false);
return fetchedRules.Select(r => this.ruleFactory.CreateRule(r));
}
}
}