Compare commits

..

5 Commits

Author SHA1 Message Date
max
4bee6e6d35 [CHANGE] Remove service 2025-09-02 20:55:25 +02:00
max
a8b0291ebf [DB] Update migrations for datetime update 2025-09-02 16:17:47 +02:00
max
d98a99d145 [DB] Auto update datetimes on entities 2025-09-02 16:01:07 +02:00
max
c30e503642 [DB] Update initial db 2025-09-02 15:37:28 +02:00
max
dbf6938a7e [CHANGE] Preparing client init 2025-09-02 15:17:14 +02:00
12 changed files with 234 additions and 66 deletions

View File

@@ -31,6 +31,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Library\" />
<Folder Include="Logs\Debug\" />
</ItemGroup>

View File

@@ -1,3 +1,6 @@
using System.Net;
using DotBased.Monads;
using Manager.Data.Entities.LibraryContext;
using Manager.YouTube;
namespace Manager.App.Services.System;
@@ -5,11 +8,42 @@ namespace Manager.App.Services.System;
public class ClientManager : BackgroundService
{
private readonly List<YouTubeClient> _clients = [];
private CancellationToken _cancellationToken;
protected override Task ExecuteAsync(CancellationToken stoppingToken)
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
throw new NotImplementedException();
_cancellationToken = stoppingToken;
stoppingToken.Register(CancellationRequested);
}
private void CancellationRequested()
{
// Clear up
}
public async Task<Result<YouTubeClient>> LoadClient(ClientAccountEntity accountEntity)
{
if (_cancellationToken.IsCancellationRequested)
{
return ResultError.Fail("Service is shutting down.");
}
var container = new CookieContainer();
if (accountEntity.HttpCookies.Count != 0)
{
var cookieColl = new CookieCollection();
foreach (var cookieEntity in accountEntity.HttpCookies)
{
cookieColl.Add(new Cookie(cookieEntity.Name, cookieEntity.Value, cookieEntity.Domain));
}
container.Add(cookieColl);
}
var ytClient = new YouTubeClient(container, accountEntity.UserAgent ?? "");
await ytClient.GetStateAsync();
return ytClient;
}
}

View File

@@ -1,3 +1,4 @@
using Manager.Data.Entities;
using Manager.Data.Entities.LibraryContext;
using Manager.Data.Entities.LibraryContext.Join;
using Microsoft.EntityFrameworkCore;
@@ -10,6 +11,7 @@ public sealed class LibraryDbContext : DbContext
{
ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
ChangeTracker.LazyLoadingEnabled = false;
Database.EnsureCreated();
}
public DbSet<CaptionEntity> Captions { get; set; }
@@ -32,16 +34,21 @@ public sealed class LibraryDbContext : DbContext
{
channel.ToTable("channels");
channel.HasKey(x => x.Id);
//TODO: Link media from channel
channel.HasMany(x => x.Media)
.WithOne()
.HasForeignKey(x => x.ChannelId);
channel.HasMany(x => x.Playlists)
.WithOne()
.HasForeignKey(x => x.ChannelId);
channel.HasOne(x => x.ClientAccount)
.WithOne()
.HasForeignKey<ClientAccountEntity>(e => e.Id);
});
modelBuilder.Entity<ClientAccountEntity>(cae =>
{
cae.ToTable("client_accounts");
cae.HasKey(x => x.Id);
cae.HasMany(x => x.Playlists)
.WithOne()
.HasForeignKey(x => x.ChannelId);
cae.HasMany(x => x.HttpCookies)
.WithOne()
.HasForeignKey(x => x.ClientId);
@@ -94,4 +101,31 @@ public sealed class LibraryDbContext : DbContext
base.OnModelCreating(modelBuilder);
}
public override int SaveChanges()
{
UpdateEntryDates();
return base.SaveChanges();
}
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new())
{
UpdateEntryDates();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
private void UpdateEntryDates()
{
var entries = ChangeTracker.Entries().Where(x => x is { Entity: DateTimeBase, State: EntityState.Added or EntityState.Modified });
foreach (var entity in entries)
{
((DateTimeBase)entity.Entity).LastModifiedUtc = DateTime.UtcNow;
if (entity.State == EntityState.Added)
{
((DateTimeBase)entity.Entity).CreatedAtUtc = DateTime.UtcNow;
}
}
}
}

View File

@@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations;
namespace Manager.Data.Entities.LibraryContext;
public class CaptionEntity
public class CaptionEntity : DateTimeBase
{
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public required string MediaId { get; set; }

View File

@@ -14,4 +14,7 @@ public class ChannelEntity : DateTimeBase
public long Subscribers { get; set; }
public long TotalVideos { get; set; }
public long TotalViews { get; set; }
public List<MediaEntity> Media { get; set; } = [];
public List<PlaylistEntity> Playlists { get; set; } = [];
public ClientAccountEntity? ClientAccount { get; set; }
}

View File

