@@ -27,6 +27,16 @@ namespace PlanB.Butler.Services
27
27
/// </summary>
28
28
public static class MealService
29
29
{
30
+ /// <summary>
31
+ /// The meta date.
32
+ /// </summary>
33
+ private const string MetaDate = "date" ;
34
+
35
+ /// <summary>
36
+ /// The meta restaurant.
37
+ /// </summary>
38
+ private const string MetaRestaurant = "restaurant" ;
39
+
30
40
/// <summary>
31
41
/// Create meal.
32
42
/// </summary>
@@ -72,8 +82,8 @@ public static async Task<IActionResult> CreateMeal(
72
82
if ( blob != null )
73
83
{
74
84
blob . Properties . ContentType = "application/json" ;
75
- blob . Metadata . Add ( "date" , date ) ;
76
- blob . Metadata . Add ( "restaurant" , mealModel . Restaurant ) ;
85
+ blob . Metadata . Add ( MetaDate , date ) ;
86
+ blob . Metadata . Add ( MetaRestaurant , mealModel . Restaurant ) ;
77
87
blob . Metadata . Add ( Constants . ButlerCorrelationTraceName , correlationId . ToString ( ) . Replace ( "-" , string . Empty ) ) ;
78
88
var meal = JsonConvert . SerializeObject ( mealModel ) ;
79
89
trace . Add ( "meal" , meal ) ;
@@ -128,25 +138,68 @@ public static async Task<IActionResult> GetMeals(
128
138
129
139
try
130
140
{
131
- string startDate = req . Query [ "startDate" ] ;
132
- string endDate = req . Query [ "endDate" ] ;
133
- string restaurant = req . Query [ "restaurant" ] ;
141
+ string startDateQuery = req . Query [ "startDate" ] ;
142
+ string endDateQuery = req . Query [ "endDate" ] ;
143
+ string restaurantQuery = req . Query [ "restaurant" ] ;
134
144
string prefix = string . Empty ;
135
145
136
- prefix = CreateBlobPrefix ( startDate , endDate ) ;
146
+ bool checkForDate = false ;
147
+ DateTime start = DateTime . MinValue ;
148
+ DateTime end = DateTime . MinValue ;
149
+
150
+ if ( ! ( string . IsNullOrEmpty ( startDateQuery ) && string . IsNullOrEmpty ( endDateQuery ) ) )
151
+ {
152
+ checkForDate = true ;
153
+ DateTime . TryParse ( startDateQuery , out start ) ;
154
+ DateTime . TryParse ( endDateQuery , out end ) ;
155
+ }
156
+
157
+ if ( checkForDate )
158
+ {
159
+ prefix = CreateBlobPrefix ( startDateQuery , endDateQuery ) ;
160
+ }
137
161
138
162
BlobContinuationToken blobContinuationToken = null ;
139
163
var options = new BlobRequestOptions ( ) ;
140
164
var operationContext = new OperationContext ( ) ;
141
165
142
- var blobs = await cloudBlobContainer . ListBlobsSegmentedAsync ( prefix , true , BlobListingDetails . All , null , blobContinuationToken , options , operationContext ) . ConfigureAwait ( false ) ;
143
- foreach ( var item in blobs . Results )
166
+ List < IListBlobItem > cloudBlockBlobs = new List < IListBlobItem > ( ) ;
167
+ do
144
168
{
145
- CloudBlockBlob blob = ( CloudBlockBlob ) item ;
169
+ var blobs = await cloudBlobContainer . ListBlobsSegmentedAsync ( prefix , true , BlobListingDetails . All , null , blobContinuationToken , options , operationContext ) . ConfigureAwait ( false ) ;
170
+ blobContinuationToken = blobs . ContinuationToken ;
171
+ cloudBlockBlobs . AddRange ( blobs . Results ) ;
172
+ }
173
+ while ( blobContinuationToken != null ) ;
146
174
147
- var content = blob . DownloadTextAsync ( ) ;
148
- var meal = JsonConvert . DeserializeObject < MealModel > ( await content ) ;
149
- meals . Add ( meal ) ;
175
+ foreach ( var item in cloudBlockBlobs )
176
+ {
177
+ CloudBlockBlob blob = ( CloudBlockBlob ) item ;
178
+ if ( checkForDate )
179
+ {
180
+ await blob . FetchAttributesAsync ( ) ;
181
+ if ( blob . Metadata . ContainsKey ( MetaDate ) )
182
+ {
183
+ var mealMetaDate = blob . Metadata [ MetaDate ] ;
184
+ DateTime mealDate = DateTime . MinValue ;
185
+ if ( DateTime . TryParse ( mealMetaDate , out mealDate ) )
186
+ {
187
+ var isDateInRange = IsDateInRange ( start , end , mealDate ) ;
188
+ if ( isDateInRange )
189
+ {
190
+ var blobContent = blob . DownloadTextAsync ( ) ;
191
+ var blobMeal = JsonConvert . DeserializeObject < MealModel > ( await blobContent ) ;
192
+ meals . Add ( blobMeal ) ;
193
+ }
194
+ }
195
+ }
196
+ }
197
+ else
198
+ {
199
+ var content = blob . DownloadTextAsync ( ) ;
200
+ var meal = JsonConvert . DeserializeObject < MealModel > ( await content ) ;
201
+ meals . Add ( meal ) ;
202
+ }
150
203
}
151
204
152
205
log . LogInformation ( correlationId , $ "'{ methodName } ' - success", trace ) ;
@@ -222,6 +275,53 @@ public static IActionResult GetMealById(
222
275
return ( ActionResult ) new OkObjectResult ( mealModel ) ;
223
276
}
224
277
278
+ /// <summary>
279
+ /// Determines whether the date in range compared to start and end.
280
+ /// </summary>
281
+ /// <param name="start">The start.</param>
282
+ /// <param name="end">The end.</param>
283
+ /// <param name="toCheck">To check.</param>
284
+ /// <returns>
285
+ /// <c>true</c> if date is in range; otherwise, <c>false</c>.
286
+ /// </returns>
287
+ internal static bool IsDateInRange ( DateTime start , DateTime end , DateTime toCheck )
288
+ {
289
+ if ( toCheck < start )
290
+ {
291
+ return false ;
292
+ }
293
+
294
+ if ( end < toCheck )
295
+ {
296
+ return false ;
297
+ }
298
+
299
+ if ( start . Equals ( toCheck ) )
300
+ {
301
+ return true ;
302
+ }
303
+
304
+ if ( end . Equals ( toCheck ) )
305
+ {
306
+ return true ;
307
+ }
308
+
309
+ if ( start . Equals ( end ) && start . Equals ( toCheck ) )
310
+ {
311
+ return true ;
312
+ }
313
+
314
+ long difference = toCheck . Ticks - start . Ticks ;
315
+ long sum = start . Ticks + difference ;
316
+
317
+ if ( sum < end . Ticks )
318
+ {
319
+ return true ;
320
+ }
321
+
322
+ return false ;
323
+ }
324
+
225
325
/// <summary>
226
326
/// Creates the BLOB prefix.
227
327
/// </summary>
0 commit comments