Skip to content

Commit

Permalink
[postgres-Server-V2] Formatter fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ale-ben committed Jan 12, 2025
1 parent dd1d67a commit 7dc34c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
99 changes: 49 additions & 50 deletions API/Schema/Chapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,83 @@ namespace API.Schema;
[PrimaryKey("ChapterId")]
public class Chapter : IComparable<Chapter>
{
[MaxLength(64)]
public string ChapterId { get; init; } = TokenGen.CreateToken(typeof(Chapter), 64);
public Chapter(Manga parentManga, string url, string chapterNumber, int? volumeNumber = null, string? title = null)
: this(parentManga.MangaId, url, chapterNumber, volumeNumber, title)
{
ParentManga = parentManga;
ArchiveFileName = BuildArchiveFileName();
}

public Chapter(string parentMangaId, string url, string chapterNumber,
int? volumeNumber = null, string? title = null)
{
ParentMangaId = parentMangaId;
Url = url;
ChapterNumber = chapterNumber;
VolumeNumber = volumeNumber;
Title = title;
}

[MaxLength(64)] public string ChapterId { get; init; } = TokenGen.CreateToken(typeof(Chapter), 64);

public int? VolumeNumber { get; private set; }
[MaxLength(10)]
public string ChapterNumber { get; private set; }

[MaxLength(10)] public string ChapterNumber { get; private set; }

public string Url { get; internal set; }
public string? Title { get; private set; }
public string ArchiveFileName { get; private set; }
public bool Downloaded { get; internal set; } = false;

public string ParentMangaId { get; internal set; }
public Manga? ParentManga { get; init; }

public Chapter(Manga parentManga, string url, string chapterNumber, int? volumeNumber = null, string? title = null)
: this(parentManga.MangaId, url, chapterNumber, volumeNumber, title)
{
this.ParentManga = parentManga;
this.ArchiveFileName = BuildArchiveFileName();
}

public Chapter(string parentMangaId, string url, string chapterNumber,
int? volumeNumber = null, string? title = null)
public int CompareTo(Chapter? other)
{
this.ParentMangaId = parentMangaId;
this.Url = url;
this.ChapterNumber = chapterNumber;
this.VolumeNumber = volumeNumber;
this.Title = title;
if (other is not { } otherChapter)
throw new ArgumentException($"{other} can not be compared to {this}");
return VolumeNumber?.CompareTo(otherChapter.VolumeNumber) switch
{
< 0 => -1,
> 0 => 1,
_ => CompareChapterNumbers(ChapterNumber, otherChapter.ChapterNumber)
};
}

public MoveFileOrFolderJob? UpdateChapterNumber(string chapterNumber)
{
this.ChapterNumber = chapterNumber;
ChapterNumber = chapterNumber;
return UpdateArchiveFileName();
}

public MoveFileOrFolderJob? UpdateVolumeNumber(int? volumeNumber)
{
this.VolumeNumber = volumeNumber;
VolumeNumber = volumeNumber;
return UpdateArchiveFileName();
}

public MoveFileOrFolderJob? UpdateTitle(string? title)
{
this.Title = title;
Title = title;
return UpdateArchiveFileName();
}

private string BuildArchiveFileName()
{
return $"{this.ParentManga.Name} - Vol.{this.VolumeNumber ?? 0} Ch.{this.ChapterNumber}{(this.Title is null ? "" : $" - {this.Title}")}.cbz";
return
$"{ParentManga.Name} - Vol.{VolumeNumber ?? 0} Ch.{ChapterNumber}{(Title is null ? "" : $" - {Title}")}.cbz";
}

private MoveFileOrFolderJob? UpdateArchiveFileName()
{
string oldPath = GetArchiveFilePath();
this.ArchiveFileName = BuildArchiveFileName();
if (Downloaded)
{
return new MoveFileOrFolderJob(oldPath, GetArchiveFilePath());
}
ArchiveFileName = BuildArchiveFileName();
if (Downloaded) return new MoveFileOrFolderJob(oldPath, GetArchiveFilePath());
return null;
}

/// <summary>
/// Creates full file path of chapter-archive
/// Creates full file path of chapter-archive
/// </summary>
/// <returns>Filepath</returns>
internal string GetArchiveFilePath()
Expand All @@ -89,11 +100,11 @@ public bool IsDownloaded()

private static int CompareChapterNumbers(string ch1, string ch2)
{
var ch1Arr = ch1.Split('.').Select(c => int.Parse(c)).ToArray();
var ch2Arr = ch2.Split('.').Select(c => int.Parse(c)).ToArray();
int[] ch1Arr = ch1.Split('.').Select(c => int.Parse(c)).ToArray();
int[] ch2Arr = ch2.Split('.').Select(c => int.Parse(c)).ToArray();

int i = 0, j = 0;

while (i < ch1Arr.Length && j < ch2Arr.Length)
{
if (ch1Arr[i] < ch2Arr[j])
Expand All @@ -103,31 +114,19 @@ private static int CompareChapterNumbers(string ch1, string ch2)
i++;
j++;
}

return 0;
}

public int CompareTo(Chapter? other)
{
if(other is not { } otherChapter)
throw new ArgumentException($"{other} can not be compared to {this}");
return this.VolumeNumber?.CompareTo(otherChapter.VolumeNumber) switch
{
<0 => -1,
>0 => 1,
_ => CompareChapterNumbers(this.ChapterNumber, otherChapter.ChapterNumber)
};
}

internal string GetComicInfoXmlString()
{
XElement comicInfo = new XElement("ComicInfo",
XElement comicInfo = new("ComicInfo",
new XElement("Tags", string.Join(',', ParentManga.Tags.Select(tag => tag.Tag))),
new XElement("LanguageISO", ParentManga.OriginalLanguage),
new XElement("Title", this.Title),
new XElement("Title", Title),
new XElement("Writer", string.Join(',', ParentManga.Authors.Select(author => author.AuthorName))),
new XElement("Volume", this.VolumeNumber),
new XElement("Number", this.ChapterNumber)
new XElement("Volume", VolumeNumber),
new XElement("Number", ChapterNumber)
);
return comicInfo.ToString();
}
Expand Down
23 changes: 12 additions & 11 deletions API/Tranga.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using API.Schema;
using API.Schema.Jobs;
using API.Schema.NotificationConnectors;
using log4net;
using log4net.Config;

Expand All @@ -20,9 +21,9 @@ internal static void StartLogger()
private static void NotificationSender(object? pgsqlContext)
{
if (pgsqlContext is null) return;
var context = (PgsqlContext)pgsqlContext;
PgsqlContext context = (PgsqlContext)pgsqlContext;

var staleNotifications =
IQueryable<Notification> staleNotifications =
context.Notifications.Where(n => n.Urgency < NotificationUrgency.Normal);
context.Notifications.RemoveRange(staleNotifications);
context.SaveChanges();
Expand All @@ -39,14 +40,14 @@ private static void NotificationSender(object? pgsqlContext)

private static void SendNotifications(PgsqlContext context, NotificationUrgency urgency)
{
var notifications = context.Notifications.Where(n => n.Urgency == urgency).ToList();
List<Notification> notifications = context.Notifications.Where(n => n.Urgency == urgency).ToList();
if (notifications.Any())
{
var max = notifications.MaxBy(n => n.Date)!.Date;
DateTime max = notifications.MaxBy(n => n.Date)!.Date;
if (DateTime.Now.Subtract(max) > TrangaSettings.NotificationUrgencyDelay(urgency))
{
foreach (var notificationConnector in context.NotificationConnectors)
foreach (var notification in notifications)
foreach (NotificationConnector notificationConnector in context.NotificationConnectors)
foreach (Notification notification in notifications)
notificationConnector.SendNotification(notification.Title, notification.Message);
context.Notifications.RemoveRange(notifications);
}
Expand All @@ -58,15 +59,15 @@ private static void SendNotifications(PgsqlContext context, NotificationUrgency
private static void JobStarter(object? pgsqlContext)
{
if (pgsqlContext is null) return;
var context = (PgsqlContext)pgsqlContext;
PgsqlContext context = (PgsqlContext)pgsqlContext;

var TRANGA =
string TRANGA =
"\n\n _______ \n|_ _|.----..---.-..-----..-----..---.-.\n | | | _|| _ || || _ || _ |\n |___| |__| |___._||__|__||___ ||___._|\n |_____| \n\n";
Log.Info(TRANGA);
while (true)
{
var completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
foreach (var job in completedJobs)
List<Job> completedJobs = context.Jobs.Where(j => j.state == JobState.Completed).ToList();
foreach (Job job in completedJobs)
if (job.RecurrenceMs <= 0)
{
context.Jobs.Remove(job);
Expand All @@ -80,7 +81,7 @@ private static void JobStarter(object? pgsqlContext)

List<Job> runJobs = context.Jobs.Where(j => j.state <= JobState.Running).AsEnumerable()
.Where(j => j.NextExecution < DateTime.UtcNow).ToList();
foreach (var job in runJobs)
foreach (Job job in runJobs)
{
// If the job is already running, skip it
if (RunningJobs.Values.Any(j => j.JobId == job.JobId)) continue;
Expand Down

0 comments on commit 7dc34c1

Please sign in to comment.