mirror of
https://github.com/C9Glax/tranga.git
synced 2025-02-23 07:40:13 +01:00
Remove APISerializable and APIJsonSerializer
This commit is contained in:
parent
77c5903cf1
commit
87274aca19
@ -1,61 +0,0 @@
|
||||
using System.Reflection;
|
||||
using System.Text.Json;
|
||||
using API.Schema;
|
||||
using API.Schema.Jobs;
|
||||
using API.Schema.LibraryConnectors;
|
||||
using API.Schema.NotificationConnectors;
|
||||
using Newtonsoft.Json;
|
||||
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
|
||||
|
||||
namespace API;
|
||||
|
||||
internal class ApiJsonSerializer : System.Text.Json.Serialization.JsonConverter<APISerializable>
|
||||
{
|
||||
|
||||
public override bool CanConvert(Type typeToConvert) => typeToConvert == typeof(Job);
|
||||
|
||||
public override APISerializable? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
Span<char> dest = stackalloc char[1024];
|
||||
string json = "";
|
||||
while (reader.Read())
|
||||
{
|
||||
reader.CopyString(dest);
|
||||
json += dest.ToString();
|
||||
}
|
||||
JsonReader jr = new JsonTextReader(new StringReader(json));
|
||||
return new JobJsonDeserializer().ReadJson(jr, typeToConvert, null, JsonSerializer.Create(new JsonSerializerSettings())) as Job;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, APISerializable value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
foreach (PropertyInfo info in value.GetType().GetProperties())
|
||||
{
|
||||
if(info.PropertyType == typeof(string))
|
||||
writer.WriteString(LowerCamelCase(info.Name), (string)info.GetValue(value)!);
|
||||
else if(info.PropertyType == typeof(bool))
|
||||
writer.WriteBoolean(LowerCamelCase(info.Name), (bool)info.GetValue(value)!);
|
||||
else if(info.PropertyType == typeof(int))
|
||||
writer.WriteNumber(LowerCamelCase(info.Name), (int)info.GetValue(value)!);
|
||||
else if(info.PropertyType == typeof(ulong))
|
||||
writer.WriteNumber(LowerCamelCase(info.Name), (ulong)info.GetValue(value)!);
|
||||
else if(info.PropertyType == typeof(JobType))
|
||||
writer.WriteString(LowerCamelCase(info.Name), Enum.GetName((JobType)info.GetValue(value)!));
|
||||
else if(info.PropertyType == typeof(JobState))
|
||||
writer.WriteString(LowerCamelCase(info.Name), Enum.GetName((JobState)info.GetValue(value)!));
|
||||
else if(info.PropertyType == typeof(NotificationConnectorType))
|
||||
writer.WriteString(LowerCamelCase(info.Name), Enum.GetName((NotificationConnectorType)info.GetValue(value)!));
|
||||
else if(info.PropertyType == typeof(LibraryType))
|
||||
writer.WriteString(LowerCamelCase(info.Name), Enum.GetName((LibraryType)info.GetValue(value)!));
|
||||
else if(info.PropertyType == typeof(DateTime))
|
||||
writer.WriteString(LowerCamelCase(info.Name), ((DateTime)info.GetValue(value)!).ToUniversalTime().ToString("u").Replace(' ','T'));
|
||||
}
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
private static string LowerCamelCase(string s)
|
||||
{
|
||||
return char.ToLowerInvariant(s[0]) + s.Substring(1);
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER $APP_UID
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["API/API.csproj", "API/"]
|
||||
RUN dotnet restore "API/API.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/API"
|
||||
RUN dotnet build "API.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "API.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "API.dll"]
|
@ -28,7 +28,6 @@ builder.Services.AddCors(options =>
|
||||
builder.Services.AddMvc().AddJsonOptions(opts =>
|
||||
{
|
||||
opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
opts.JsonSerializerOptions.Converters.Add(new ApiJsonSerializer());
|
||||
});
|
||||
|
||||
builder.Services.AddApiVersioning(option =>
|
||||
|
@ -1,3 +0,0 @@
|
||||
namespace API.Schema;
|
||||
|
||||
public interface APISerializable;
|
@ -1,5 +1,4 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Schema;
|
||||
|
@ -6,7 +6,7 @@ using Newtonsoft.Json;
|
||||
namespace API.Schema.Jobs;
|
||||
|
||||
[PrimaryKey("JobId")]
|
||||
public abstract class Job : APISerializable
|
||||
public abstract class Job
|
||||
{
|
||||
[MaxLength(64)]
|
||||
public string JobId { get; init; }
|
||||
|
@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
namespace API.Schema.LibraryConnectors;
|
||||
|
||||
[PrimaryKey("LibraryConnectorId")]
|
||||
public abstract class LibraryConnector(string libraryConnectorId, LibraryType libraryType, string baseUrl, string auth) : APISerializable
|
||||
public abstract class LibraryConnector(string libraryConnectorId, LibraryType libraryType, string baseUrl, string auth)
|
||||
{
|
||||
[MaxLength(64)]
|
||||
public string LibraryConnectorId { get; } = libraryConnectorId;
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Schema;
|
||||
@ -11,7 +10,4 @@ public class Link(string linkProvider, string linkUrl)
|
||||
public string LinkId { get; init; } = TokenGen.CreateToken(typeof(Link), 64);
|
||||
public string LinkProvider { get; init; } = linkProvider;
|
||||
public string LinkUrl { get; init; } = linkUrl;
|
||||
|
||||
[ForeignKey("MangaId")]
|
||||
public virtual Manga Manga { get; init; }
|
||||
}
|
@ -71,9 +71,6 @@ public class Manga(
|
||||
[ForeignKey("AltTitleIds")]
|
||||
public virtual MangaAltTitle[] AltTitles { get; }
|
||||
|
||||
[ForeignKey("ChapterIds")]
|
||||
public virtual Chapter[] Chapters { get; internal set; }
|
||||
|
||||
public MoveFileOrFolderJob UpdateFolderName(string downloadLocation, string newName)
|
||||
{
|
||||
string oldName = this.FolderName;
|
||||
|
@ -12,7 +12,4 @@ public class MangaAltTitle(string language, string title)
|
||||
[MaxLength(8)]
|
||||
public string Language { get; init; } = language;
|
||||
public string Title { get; set; } = title;
|
||||
|
||||
[ForeignKey("MangaId")]
|
||||
public virtual Manga Manga { get; init; }
|
||||
}
|
@ -14,11 +14,6 @@ public abstract class MangaConnector(string name, string[] supportedLanguages, s
|
||||
public string[] SupportedLanguages { get; init; } = supportedLanguages;
|
||||
public string[] BaseUris { get; init; } = baseUris;
|
||||
|
||||
[JsonIgnore]
|
||||
[ForeignKey("MangaIds")]
|
||||
public virtual Manga[] Mangas { get; internal set; } = [];
|
||||
|
||||
|
||||
public abstract (Manga, Author[], MangaTag[], Link[], MangaAltTitle[])[] GetManga(string publicationTitle = "");
|
||||
|
||||
public abstract (Manga, Author[], MangaTag[], Link[], MangaAltTitle[])? GetMangaFromUrl(string url);
|
||||
|
@ -7,7 +7,4 @@ namespace API.Schema;
|
||||
public class MangaTag(string tag)
|
||||
{
|
||||
public string Tag { get; init; } = tag;
|
||||
|
||||
[ForeignKey("MangaIds")]
|
||||
public virtual Manga[] Mangas { get; internal set; } = [];
|
||||
}
|
@ -6,7 +6,7 @@ using Newtonsoft.Json;
|
||||
namespace API.Schema.NotificationConnectors;
|
||||
|
||||
[PrimaryKey("NotificationConnectorId")]
|
||||
public abstract class NotificationConnector(string notificationConnectorId, NotificationConnectorType notificationConnectorType) : APISerializable
|
||||
public abstract class NotificationConnector(string notificationConnectorId, NotificationConnectorType notificationConnectorType)
|
||||
{
|
||||
[MaxLength(64)]
|
||||
public string NotificationConnectorId { get; } = notificationConnectorId;
|
||||
|
@ -52,9 +52,7 @@ public class PgsqlContext(DbContextOptions<PgsqlContext> options) : DbContext(op
|
||||
.HasValue<UpdateMetadataJob>(JobType.UpdateMetaDataJob);
|
||||
|
||||
modelBuilder.Entity<Chapter>()
|
||||
.HasOne<Manga>(c => c.ParentManga)
|
||||
.WithMany(m => m.Chapters)
|
||||
.HasForeignKey(c => c.ParentMangaId);
|
||||
.HasOne<Manga>(c => c.ParentManga);
|
||||
|
||||
modelBuilder.Entity<Manga>()
|
||||
.HasOne<Chapter>(m => m.LatestChapterAvailable)
|
||||
@ -63,30 +61,14 @@ public class PgsqlContext(DbContextOptions<PgsqlContext> options) : DbContext(op
|
||||
.HasOne<Chapter>(m => m.LatestChapterDownloaded)
|
||||
.WithOne();
|
||||
modelBuilder.Entity<Manga>()
|
||||
.HasOne<MangaConnector>(m => m.MangaConnector)
|
||||
.WithMany(c => c.Mangas)
|
||||
.HasForeignKey(m => m.MangaConnectorName);
|
||||
.HasOne<MangaConnector>(m => m.MangaConnector);
|
||||
modelBuilder.Entity<Manga>()
|
||||
.HasMany<Author>(m => m.Authors)
|
||||
.WithMany(a => a.Mangas)
|
||||
.UsingEntity(
|
||||
"MangaAuthor",
|
||||
l => l.HasOne(typeof(Manga)).WithMany().HasForeignKey("MangaId").HasPrincipalKey("MangaId"),
|
||||
r => r.HasOne(typeof(Author)).WithMany().HasForeignKey("AuthorId").HasPrincipalKey("AuthorId"),
|
||||
j => j.HasKey("MangaId", "AuthorId"));
|
||||
.HasMany<Author>(m => m.Authors);
|
||||
modelBuilder.Entity<Manga>()
|
||||
.HasMany<MangaTag>(m => m.Tags)
|
||||
.WithMany(t => t.Mangas)
|
||||
.UsingEntity(
|
||||
"MangaTag",
|
||||
l => l.HasOne(typeof(Manga)).WithMany().HasForeignKey("MangaId").HasPrincipalKey("MangaId"),
|
||||
r => r.HasOne(typeof(MangaTag)).WithMany().HasForeignKey("Tag").HasPrincipalKey("Tag"),
|
||||
j => j.HasKey("MangaId", "Tag"));
|
||||
.HasMany<MangaTag>(m => m.Tags);
|
||||
modelBuilder.Entity<Manga>()
|
||||
.HasMany<Link>(m => m.Links)
|
||||
.WithOne(c => c.Manga);
|
||||
.HasMany<Link>(m => m.Links);
|
||||
modelBuilder.Entity<Manga>()
|
||||
.HasMany<MangaAltTitle>(m => m.AltTitles)
|
||||
.WithOne(c => c.Manga);
|
||||
.HasMany<MangaAltTitle>(m => m.AltTitles);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user