6
6
using System ;
7
7
using System . Collections . Generic ;
8
8
using System . IO ;
9
+ using System . Linq ;
9
10
using System . Text . Json ;
10
11
using System . Threading . Tasks ;
11
12
12
13
namespace Pixel . Persistence . Respository
13
- {
14
+ {
14
15
public class ControlRepository : IControlRepository
15
16
{
16
17
private readonly IMongoCollection < BsonDocument > controlsCollection ;
@@ -32,6 +33,87 @@ public ControlRepository(IMongoDbSettings dbSettings)
32
33
33
34
}
34
35
36
+ ///<inheritdoc/>
37
+ public async IAsyncEnumerable < DataFile > GetControlFiles ( string applicationId , string controlId )
38
+ {
39
+ Guard . Argument ( applicationId , nameof ( applicationId ) ) . NotNull ( ) . NotEmpty ( ) ;
40
+ Guard . Argument ( controlId , nameof ( controlId ) ) . NotNull ( ) . NotEmpty ( ) ;
41
+
42
+ //Get all the versoins of the control
43
+ var projection = Builders < BsonDocument > . Projection . Exclude ( "_id" ) . Exclude ( "LastUpdated" ) ;
44
+ var result = controlsCollection . Find < BsonDocument > ( CreateControlFilter ( applicationId , controlId ) ) . Project ( projection ) ;
45
+ var documents = await result . ToListAsync ( ) ;
46
+ foreach ( var document in documents )
47
+ {
48
+ using ( MemoryStream ms = new MemoryStream ( ) )
49
+ {
50
+ using ( StreamWriter sw = new StreamWriter ( ms , System . Text . Encoding . UTF8 ) )
51
+ {
52
+ var jsonData = JsonSerializer . Serialize ( BsonTypeMapper . MapToDotNetValue ( document ) , new JsonSerializerOptions ( )
53
+ {
54
+ WriteIndented = true
55
+ } ) ;
56
+ sw . Write ( jsonData ) ;
57
+ sw . Flush ( ) ;
58
+ yield return new DataFile ( ) { FileName = $ "{ controlId } .dat", Version = document [ "Version" ] . AsString , Bytes = ms . ToArray ( ) , Type = "ControlFile" } ;
59
+
60
+ }
61
+ }
62
+ }
63
+
64
+ //Get all the versions of the images
65
+ var filter = CreateImageFilter ( applicationId , controlId ) ;
66
+ var sort = Builders < GridFSFileInfo > . Sort . Descending ( x => x . UploadDateTime ) ;
67
+ var options = new GridFSFindOptions
68
+ {
69
+ Sort = sort
70
+ } ;
71
+ using ( var cursor = await imageBucket . FindAsync ( filter , new GridFSFindOptions ( ) ) )
72
+ {
73
+ var imageFiles = await cursor . ToListAsync ( ) ;
74
+ foreach ( var imageFile in imageFiles )
75
+ {
76
+ var imageBytes = await imageBucket . DownloadAsBytesAsync ( imageFile . Id ) ;
77
+ yield return new DataFile ( ) { FileName = imageFile . Filename , Version = imageFile . Metadata [ "version" ] . AsString , Bytes = imageBytes , Type = "ControlImage" } ;
78
+ }
79
+ }
80
+
81
+ }
82
+
83
+ ///<inheritdoc/>
84
+ public async Task < IEnumerable < object > > GetAllControlsForApplication ( string applicationId , DateTime laterThan )
85
+ {
86
+ Guard . Argument ( applicationId , nameof ( applicationId ) ) . NotNull ( ) . NotEmpty ( ) ;
87
+ var controlFilter = Builders < BsonDocument > . Filter . Eq ( x => x [ "ApplicationId" ] , applicationId ) & Builders < BsonDocument > . Filter . Gt ( x => x [ "LastUpdated" ] , laterThan ) ;
88
+ var controls = ( await ( await controlsCollection . FindAsync < BsonDocument > ( controlFilter ) ) . ToListAsync ( ) ) . Select ( s => BsonTypeMapper . MapToDotNetValue ( s ) ) ;
89
+ return controls ;
90
+ }
91
+
92
+ ///<inheritdoc/>
93
+ public async Task < IEnumerable < ControlImageDataFile > > GetAllControlImagesForApplication ( string applicationId , DateTime laterThan )
94
+ {
95
+ Guard . Argument ( applicationId , nameof ( applicationId ) ) . NotNull ( ) . NotEmpty ( ) ;
96
+ List < ControlImageDataFile > controlImages = new ( ) ;
97
+ var imageFilter = Builders < GridFSFileInfo > . Filter . Eq ( x => x . Metadata [ "applicationId" ] , applicationId ) & Builders < GridFSFileInfo > . Filter . Gt ( x => x . UploadDateTime , laterThan ) ;
98
+ using ( var cursor = await imageBucket . FindAsync ( imageFilter , new GridFSFindOptions ( ) ) )
99
+ {
100
+ var imageFiles = await cursor . ToListAsync ( ) ;
101
+
102
+ foreach ( var imageFile in imageFiles )
103
+ {
104
+ var imageBytes = await imageBucket . DownloadAsBytesAsync ( imageFile . Id ) ;
105
+ controlImages . Add ( new ControlImageDataFile ( )
106
+ {
107
+ FileName = imageFile . Filename ,
108
+ ControlId = imageFile . Metadata [ "controlId" ] . AsString ,
109
+ Version = imageFile . Metadata [ "version" ] . AsString ,
110
+ Bytes = imageBytes
111
+ } ) ;
112
+ }
113
+ }
114
+ return controlImages ;
115
+ }
116
+
35
117
///<inheritdoc/>
36
118
public async Task AddOrUpdateControl ( string controlDataJson )
37
119
{
@@ -101,74 +183,7 @@ public async Task DeleteImageAsync(ControlImageMetaData imageMetaData)
101
183
await imageBucket . DeleteAsync ( imageFile . Id ) ;
102
184
}
103
185
}
104
- }
105
-
106
- ///<inheritdoc/>
107
- public async IAsyncEnumerable < DataFile > GetControlFiles ( string applicationId , string controlId )
108
- {
109
- Guard . Argument ( applicationId , nameof ( applicationId ) ) . NotNull ( ) . NotEmpty ( ) ;
110
- Guard . Argument ( controlId , nameof ( controlId ) ) . NotNull ( ) . NotEmpty ( ) ;
111
-
112
- //Get all the versoins of the control
113
- var projection = Builders < BsonDocument > . Projection . Exclude ( "_id" ) . Exclude ( "LastUpdated" ) ;
114
- var result = controlsCollection . Find < BsonDocument > ( CreateControlFilter ( applicationId , controlId ) ) . Project ( projection ) ;
115
- var documents = await result . ToListAsync ( ) ;
116
- foreach ( var document in documents )
117
- {
118
- using ( MemoryStream ms = new MemoryStream ( ) )
119
- {
120
- using ( StreamWriter sw = new StreamWriter ( ms , System . Text . Encoding . UTF8 ) )
121
- {
122
- var jsonData = JsonSerializer . Serialize ( BsonTypeMapper . MapToDotNetValue ( document ) , new JsonSerializerOptions ( )
123
- {
124
- WriteIndented = true
125
- } ) ;
126
- sw . Write ( jsonData ) ;
127
- sw . Flush ( ) ;
128
- yield return new DataFile ( ) { FileName = $ "{ controlId } .dat", Version = document [ "Version" ] . AsString , Bytes = ms . ToArray ( ) , Type = "ControlFile" } ;
129
-
130
- }
131
- }
132
- }
133
-
134
- //Get all the versions of the images
135
- var filter = CreateImageFilter ( applicationId , controlId ) ;
136
- var sort = Builders < GridFSFileInfo > . Sort . Descending ( x => x . UploadDateTime ) ;
137
- var options = new GridFSFindOptions
138
- {
139
- Sort = sort
140
- } ;
141
- using ( var cursor = await imageBucket . FindAsync ( filter , new GridFSFindOptions ( ) ) )
142
- {
143
- var imageFiles = await cursor . ToListAsync ( ) ;
144
- foreach ( var imageFile in imageFiles )
145
- {
146
- var imageBytes = await imageBucket . DownloadAsBytesAsync ( imageFile . Id ) ;
147
- yield return new DataFile ( ) { FileName = imageFile . Filename , Version = imageFile . Metadata [ "version" ] . AsString , Bytes = imageBytes , Type = "ControlImage" } ;
148
- }
149
- }
150
-
151
- }
152
-
153
- ///<inheritdoc/>
154
- public async IAsyncEnumerable < ControlMetaData > GetMetadataAsync ( string applicationId )
155
- {
156
- Guard . Argument ( applicationId , nameof ( applicationId ) ) . NotNull ( ) . NotEmpty ( ) ;
157
-
158
- var filterBuilder = Builders < BsonDocument > . Filter ;
159
- var filter = filterBuilder . Eq ( x => x [ "ApplicationId" ] , applicationId ) ;
160
- var projection = Builders < BsonDocument > . Projection . Include ( "ControlId" ) . Include ( "Version" ) . Include ( "LastUpdated" ) ;
161
- var results = await controlsCollection . Find < BsonDocument > ( filter ) . Project ( projection ) . ToListAsync ( ) ;
162
- foreach ( var doc in results )
163
- {
164
- yield return new ControlMetaData ( )
165
- {
166
- ControlId = doc [ "ControlId" ] . AsString ,
167
- Version = doc [ "Version" ] . AsString ,
168
- LastUpdated = doc [ "LastUpdated" ] . ToUniversalTime ( )
169
- } ;
170
- }
171
- }
186
+ }
172
187
173
188
/// <summary>
174
189
/// Create filter condition for control file with given applicationId and controlId
0 commit comments