Tranga-Website/Tranga-API/Program.cs

82 lines
3.3 KiB
C#
Raw Normal View History

2023-05-19 17:36:27 +02:00
using System.Text.Json;
using Tranga;
2023-05-20 17:18:22 +02:00
TaskManager taskManager = new (Directory.GetCurrentDirectory());
2023-05-19 17:36:27 +02:00
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/GetConnectors", () => JsonSerializer.Serialize(taskManager.GetAvailableConnectors().Values.ToArray()));
2023-05-20 17:18:22 +02:00
app.MapGet("/GetPublications", (string connectorName, string? publicationName) =>
2023-05-19 17:36:27 +02:00
{
2023-05-20 14:21:47 +02:00
Connector connector = taskManager.GetConnector(connectorName);
2023-05-19 17:36:27 +02:00
Publication[] publications;
2023-05-20 17:18:22 +02:00
if (publicationName is not null)
publications = connector.GetPublications(publicationName);
2023-05-19 17:36:27 +02:00
else
publications = connector.GetPublications();
return JsonSerializer.Serialize(publications);
});
app.MapGet("/ListTasks", () => JsonSerializer.Serialize(taskManager.GetAllTasks()));
app.MapGet("/CreateTask",
2023-05-20 17:18:22 +02:00
(TrangaTask.Task task, string? connectorName, string? publicationName, TimeSpan reoccurrence, string? language) =>
2023-05-19 17:36:27 +02:00
{
2023-05-20 17:18:22 +02:00
switch (task)
{
case TrangaTask.Task.UpdateKomgaLibrary:
taskManager.AddTask(TrangaTask.Task.UpdateKomgaLibrary, null, null, reoccurrence);
break;
case TrangaTask.Task.DownloadNewChapters:
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");
2023-05-19 17:36:27 +02:00
});
2023-05-20 17:18:22 +02:00
app.MapGet("/RemoveTask", (TrangaTask.Task task, string? connectorName, string? publicationName) =>
2023-05-19 17:36:27 +02:00
{
2023-05-20 17:18:22 +02:00
switch (task)
{
case TrangaTask.Task.UpdateKomgaLibrary:
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");
2023-05-19 17:36:27 +02:00
2023-05-20 17:18:22 +02:00
default: return JsonSerializer.Serialize("Not Implemented");
}
2023-05-19 17:36:27 +02:00
});
app.Run();