Skip to content

Commit

Permalink
✨ Implemented endpoints
Browse files Browse the repository at this point in the history
API #1
  • Loading branch information
LNA-DEV committed Sep 27, 2024
1 parent b6f8b6c commit 2e2eb9c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
34 changes: 29 additions & 5 deletions OpenMediaServer/Endpoints/ApiEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,39 @@ public async Task<IResult> ListCategories()

public async Task<IResult> 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<IResult> GetItem(string category, string id)
public async Task<IResult> 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");
}
}
}
2 changes: 1 addition & 1 deletion OpenMediaServer/Interfaces/Services/IInventoryService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public interface IInventoryService
public void CreateFromPaths(IEnumerable<string> paths);
public Task<IEnumerable<string>> ListCategories();
public Task<IEnumerable<InventoryItem>> ListItems(string category);
public Task<InventoryItem> GetItem(string id, string category);
public Task<InventoryItem> GetItem(Guid id, string category);
}
25 changes: 21 additions & 4 deletions OpenMediaServer/Services/InventoryService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text.Json;
using OpenMediaServer.Interfaces.APIs;
using OpenMediaServer.Interfaces.Repositories;
using OpenMediaServer.Interfaces.Services;
Expand All @@ -23,17 +24,33 @@ public InventoryService(ILogger<InventoryService> logger, IStorageRepository sto

public async Task<IEnumerable<string>> ListCategories()
{
return new List<string> { }; // 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<IEnumerable<InventoryItem>> ListItems(string category)
{
return new List<InventoryItem> { }; // TODO Implement
var text = await File.ReadAllTextAsync(Path.Combine(Globals.ConfigFolder, "inventory", category) + ".json");
var items = JsonSerializer.Deserialize<IEnumerable<InventoryItem>>(text);

return items;
}

public async Task<InventoryItem> GetItem(string id, string category)
public async Task<InventoryItem> 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<IEnumerable<InventoryItem>>(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<string> paths)
Expand Down

0 comments on commit 2e2eb9c

Please sign in to comment.