-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionRepository.cs
162 lines (136 loc) · 6.21 KB
/
CollectionRepository.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
using System.Collections.Generic;
using System.Threading.Tasks;
using Collections.API.Helpers;
using Collections.API.Repositories.Interfaces;
using Collections.API.Services.Interfaces;
using MongoDB.Bson;
using MongoDB.Driver;
namespace Collections.API.Repositories
{
/// <summary>
/// Repository for data from Mongo DB
/// </summary>
/// <seealso cref="Collections.API.Repositories.Interfaces.ICollectionRepository" />
public class CollectionRepository : ICollectionRepository
{
/// <summary>
/// The data collection
/// </summary>
private readonly IDataRepository dataRepository;
/// <summary>
/// The query service
/// </summary>
private readonly IQueryService queryService;
/// <summary>
/// Initializes a new instance of the <see cref="CollectionRepository"/> class.
/// </summary>
/// <param name="queryService">The query service</param>
/// <param name="dataRepository">The data collection</param>
public CollectionRepository(IQueryService queryService, IDataRepository dataRepository)
{
this.queryService = queryService;
this.dataRepository = dataRepository;
}
/// <summary>
/// Gets the records asynchronously.
/// </summary>
/// <typeparam name="TInterface">The collection type</typeparam>
/// <typeparam name="TModel">The model type</typeparam>
/// <returns>All records of requested type</returns>
public async Task<IEnumerable<TInterface>> GetAsync<TInterface, TModel>() where TModel : class, TInterface, new()
{
var filter = Builders<TInterface>.Filter.Eq("_t", (typeof(TModel).Name));
var result = await this.dataRepository.FindManyAsync<TInterface>(filter);
return result;
}
/// <summary>
/// Gets the requested record asynchronously.
/// </summary>
/// <param name="id">The Id of the record to be retrieved</param>
/// <typeparam name="TInterface">The collection type</typeparam>
/// <returns>Retrieved record by it's Id</returns>
public async Task<TInterface> GetByIdAsync<TInterface>(string id)
{
var filter = Builders<TInterface>.Filter.Eq("_id", ObjectId.Parse(id));
var result = await this.dataRepository.FindOneAsync<TInterface>(filter);
return result;
}
/// <summary>
/// Searches for the model provided.
/// </summary>
/// <typeparam name="TInterface">The collection type</typeparam>
/// <typeparam name="TModel">The model type</typeparam>
/// <param name="model">The advanced search model.</param>
/// <returns>Results from the search</returns>
public async Task<IEnumerable<TInterface>> PostSearchAsync<TInterface, TModel>(TModel model) where TModel : class, TInterface, new()
{
var filter = this.queryService.BuildQuery<TInterface, TModel>(model);
var result = await this.dataRepository.FindManyAsync<TInterface>(filter);
return result;
}
/// <summary>
/// Posts multiple records asynchronously
/// </summary>
/// <param name="model">The model to be posted</param>
/// <typeparam name="TInterface">The collection type</typeparam>
/// <typeparam name="TModel">The model type</typeparam>
/// <returns>True if successful</returns>
public async Task<bool> PostMultipleAsync<TInterface, TModel>(IEnumerable<TModel> model) where TModel : class, TInterface, new()
{
await this.dataRepository.InsertManyAsync<TInterface, TModel>(model);
return true;
}
/// <summary>
/// Patches specified record asynchronously.
/// </summary>
/// <param name="id">The identifier.</param>
/// <param name="model">The model.</param>
/// <typeparam name="TInterface">The collection type</typeparam>
/// <typeparam name="TModel">The model tyoe</typeparam>
/// <returns>True if successful</returns>
public async Task<bool> PatchAsync<TInterface, TModel>(string id, TModel model) where TModel : class, TInterface, new()
{
var dictionary = model.ToValueDictionary();
var result = await this.dataRepository.UpdateOneAsync<TInterface>(new BsonDocument("_id", ObjectId.Parse(id)), new BsonDocument("$set", new BsonDocument(dictionary)));
if (result.IsAcknowledged && (result.MatchedCount == 1 && result.ModifiedCount == 1))
{
return true;
}
return false;
}
/// <summary>
/// Puts specified record asynchronously.
/// </summary>
/// <param name="id">The identifier.</param>
/// <param name="model">The model.</param>
/// <typeparam name="TInterface">The collection type</typeparam>
/// <typeparam name="TModel">The model tyoe</typeparam>
/// <returns>True if successful</returns>
public async Task<bool> PutAsync<TInterface, TModel>(string id, TModel model) where TModel : class, TInterface, new()
{
var filter = Builders<TInterface>.Filter.Eq("_id", ObjectId.Parse(id));
var result = await this.dataRepository.ReplaceOneAsync<TInterface, TModel>(filter, model);
if (result.IsAcknowledged && (result.MatchedCount == 1 && result.ModifiedCount == 1))
{
return true;
}
return false;
}
/// <summary>
/// Deletes specified records asynchronously.
/// </summary>
/// <param name="id">The identifier.</param>
/// <typeparam name="TInterface">The collection type</typeparam>
/// <returns>True if successful</returns>
public async Task<bool> DeleteAsync<TInterface>(string id)
{
var filter = Builders<TInterface>.Filter.Eq("_id", ObjectId.Parse(id));
var result = await this.dataRepository.DeleteManyAsync<TInterface>(filter);
if (result.IsAcknowledged && result.DeletedCount >= 1)
{
return true;
}
return false;
}
}
}