Skip to content

Commit

Permalink
[CastIt.Server] Minor improvement while loading the playlist files
Browse files Browse the repository at this point in the history
  • Loading branch information
Efrain Bastidas Berrios committed Sep 18, 2023
1 parent dde9261 commit 7d79f03
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions CastIt.Server/Interfaces/IAppDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IAppDataService
Task DeletePlayList(long id);
Task DeletePlayLists(List<long> ids);
Task<List<FileItem>> GetAllFiles(long playlistId);
Task<List<FileItem>> GetAllFilesByPlayListIds(params long[] playListIds);
Task<FileItem> GetFile(long id);
Task<FileItem> AddFile(
long playListId,
Expand Down
5 changes: 5 additions & 0 deletions CastIt.Server/Services/AppDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public Task<List<FileItem>> GetAllFiles(long playlistId)
return _db.Select<FileItem>().Where(pl => pl.PlayListId == playlistId).ToListAsync();
}

public Task<List<FileItem>> GetAllFilesByPlayListIds(params long[] playListIds)
{
return _db.Select<FileItem>().Where(pl => playListIds.Contains(pl.PlayListId)).ToListAsync();
}

public Task<FileItem> GetFile(long id)
{
return _db.Select<FileItem>().Where(f => f.Id == id).FirstAsync();
Expand Down
30 changes: 17 additions & 13 deletions CastIt.Server/Services/ServerCastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,24 @@ public async Task Init()
_logger.LogInformation($"{nameof(Init)}: Initializing all...");
await _appDataService.DeleteOldTinyCodes();
var playLists = await _appDataService.GetAllPlayLists();
var tasks = playLists.ConvertAll(async pl =>
{
var files = await _appDataService.GetAllFiles(pl.Id);
var mapped = _mapper.Map<ServerPlayList>(pl);
mapped.Files = files
.Select(f => ServerFileItem.From(_fileService, f))
.OrderBy(f => f.Position)
.ToList();
return mapped;
});

var mappedPlayLists = await Task.WhenAll(tasks);
long[] playListIds = playLists.Select(pl => pl.Id).ToArray();
if (playListIds.Any())
{
var files = await _appDataService.GetAllFilesByPlayListIds(playListIds);
var mappedPlayLists = new List<ServerPlayList>();
foreach (var pl in playLists)
{
var mapped = _mapper.Map<ServerPlayList>(pl);
mapped.Files = files
.Where(f => f.PlayListId == pl.Id)
.Select(f => ServerFileItem.From(_fileService, f))
.OrderBy(f => f.Position)
.ToList();
mappedPlayLists.Add(mapped);
}

PlayLists.AddRange(mappedPlayLists.OrderBy(pl => pl.Position));
PlayLists.AddRange(mappedPlayLists.OrderBy(pl => pl.Position));
}

_player.FileLoading += FileLoading;
_player.FileLoaded += FileLoaded;
Expand Down

0 comments on commit 7d79f03

Please sign in to comment.