Speed up all albums endpoint

pull/6/head
ta264 4 years ago
parent 0a33fd98cc
commit 309b8f82e1

@ -29,10 +29,12 @@ namespace Lidarr.Api.V1.Albums
IHandle<TrackImportedEvent>,
IHandle<TrackFileDeletedEvent>
{
protected readonly IArtistService _artistService;
protected readonly IReleaseService _releaseService;
protected readonly IAddAlbumService _addAlbumService;
public AlbumModule(IAlbumService albumService,
public AlbumModule(IArtistService artistService,
IAlbumService albumService,
IAddAlbumService addAlbumService,
IReleaseService releaseService,
IArtistStatisticsService artistStatisticsService,
@ -44,6 +46,7 @@ namespace Lidarr.Api.V1.Albums
: base(albumService, artistStatisticsService, coverMapper, upgradableSpecification, signalRBroadcaster)
{
_artistService = artistService;
_releaseService = releaseService;
_addAlbumService = addAlbumService;
@ -69,7 +72,25 @@ namespace Lidarr.Api.V1.Albums
if (!Request.Query.ArtistId.HasValue && !albumIdsQuery.HasValue && !foreignIdQuery.HasValue)
{
return MapToResource(_albumService.GetAllAlbums(), false);
var albums = _albumService.GetAllAlbums();
var artists = _artistService.GetAllArtists().ToDictionary(x => x.ArtistMetadataId);
var releases = _releaseService.GetAllReleases().GroupBy(x => x.AlbumId).ToDictionary(x => x.Key, y => y.ToList());
foreach (var album in albums)
{
album.Artist = artists[album.ArtistMetadataId];
if (releases.TryGetValue(album.Id, out var albumReleases))
{
album.AlbumReleases = albumReleases;
}
else
{
album.AlbumReleases = new List<AlbumRelease>();
}
}
return MapToResource(albums, false);
}
if (artistIdQuery.HasValue)

@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Music.Events;
@ -8,6 +9,7 @@ namespace NzbDrone.Core.Music
{
AlbumRelease GetRelease(int id);
AlbumRelease GetReleaseByForeignReleaseId(string foreignReleaseId, bool checkRedirect = false);
List<AlbumRelease> GetAllReleases();
void InsertMany(List<AlbumRelease> releases);
void UpdateMany(List<AlbumRelease> releases);
void DeleteMany(List<AlbumRelease> releases);
@ -40,6 +42,11 @@ namespace NzbDrone.Core.Music
return _releaseRepository.FindByForeignReleaseId(foreignReleaseId, checkRedirect);
}
public List<AlbumRelease> GetAllReleases()
{
return _releaseRepository.All().ToList();
}
public void InsertMany(List<AlbumRelease> releases)
{
_releaseRepository.InsertMany(releases);

Loading…
Cancel
Save