JellyGlass/backend/src/Services/LibraryService.cs
2026-02-22 21:58:23 +00:00

144 lines
No EOL
3.1 KiB
C#

using JellyGlass.Models;
using JellyGlass.Models.JellyfinApi;
namespace JellyGlass.Services;
public class LibraryService : ILibraryService
{
private IClientService _clientService;
public LibraryService(IClientService serverService)
{
_clientService = serverService;
}
public async Task<Library[]> GetLibraries()
{
var clients = await _clientService.GetJellyfinClients();
var libraries = new Dictionary<string, Library>();
foreach (var client in clients)
{
var clientLibraries = await client.GetInstanceLibraries();
foreach (var library in clientLibraries)
{
if (library.Name == "Collections" || library.Name == "Playlists")
{
continue;
}
if (!libraries.ContainsKey(library.Name))
{
libraries.Add(library.Name, new Library()
{
Name = library.Name,
ThumbnailUrl = $"{client.InstanceUrl}/Items/{library.Id}/Primary"
});
}
}
}
return libraries.Values.ToArray();
}
public async Task<Item> GetLibrary(string libraryName, string serverId)
{
var client = await _clientService.GetClientForServerId(serverId);
if (client == null)
{
throw new Exception($"Could not find client with ID of {serverId}");
}
var libraries = await client.GetInstanceLibraries();
foreach (var library in libraries)
{
if (library.Name == libraryName)
{
return library;
}
}
throw new Exception("Couldn't find library");
}
public async Task<ItemDTO[]> GetItemsFromLibrary(string libraryName, string serverId)
{
var client = await _clientService.GetClientForServerId(serverId);
var library = await GetLibrary(libraryName, serverId);
var items = await client.GetItemChildren(library.Id);
var dtos = new List<ItemDTO>();
foreach (var item in items)
{
dtos.Add(new ItemDTO(item, client.InstanceUrl));
}
return dtos.ToArray();
}
public async Task<Library[]> GetLibrariesFromServer(string serverId)
{
var client = await _clientService.GetClientForServerId(serverId);
var libraries = new Dictionary<string, Library>();
var clientLibraries = await client.GetInstanceLibraries();
foreach (var library in clientLibraries)
{
if (library.Name == "Collections" || library.Name == "Playlists")
{
continue;
}
if (!libraries.ContainsKey(library.Name))
{
libraries.Add(library.Name, new Library()
{
Name = library.Name,
ThumbnailUrl = $"{client.InstanceUrl}/Items/{library.Id}/Primary"
});
}
}
return libraries.Values.ToArray();
}
// public async Task<ItemDTO[]> GetChildrenFromItems(ItemDTO[] items)
// {
// var children = new List<ItemDTO>();
// foreach (var item in items)
// {
// var client = _serverService.GetClientForServerId(item.ServerID);
// var itemChildren = await client.GetItemChildren(item.ID);
// foreach (var child in itemChildren.Items)
// {
// children.Add(new ItemDTO(child, client.InstanceUrl));
// }
// }
// return children.ToArray();
// }
// public async Task<ItemDTO> GetItemsByName(string name, string itemType)
// {
// }
// public async Task<ItemDTO> GetItemsByType(string itemType)
// {
// }
}