@@ -37,10 +37,12 @@ public interface IApplicationService
37
37
Task < bool > DeleteApplicationAsync ( Guid id , CancellationToken ct ) ;
38
38
39
39
// Application Instances
40
- Task < IEnumerable < ViewModels . ApplicationInstance > > GetInstancesByTeamAsync ( Guid teamId , CancellationToken ct ) ;
40
+ Task < IEnumerable < ViewModels . ApplicationInstance > > GetInstancesByTeamAsync ( Guid teamId , bool skipVerification , CancellationToken ct ) ;
41
41
Task < ViewModels . ApplicationInstance > GetInstanceAsync ( Guid id , CancellationToken ct ) ;
42
42
Task < ViewModels . ApplicationInstance > CreateInstanceAsync ( Guid teamId , ViewModels . ApplicationInstanceForm instance , CancellationToken ct ) ;
43
43
Task < ViewModels . ApplicationInstance > UpdateInstanceAsync ( Guid id , ViewModels . ApplicationInstanceForm instance , CancellationToken ct ) ;
44
+ Task < IEnumerable < ViewModels . ApplicationInstance > > MoveUpInstanceAsync ( Guid id , CancellationToken ct ) ;
45
+ Task < IEnumerable < ViewModels . ApplicationInstance > > MoveDownInstanceAsync ( Guid id , CancellationToken ct ) ;
44
46
Task < bool > DeleteInstanceAsync ( Guid id , CancellationToken ct ) ;
45
47
}
46
48
@@ -225,24 +227,27 @@ public async Task<bool> DeleteApplicationAsync(Guid id, CancellationToken ct)
225
227
226
228
#region Application Instances
227
229
228
- public async Task < IEnumerable < ViewModels . ApplicationInstance > > GetInstancesByTeamAsync ( Guid teamId , CancellationToken ct )
230
+ public async Task < IEnumerable < ViewModels . ApplicationInstance > > GetInstancesByTeamAsync ( Guid teamId , bool skipVerification , CancellationToken ct )
229
231
{
230
- var team = await _context . Teams
232
+ if ( ! skipVerification )
233
+ {
234
+ var team = await _context . Teams
231
235
. Where ( e => e . Id == teamId )
232
236
. SingleOrDefaultAsync ( ct ) ;
233
237
238
+ if ( team == null )
239
+ throw new EntityNotFoundException < Team > ( ) ;
240
+
241
+ if ( ! ( await _authorizationService . AuthorizeAsync ( _user , null , new TeamAccessRequirement ( team . ViewId , teamId ) ) ) . Succeeded )
242
+ throw new ForbiddenException ( ) ;
243
+ }
244
+
234
245
var instances = await _context . ApplicationInstances
235
246
. Where ( i => i . TeamId == teamId )
236
247
. OrderBy ( a => a . DisplayOrder )
237
248
. ProjectTo < ViewModels . ApplicationInstance > ( _mapper . ConfigurationProvider )
238
249
. ToArrayAsync ( ct ) ;
239
250
240
- if ( team == null )
241
- throw new EntityNotFoundException < Team > ( ) ;
242
-
243
- if ( ! ( await _authorizationService . AuthorizeAsync ( _user , null , new TeamAccessRequirement ( team . ViewId , teamId ) ) ) . Succeeded )
244
- throw new ForbiddenException ( ) ;
245
-
246
251
return instances ;
247
252
}
248
253
@@ -322,6 +327,91 @@ public async Task<bool> DeleteInstanceAsync(Guid id, CancellationToken ct)
322
327
return true ;
323
328
}
324
329
330
+ public async Task < IEnumerable < ViewModels . ApplicationInstance > > MoveUpInstanceAsync ( Guid id , CancellationToken ct )
331
+ {
332
+ return await this . MoveInstanceAsync ( id , Direction . Up , ct ) ;
333
+ }
334
+
335
+ public async Task < IEnumerable < ViewModels . ApplicationInstance > > MoveDownInstanceAsync ( Guid id , CancellationToken ct )
336
+ {
337
+ return await this . MoveInstanceAsync ( id , Direction . Down , ct ) ;
338
+ }
339
+
340
+ private enum Direction
341
+ {
342
+ Up ,
343
+ Down
344
+ }
345
+
346
+ private async Task < IEnumerable < ViewModels . ApplicationInstance > > MoveInstanceAsync ( Guid id , Direction direction , CancellationToken ct )
347
+ {
348
+ var instanceToUpdate = await _context . ApplicationInstances
349
+ . Include ( x => x . Team )
350
+ . SingleOrDefaultAsync ( x => x . Id == id , ct ) ;
351
+
352
+ if ( instanceToUpdate == null )
353
+ throw new EntityNotFoundException < ApplicationInstance > ( ) ;
354
+
355
+ if ( ! ( await _authorizationService . AuthorizeAsync ( _user , null , new ViewAdminRequirement ( instanceToUpdate . Team . ViewId ) ) ) . Succeeded )
356
+ throw new ForbiddenException ( ) ;
357
+
358
+ var teamInstances = await _context . ApplicationInstances
359
+ . Where ( x => x . TeamId == instanceToUpdate . TeamId )
360
+ . OrderBy ( x => x . DisplayOrder )
361
+ . ToArrayAsync ( ct ) ;
362
+
363
+ if ( direction == Direction . Up )
364
+ {
365
+ for ( int i = 0 ; i < teamInstances . Length ; i ++ )
366
+ {
367
+ var teamInstance = teamInstances [ i ] ;
368
+
369
+ if ( teamInstance . Id == id )
370
+ {
371
+ teamInstance . DisplayOrder = i - 1 ;
372
+
373
+ var previous = teamInstances . ElementAtOrDefault ( i - 1 ) ;
374
+
375
+ if ( previous != null )
376
+ {
377
+ previous . DisplayOrder = previous . DisplayOrder + 1 ;
378
+ }
379
+ }
380
+ else
381
+ {
382
+ teamInstance . DisplayOrder = i ;
383
+ }
384
+ }
385
+ }
386
+ else if ( direction == Direction . Down )
387
+ {
388
+ for ( int i = teamInstances . Length - 1 ; i >= 0 ; i -- )
389
+ {
390
+ var teamInstance = teamInstances [ i ] ;
391
+
392
+ if ( teamInstance . Id == id )
393
+ {
394
+ teamInstance . DisplayOrder = i + 1 ;
395
+
396
+ var previous = teamInstances . ElementAtOrDefault ( i + 1 ) ;
397
+
398
+ if ( previous != null )
399
+ {
400
+ previous . DisplayOrder = previous . DisplayOrder - 1 ;
401
+ }
402
+ }
403
+ else
404
+ {
405
+ teamInstance . DisplayOrder = i ;
406
+ }
407
+ }
408
+ }
409
+
410
+ await _context . SaveChangesAsync ( ct ) ;
411
+
412
+ return await this . GetInstancesByTeamAsync ( instanceToUpdate . TeamId , skipVerification : true , ct ) ;
413
+ }
414
+
325
415
#endregion
326
416
}
327
417
}
0 commit comments