Working search

This commit is contained in:
Fishandchips321 2026-02-22 21:58:23 +00:00
parent 271cf1f407
commit 2a572e8bc4
15 changed files with 217 additions and 39 deletions

View file

@ -25,6 +25,24 @@ public class ClientService : IClientService
return _clients;
}
public async Task<JellyfinApiClient> GetClientForServerId(string serverId)
{
if (!_clients.Any())
{
await LoadClients();
}
foreach (var client in _clients)
{
if (client.ID == serverId)
{
return client;
}
}
throw new Exception($"Client with ID {serverId} not found");
}
private async Task LoadClients()
{
var servers = await _repository.GetServers();

View file

@ -6,5 +6,5 @@ public interface IClientService
{
public Task<JellyfinApiClient[]> GetJellyfinClients();
// public JellyfinApiClient GetClientForServer(string url);
// public JellyfinApiClient GetClientForServerId(string serverId);
public Task<JellyfinApiClient> GetClientForServerId(string serverId);
}

View file

@ -5,7 +5,9 @@ namespace JellyGlass.Services;
public interface ILibraryService
{
public Task<Library[]> GetLibraries();
public Task<ItemDTO[]> GetItemsFromLibrary(string libraryName);
public Task<ItemDTO[]> GetItemsFromLibrary(string libraryName, string serverId);
public Task<Library[]> GetLibrariesFromServer(string serverId);
// public Task<ItemDTO[]> GetChildrenFromItems(ItemDTO[] items);

View file

@ -0,0 +1,8 @@
using JellyGlass.Models;
namespace JellyGlass.Services;
public interface ISearchService
{
public Task<ItemDTO[]> Search(string searchTerm, string serverId);
}

View file

@ -1,19 +1,20 @@
using JellyGlass.Models;
using JellyGlass.Models.JellyfinApi;
namespace JellyGlass.Services;
public class LibraryService : ILibraryService
{
private IClientService _serverService;
private IClientService _clientService;
public LibraryService(IClientService serverService)
{
_serverService = serverService;
_clientService = serverService;
}
public async Task<Library[]> GetLibraries()
{
var clients = await _serverService.GetJellyfinClients();
var clients = await _clientService.GetJellyfinClients();
var libraries = new Dictionary<string, Library>();
@ -21,7 +22,7 @@ public class LibraryService : ILibraryService
{
var clientLibraries = await client.GetInstanceLibraries();
foreach (var library in clientLibraries.Items)
foreach (var library in clientLibraries)
{
if (library.Name == "Collections" || library.Name == "Playlists")
{
@ -43,9 +44,73 @@ public class LibraryService : ILibraryService
return libraries.Values.ToArray();
}
public async Task<ItemDTO[]> GetItemsFromLibrary(string libraryName)
public async Task<Item> GetLibrary(string libraryName, string serverId)
{
throw new NotImplementedException();
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)

View file

@ -0,0 +1,65 @@
using JellyGlass.Models;
using JellyGlass.Models.JellyfinApi;
namespace JellyGlass.Services;
public class SearchService : ISearchService
{
private ILibraryService _libraryService;
private IClientService _clientService;
public SearchService(ILibraryService libraryService, IClientService clientService)
{
_libraryService = libraryService;
_clientService = clientService;
}
public async Task<ItemDTO[]> Search(string searchTerm, string serverId)
{
var client = await _clientService.GetClientForServerId(serverId);
var items = await client.GetItems(searchTerm: searchTerm);
var dtos = new List<ItemDTO>();
foreach (var item in items)
{
dtos.Add(new ItemDTO(item, client.InstanceUrl));
}
return dtos.ToArray();
}
public async Task<ItemDTO[]> Search2(string searchTerm, string serverId)
{
var libraries = await _libraryService.GetLibrariesFromServer(serverId);
var foundItems = new List<ItemDTO>();
foreach (var library in libraries)
{
var found = await SearchLibraryForTerm(searchTerm, serverId, library);
foundItems.AddRange(found);
}
return foundItems.ToArray();
}
private async Task<ItemDTO[]> SearchLibraryForTerm(string searchTerm, string serverId, Library library)
{
var items = await _libraryService.GetItemsFromLibrary(library.Name, serverId);
var foundItems = new List<ItemDTO>();
foreach (var item in items)
{
if (item.Name.Contains(searchTerm, StringComparison.CurrentCultureIgnoreCase))
{
foundItems.Add(item);
}
}
return foundItems.ToArray();
}
}