@@ -54,6 +54,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
54
54
[ ParameterSwapControllerActionSelector ( nameof ( GetById ) , "id" , typeof ( int ) , typeof ( Guid ) , typeof ( Udi ) ) ]
55
55
[ ParameterSwapControllerActionSelector ( nameof ( GetByIds ) , "ids" , typeof ( int [ ] ) , typeof ( Guid [ ] ) , typeof ( Udi [ ] ) ) ]
56
56
[ ParameterSwapControllerActionSelector ( nameof ( GetUrl ) , "id" , typeof ( int ) , typeof ( Udi ) ) ]
57
+ [ ParameterSwapControllerActionSelector ( nameof ( GetUrlsByIds ) , "ids" , typeof ( int [ ] ) , typeof ( Guid [ ] ) , typeof ( Udi [ ] ) ) ]
57
58
public class EntityController : UmbracoAuthorizedJsonController
58
59
{
59
60
private static readonly string [ ] _postFilterSplitStrings = { "=" , "==" , "!=" , "<>" , ">" , "<" , ">=" , "<=" } ;
@@ -319,45 +320,182 @@ public IActionResult GetUrl(Udi id, string culture = "*")
319
320
}
320
321
321
322
/// <summary>
322
- /// Get entity URLs by UDIs
323
+ /// Get entity URLs by IDs
323
324
/// </summary>
324
- /// <param name="udis ">
325
- /// A list of UDIs to lookup items by
325
+ /// <param name="ids ">
326
+ /// A list of IDs to lookup items by
326
327
/// </param>
327
- /// <param name="culture">The culture to fetch the URL for</param>
328
+ /// <param name="type">The entity type to look for.</param>
329
+ /// <param name="culture">The culture to fetch the URL for.</param>
328
330
/// <returns>Dictionary mapping Udi -> Url</returns>
329
331
/// <remarks>
330
- /// We allow for POST because there could be quite a lot of Ids.
332
+ /// We allow for POST because there could be quite a lot of Ids.
331
333
/// </remarks>
332
334
[ HttpGet ]
333
335
[ HttpPost ]
334
- public IDictionary < Udi , string > GetUrlsByUdis ( [ FromJsonPath ] Udi [ ] udis , string culture = null )
336
+ public IDictionary < int , string > GetUrlsByIds ( [ FromJsonPath ] int [ ] ids , [ FromQuery ] UmbracoEntityTypes type , [ FromQuery ] string culture = null )
337
+ {
338
+ if ( ids == null || ! ids . Any ( ) )
339
+ {
340
+ return new Dictionary < int , string > ( ) ;
341
+ }
342
+
343
+ string MediaOrDocumentUrl ( int id )
344
+ {
345
+ switch ( type )
346
+ {
347
+ case UmbracoEntityTypes . Document :
348
+ return _publishedUrlProvider . GetUrl ( id , culture : culture ?? ClientCulture ( ) ) ;
349
+
350
+ case UmbracoEntityTypes . Media :
351
+ {
352
+ IPublishedContent media = _publishedContentQuery . Media ( id ) ;
353
+
354
+ // NOTE: If culture is passed here we get an empty string rather than a media item URL.
355
+ return _publishedUrlProvider . GetMediaUrl ( media , culture : null ) ;
356
+ }
357
+
358
+ default :
359
+ return null ;
360
+ }
361
+ }
362
+
363
+ return ids
364
+ . Distinct ( )
365
+ . Select ( id => new {
366
+ Id = id ,
367
+ Url = MediaOrDocumentUrl ( id )
368
+ } ) . ToDictionary ( x => x . Id , x => x . Url ) ;
369
+ }
370
+
371
+ /// <summary>
372
+ /// Get entity URLs by IDs
373
+ /// </summary>
374
+ /// <param name="ids">
375
+ /// A list of IDs to lookup items by
376
+ /// </param>
377
+ /// <param name="type">The entity type to look for.</param>
378
+ /// <param name="culture">The culture to fetch the URL for.</param>
379
+ /// <returns>Dictionary mapping Udi -> Url</returns>
380
+ /// <remarks>
381
+ /// We allow for POST because there could be quite a lot of Ids.
382
+ /// </remarks>
383
+ [ HttpGet ]
384
+ [ HttpPost ]
385
+ public IDictionary < Guid , string > GetUrlsByIds ( [ FromJsonPath ] Guid [ ] ids , [ FromQuery ] UmbracoEntityTypes type , [ FromQuery ] string culture = null )
335
386
{
336
- if ( udis == null || udis . Length == 0 )
387
+ if ( ids == null || ! ids . Any ( ) )
388
+ {
389
+ return new Dictionary < Guid , string > ( ) ;
390
+ }
391
+
392
+ string MediaOrDocumentUrl ( Guid id )
393
+ {
394
+ return type switch
395
+ {
396
+ UmbracoEntityTypes . Document => _publishedUrlProvider . GetUrl ( id , culture : culture ?? ClientCulture ( ) ) ,
397
+
398
+ // NOTE: If culture is passed here we get an empty string rather than a media item URL.
399
+ UmbracoEntityTypes . Media => _publishedUrlProvider . GetMediaUrl ( id , culture : null ) ,
400
+
401
+ _ => null
402
+ } ;
403
+ }
404
+
405
+ return ids
406
+ . Distinct ( )
407
+ . Select ( id => new {
408
+ Id = id ,
409
+ Url = MediaOrDocumentUrl ( id )
410
+ } ) . ToDictionary ( x => x . Id , x => x . Url ) ;
411
+ }
412
+
413
+ /// <summary>
414
+ /// Get entity URLs by IDs
415
+ /// </summary>
416
+ /// <param name="ids">
417
+ /// A list of IDs to lookup items by
418
+ /// </param>
419
+ /// <param name="type">The entity type to look for.</param>
420
+ /// <param name="culture">The culture to fetch the URL for.</param>
421
+ /// <returns>Dictionary mapping Udi -> Url</returns>
422
+ /// <remarks>
423
+ /// We allow for POST because there could be quite a lot of Ids.
424
+ /// </remarks>
425
+ [ HttpGet ]
426
+ [ HttpPost ]
427
+ public IDictionary < Udi , string > GetUrlsByIds ( [ FromJsonPath ] Udi [ ] ids , [ FromQuery ] UmbracoEntityTypes type , [ FromQuery ] string culture = null )
428
+ {
429
+ if ( ids == null || ! ids . Any ( ) )
337
430
{
338
431
return new Dictionary < Udi , string > ( ) ;
339
432
}
340
433
341
434
// TODO: PMJ 2021-09-27 - Should GetUrl(Udi) exist as an extension method on UrlProvider/IUrlProvider (in v9)
342
- string MediaOrDocumentUrl ( Udi udi )
435
+ string MediaOrDocumentUrl ( Udi id )
343
436
{
344
- if ( udi is not GuidUdi guidUdi )
437
+ if ( id is not GuidUdi guidUdi )
345
438
{
346
439
return null ;
347
440
}
348
441
349
- return guidUdi . EntityType switch
442
+ return type switch
350
443
{
351
- Constants . UdiEntityType . Document => _publishedUrlProvider . GetUrl ( guidUdi . Guid ,
352
- culture : culture ?? ClientCulture ( ) ) ,
353
- // NOTE: If culture is passed here we get an empty string rather than a media item URL WAT
354
- Constants . UdiEntityType . Media => _publishedUrlProvider . GetMediaUrl ( guidUdi . Guid , culture : null ) ,
444
+ UmbracoEntityTypes . Document => _publishedUrlProvider . GetUrl ( guidUdi . Guid , culture : culture ?? ClientCulture ( ) ) ,
445
+
446
+ // NOTE: If culture is passed here we get an empty string rather than a media item URL.
447
+ UmbracoEntityTypes . Media => _publishedUrlProvider . GetMediaUrl ( guidUdi . Guid , culture : null ) ,
448
+
355
449
_ => null
356
450
} ;
357
451
}
358
452
359
- return udis
360
- . Select ( udi => new { Udi = udi , Url = MediaOrDocumentUrl ( udi ) } ) . ToDictionary ( x => x . Udi , x => x . Url ) ;
453
+ return ids
454
+ . Distinct ( )
455
+ . Select ( id => new {
456
+ Id = id ,
457
+ Url = MediaOrDocumentUrl ( id )
458
+ } ) . ToDictionary ( x => x . Id , x => x . Url ) ;
459
+ }
460
+
461
+ /// <summary>
462
+ /// Get entity URLs by UDIs
463
+ /// </summary>
464
+ /// <param name="udis">
465
+ /// A list of UDIs to lookup items by
466
+ /// </param>
467
+ /// <param name="culture">The culture to fetch the URL for</param>
468
+ /// <returns>Dictionary mapping Udi -> Url</returns>
469
+ /// <remarks>
470
+ /// We allow for POST because there could be quite a lot of Ids.
471
+ /// </remarks>
472
+ [ HttpGet ]
473
+ [ HttpPost ]
474
+ [ Obsolete ( "Use GetUrlsByIds instead." ) ]
475
+ public IDictionary < Udi , string > GetUrlsByUdis ( [ FromJsonPath ] Udi [ ] udis , string culture = null )
476
+ {
477
+ if ( udis == null || ! udis . Any ( ) )
478
+ {
479
+ return new Dictionary < Udi , string > ( ) ;
480
+ }
481
+
482
+ var udiEntityType = udis . First ( ) . EntityType ;
483
+ UmbracoEntityTypes entityType ;
484
+
485
+ switch ( udiEntityType )
486
+ {
487
+ case Constants . UdiEntityType . Document :
488
+ entityType = UmbracoEntityTypes . Document ;
489
+ break ;
490
+ case Constants . UdiEntityType . Media :
491
+ entityType = UmbracoEntityTypes . Media ;
492
+ break ;
493
+ default :
494
+ entityType = ( UmbracoEntityTypes ) ( - 1 ) ;
495
+ break ;
496
+ }
497
+
498
+ return GetUrlsByIds ( udis , entityType , culture ) ;
361
499
}
362
500
363
501
/// <summary>
0 commit comments