Some work on API-side

This commit is contained in:
glax 2023-05-20 17:18:22 +02:00
parent a6a2d20981
commit 1916018fba
2 changed files with 55 additions and 20 deletions

View File

@ -1,20 +1,19 @@
using System.Text.Json; using System.Text.Json;
using Tranga; using Tranga;
using Tranga.Connectors;
TaskManager taskManager = new TaskManager(Directory.GetCurrentDirectory()); TaskManager taskManager = new (Directory.GetCurrentDirectory());
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
var app = builder.Build(); var app = builder.Build();
app.MapGet("/GetConnectors", () => JsonSerializer.Serialize(taskManager.GetAvailableConnectors().Values.ToArray())); app.MapGet("/GetConnectors", () => JsonSerializer.Serialize(taskManager.GetAvailableConnectors().Values.ToArray()));
app.MapGet("/GetPublications", (string connectorName, string? title) => app.MapGet("/GetPublications", (string connectorName, string? publicationName) =>
{ {
Connector connector = taskManager.GetConnector(connectorName); Connector connector = taskManager.GetConnector(connectorName);
Publication[] publications; Publication[] publications;
if (title is not null) if (publicationName is not null)
publications = connector.GetPublications(title); publications = connector.GetPublications(publicationName);
else else
publications = connector.GetPublications(); publications = connector.GetPublications();
@ -24,26 +23,60 @@ app.MapGet("/GetPublications", (string connectorName, string? title) =>
app.MapGet("/ListTasks", () => JsonSerializer.Serialize(taskManager.GetAllTasks())); app.MapGet("/ListTasks", () => JsonSerializer.Serialize(taskManager.GetAllTasks()));
app.MapGet("/CreateTask", app.MapGet("/CreateTask",
(TrangaTask.Task task, string connectorName, string? publicationName, TimeSpan reoccurrence, string language) => (TrangaTask.Task task, string? connectorName, string? publicationName, TimeSpan reoccurrence, string? language) =>
{ {
Publication? publication = switch (task)
taskManager.GetAllPublications().FirstOrDefault(pub => pub.downloadUrl == publicationName); {
if (publication is null) case TrangaTask.Task.UpdateKomgaLibrary:
JsonSerializer.Serialize($"Publication {publicationName} is unknown."); taskManager.AddTask(TrangaTask.Task.UpdateKomgaLibrary, null, null, reoccurrence);
break;
taskManager.AddTask(task, connectorName, publication, reoccurrence, language); case TrangaTask.Task.DownloadNewChapters:
JsonSerializer.Serialize("Success"); try
{
Connector connector = taskManager.GetConnector(connectorName);
Publication[] publications;
if (publicationName is not null)
publications = connector.GetPublications(publicationName);
else
publications = connector.GetPublications();
Publication? publication = publications.FirstOrDefault(pub => pub.downloadUrl == publicationName);
if (publication is null)
JsonSerializer.Serialize($"Publication {publicationName} is unknown.");
taskManager.AddTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication, reoccurrence, language ?? "");
return JsonSerializer.Serialize("Success");
}
catch (Exception e)
{
return JsonSerializer.Serialize(e.Message);
}
default: return JsonSerializer.Serialize("Not Implemented");
}
return JsonSerializer.Serialize("Not Implemented");
}); });
app.MapGet("/RemoveTask", (TrangaTask.Task task, string connector, string? publicationName) => app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string? publicationName) =>
{ {
Publication? publication = switch (task)
taskManager.GetAllPublications().FirstOrDefault(pub => pub.downloadUrl == publicationName); {
if (publication is null) case TrangaTask.Task.UpdateKomgaLibrary:
JsonSerializer.Serialize($"Publication {publicationName} is unknown."); taskManager.RemoveTask(TrangaTask.Task.UpdateKomgaLibrary, null, null);
return JsonSerializer.Serialize("Success");
break;
case TrangaTask.Task.DownloadNewChapters:
Publication? publication = taskManager.GetAllPublications().FirstOrDefault(pub => pub.downloadUrl == publicationName);
if (publication is null)
JsonSerializer.Serialize($"Publication {publicationName} is unknown.");
taskManager.RemoveTask(TrangaTask.Task.DownloadNewChapters, connectorName, publication);
return JsonSerializer.Serialize("Success");
taskManager.RemoveTask(task, connector, publication); default: return JsonSerializer.Serialize("Not Implemented");
JsonSerializer.Serialize("Success"); }
}); });
app.Run(); app.Run();

View File

@ -193,6 +193,8 @@ public class TaskManager
/// <exception cref="Exception">If Connector is not available</exception> /// <exception cref="Exception">If Connector is not available</exception>
public Connector GetConnector(string connectorName) public Connector GetConnector(string connectorName)
{ {
if(connectorName is null)
throw new Exception($"connectorName can not be null");
Connector? ret = this._connectors.FirstOrDefault(connector => connector.name == connectorName); Connector? ret = this._connectors.FirstOrDefault(connector => connector.name == connectorName);
if (ret is null) if (ret is null)
throw new Exception($"Connector {connectorName} is not an available Connector."); throw new Exception($"Connector {connectorName} is not an available Connector.");