@@ -6,8 +6,7 @@ public class ClientAccountEntity : DateTimeBase
{
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public required string Id { get; set; }
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public string Name { get; set; } = "";
public List<PlaylistEntity> Playlists { get; set; } = [];
public List<HttpCookieEntity> HttpCookies { get; set; } = [];
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public string? UserAgent { get; set; }
}

View File

@@ -2,13 +2,10 @@ using System.ComponentModel.DataAnnotations;
namespace Manager.Data.Entities.LibraryContext.Join;
public class PlaylistMedia
public class PlaylistMedia : DateTimeBase
{
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public required string PlaylistId { get; set; }
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public required string MediaId { get; set; }
public DateTime DateAddedUtc { get; set; }
public DateTime DateModifiedUtc { get; set; }
}

View File

@@ -2,7 +2,7 @@ using System.ComponentModel.DataAnnotations;
namespace Manager.Data.Entities.LibraryContext;
public class MediaFormatEntity
public class MediaFormatEntity : DateTimeBase
{
[MaxLength(DataConstants.DbContext.DefaultDbStringSize)]
public required string MediaId { get; set; }

View File

@@ -11,8 +11,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace Manager.Data.Migrations
{
[DbContext(typeof(LibraryDbContext))]
[Migration("20250831150411_initialLib")]
partial class initialLib
[Migration("20250902141251_InitialLibrary")]
partial class InitialLibrary
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -30,6 +30,12 @@ namespace Manager.Data.Migrations
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("TEXT");
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
@@ -89,8 +95,7 @@ namespace Manager.Data.Migrations
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
b.Property<string>("UserAgent")
.HasMaxLength(100)
.HasColumnType("TEXT");
@@ -158,10 +163,10 @@ namespace Manager.Data.Migrations
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<DateTime>("DateAddedUtc")
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("TEXT");
b.Property<DateTime>("DateModifiedUtc")
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.HasKey("PlaylistId", "MediaId");
@@ -210,6 +215,8 @@ namespace Manager.Data.Migrations
b.HasKey("Id");
b.HasIndex("ChannelId");
b.ToTable("media", (string)null);
});
@@ -241,6 +248,9 @@ namespace Manager.Data.Migrations
b.Property<long>("ContentLengthBytes")
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("TEXT");
b.Property<double?>("Framerate")
.HasColumnType("REAL");
@@ -253,6 +263,9 @@ namespace Manager.Data.Migrations
b.Property<long>("LastModifiedUnixEpoch")
.HasColumnType("INTEGER");
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.Property<double?>("LoudnessDb")
.HasColumnType("REAL");
@@ -318,6 +331,15 @@ namespace Manager.Data.Migrations
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.ClientAccountEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ChannelEntity", null)
.WithOne("ClientAccount")
.HasForeignKey("Manager.Data.Entities.LibraryContext.ClientAccountEntity", "Id")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.HttpCookieEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ClientAccountEntity", null)
@@ -342,6 +364,15 @@ namespace Manager.Data.Migrations
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.MediaEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ChannelEntity", null)
.WithMany("Media")
.HasForeignKey("ChannelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.MediaFormatEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.MediaEntity", null)
@@ -353,18 +384,25 @@ namespace Manager.Data.Migrations
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.PlaylistEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ClientAccountEntity", null)
b.HasOne("Manager.Data.Entities.LibraryContext.ChannelEntity", null)
.WithMany("Playlists")
.HasForeignKey("ChannelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.ChannelEntity", b =>
{
b.Navigation("ClientAccount");
b.Navigation("Media");
b.Navigation("Playlists");
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.ClientAccountEntity", b =>
{
b.Navigation("HttpCookies");
b.Navigation("Playlists");
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.MediaEntity", b =>

View File

@@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Manager.Data.Migrations
{
/// <inheritdoc />
public partial class initialLib : Migration
public partial class InitialLibrary : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
@@ -35,13 +35,19 @@ namespace Manager.Data.Migrations
columns: table => new
{
Id = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
UserAgent = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
LastModifiedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_client_accounts", x => x.Id);
table.ForeignKey(
name: "FK_client_accounts_channels_Id",
column: x => x.Id,
principalTable: "channels",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
@@ -62,6 +68,34 @@ namespace Manager.Data.Migrations
constraints: table =>
{
table.PrimaryKey("PK_media", x => x.Id);
table.ForeignKey(
name: "FK_media_channels_ChannelId",
column: x => x.ChannelId,
principalTable: "channels",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "playlists",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
Description = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true),
ChannelId = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
LastModifiedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_playlists", x => x.Id);
table.ForeignKey(
name: "FK_playlists_channels_ChannelId",
column: x => x.ChannelId,
principalTable: "channels",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
@@ -91,35 +125,15 @@ namespace Manager.Data.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "playlists",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
Description = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true),
ChannelId = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
LastModifiedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_playlists", x => x.Id);
table.ForeignKey(
name: "FK_playlists_client_accounts_ChannelId",
column: x => x.ChannelId,
principalTable: "client_accounts",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "captions",
columns: table => new
{
MediaId = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
LanguageCode = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false)
Name = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
LastModifiedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
@@ -152,7 +166,9 @@ namespace Manager.Data.Migrations
QualityLabel = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
AudioChannels = table.Column<int>(type: "INTEGER", nullable: true),
AudioSampleRate = table.Column<string>(type: "TEXT", maxLength: 100, nullable: true),
LoudnessDb = table.Column<double>(type: "REAL", nullable: true)
LoudnessDb = table.Column<double>(type: "REAL", nullable: true),
CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
LastModifiedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
@@ -171,8 +187,8 @@ namespace Manager.Data.Migrations
{
PlaylistId = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
MediaId = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
DateAddedUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
DateModifiedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
CreatedAtUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
LastModifiedUtc = table.Column<DateTime>(type: "TEXT", nullable: false)
},
constraints: table =>
{
@@ -201,6 +217,11 @@ namespace Manager.Data.Migrations
table: "join_playlist_media",
column: "MediaId");
migrationBuilder.CreateIndex(
name: "IX_media_ChannelId",
table: "media",
column: "ChannelId");
migrationBuilder.CreateIndex(
name: "IX_playlists_ChannelId",
table: "playlists",
@@ -213,9 +234,6 @@ namespace Manager.Data.Migrations
migrationBuilder.DropTable(
name: "captions");
migrationBuilder.DropTable(
name: "channels");
migrationBuilder.DropTable(
name: "http_cookies");
@@ -225,6 +243,9 @@ namespace Manager.Data.Migrations
migrationBuilder.DropTable(
name: "media_formats");
migrationBuilder.DropTable(
name: "client_accounts");
migrationBuilder.DropTable(
name: "playlists");
@@ -232,7 +253,7 @@ namespace Manager.Data.Migrations
name: "media");
migrationBuilder.DropTable(
name: "client_accounts");
name: "channels");
}
}
}

View File

@@ -27,6 +27,12 @@ namespace Manager.Data.Migrations
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("TEXT");
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
@@ -86,8 +92,7 @@ namespace Manager.Data.Migrations
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
b.Property<string>("UserAgent")
.HasMaxLength(100)
.HasColumnType("TEXT");
@@ -155,10 +160,10 @@ namespace Manager.Data.Migrations
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<DateTime>("DateAddedUtc")
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("TEXT");
b.Property<DateTime>("DateModifiedUtc")
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.HasKey("PlaylistId", "MediaId");
@@ -207,6 +212,8 @@ namespace Manager.Data.Migrations
b.HasKey("Id");
b.HasIndex("ChannelId");
b.ToTable("media", (string)null);
});
@@ -238,6 +245,9 @@ namespace Manager.Data.Migrations
b.Property<long>("ContentLengthBytes")
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAtUtc")
.HasColumnType("TEXT");
b.Property<double?>("Framerate")
.HasColumnType("REAL");
@@ -250,6 +260,9 @@ namespace Manager.Data.Migrations
b.Property<long>("LastModifiedUnixEpoch")
.HasColumnType("INTEGER");
b.Property<DateTime>("LastModifiedUtc")
.HasColumnType("TEXT");
b.Property<double?>("LoudnessDb")
.HasColumnType("REAL");
@@ -315,6 +328,15 @@ namespace Manager.Data.Migrations
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.ClientAccountEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ChannelEntity", null)
.WithOne("ClientAccount")
.HasForeignKey("Manager.Data.Entities.LibraryContext.ClientAccountEntity", "Id")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.HttpCookieEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ClientAccountEntity", null)
@@ -339,6 +361,15 @@ namespace Manager.Data.Migrations
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.MediaEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ChannelEntity", null)
.WithMany("Media")
.HasForeignKey("ChannelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.MediaFormatEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.MediaEntity", null)
@@ -350,18 +381,25 @@ namespace Manager.Data.Migrations
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.PlaylistEntity", b =>
{
b.HasOne("Manager.Data.Entities.LibraryContext.ClientAccountEntity", null)
b.HasOne("Manager.Data.Entities.LibraryContext.ChannelEntity", null)
.WithMany("Playlists")
.HasForeignKey("ChannelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.ChannelEntity", b =>
{
b.Navigation("ClientAccount");
b.Navigation("Media");
b.Navigation("Playlists");
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.ClientAccountEntity", b =>
{
b.Navigation("HttpCookies");
b.Navigation("Playlists");
});
modelBuilder.Entity("Manager.Data.Entities.LibraryContext.MediaEntity", b =>

View File

@@ -12,4 +12,7 @@ Update the database
```shell
dotnet ef database update --project Manager.Data --startup-project Manager.App --context Manager.Data.Contexts.LibraryDbContext
```
Remove migration
```shell
dotnet ef migrations remove --project Manager.Data --startup-project Manager.App --context Manager.Data.Contexts.LibraryDbContext
```