diff --git a/OpenMediaServer/Endpoints/ApiEndpoints.cs b/OpenMediaServer/Endpoints/ApiEndpoints.cs index 15ebb0a..5e0185a 100644 --- a/OpenMediaServer/Endpoints/ApiEndpoints.cs +++ b/OpenMediaServer/Endpoints/ApiEndpoints.cs @@ -33,15 +33,39 @@ public async Task ListCategories() public async Task ListItems(string category) { - var items = await _inventoryService.ListItems(category); + try + { + var items = await _inventoryService.ListItems(category); - return Results.Ok(items); + return Results.Ok(items); + } + catch (FileNotFoundException fileEx) + { + _logger.LogWarning(fileEx, "Category could not be found"); + + return Results.NotFound("Category not found"); + } } - public async Task GetItem(string category, string id) + public async Task GetItem(string category, Guid id) { - var item = await _inventoryService.GetItem(id: id, category: category); + try + { + var item = await _inventoryService.GetItem(id: id, category: category); + + return Results.Ok(item); + } + catch (ArgumentException argEx) + { + _logger.LogWarning(argEx, "Id could not be found in category"); + + return Results.NotFound("Id not found in category"); + } + catch (FileNotFoundException fileEx) + { + _logger.LogWarning(fileEx, "Category could not be found to retrieve id"); - return Results.Ok(item); + return Results.NotFound("Category not found"); + } } } diff --git a/OpenMediaServer/Interfaces/Services/IInventoryService.cs b/OpenMediaServer/Interfaces/Services/IInventoryService.cs index ce96096..0afe34e 100644 --- a/OpenMediaServer/Interfaces/Services/IInventoryService.cs +++ b/OpenMediaServer/Interfaces/Services/IInventoryService.cs @@ -10,5 +10,5 @@ public interface IInventoryService public void CreateFromPaths(IEnumerable paths); public Task> ListCategories(); public Task> ListItems(string category); - public Task GetItem(string id, string category); + public Task GetItem(Guid id, string category); } diff --git a/OpenMediaServer/Services/InventoryService.cs b/OpenMediaServer/Services/InventoryService.cs index 6ca0159..1df595d 100644 --- a/OpenMediaServer/Services/InventoryService.cs +++ b/OpenMediaServer/Services/InventoryService.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json; using OpenMediaServer.Interfaces.APIs; using OpenMediaServer.Interfaces.Repositories; using OpenMediaServer.Interfaces.Services; @@ -23,17 +24,33 @@ public InventoryService(ILogger logger, IStorageRepository sto public async Task> ListCategories() { - return new List { }; // TODO Implement + var files = Directory.EnumerateFiles(Path.Join(Globals.ConfigFolder, "inventory")); + var fileNames = files.Select(i => i.Split("/").Last().Replace(".json", "")); + + return fileNames; } public async Task> ListItems(string category) { - return new List { }; // TODO Implement + var text = await File.ReadAllTextAsync(Path.Combine(Globals.ConfigFolder, "inventory", category) + ".json"); + var items = JsonSerializer.Deserialize>(text); + + return items; } - public async Task GetItem(string id, string category) + public async Task GetItem(Guid id, string category) { - return new InventoryItem(); // TODO Implement + var text = await File.ReadAllTextAsync(Path.Combine(Globals.ConfigFolder, "inventory", category) + ".json"); + var items = JsonSerializer.Deserialize>(text); + var possibleItems = items?.Where(i => i.Id == id); + + if (possibleItems == null || possibleItems.Count() != 1) + { + _logger.LogDebug("PossibleItems count in GetItem: {ItemCount}", possibleItems?.Count()); + throw new ArgumentException("No id found in category"); + } + + return possibleItems.First(); } public async void CreateFromPaths(IEnumerable paths)