Add creation date on the db

This commit is contained in:
Zoe Roux 2023-09-07 22:33:31 +02:00
parent 7962d3a16f
commit 5f6e96333c
12 changed files with 1827 additions and 23 deletions

View File

@ -45,7 +45,7 @@ namespace Kyoo.Abstractions.Models
Collection
}
public class LibraryItem : IResource, IThumbnails, IMetadata
public class LibraryItem : IResource, IThumbnails, IMetadata, IAddedDate
{
/// <inheritdoc />
public int Id { get; set; }
@ -110,6 +110,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public DateTime? AirDate { get; set; }
/// <inheritdoc />
public DateTime AddedDate { get; set; }
/// <inheritdoc />
public Image? Poster { get; set; }

View File

@ -16,6 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Kyoo.Abstractions.Models.Attributes;
@ -27,7 +28,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A class representing collections of <see cref="Show"/>.
/// </summary>
public class Collection : IResource, IMetadata, IThumbnails
public class Collection : IResource, IMetadata, IThumbnails, IAddedDate
{
/// <inheritdoc />
public int Id { get; set; }
@ -45,6 +46,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public string? Overview { get; set; }
/// <inheritdoc />
public DateTime AddedDate { get; set; }
/// <inheritdoc />
public Image? Poster { get; set; }

View File

@ -29,7 +29,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A class to represent a single show's episode.
/// </summary>
public class Episode : IResource, IMetadata, IThumbnails
public class Episode : IResource, IMetadata, IThumbnails, IAddedDate
{
/// <inheritdoc />
public int Id { get; set; }
@ -138,6 +138,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public DateTime? ReleaseDate { get; set; }
/// <inheritdoc />
public DateTime AddedDate { get; set; }
/// <inheritdoc />
public Image? Poster { get; set; }

View File

@ -0,0 +1,33 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
namespace Kyoo.Abstractions.Models
{
/// <summary>
/// An interface applied to resources.
/// </summary>
public interface IAddedDate
{
/// <summary>
/// The date at which this resource was added to kyoo.
/// </summary>
public DateTime AddedDate { get; set; }
}
}

View File

@ -29,7 +29,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A series or a movie.
/// </summary>
public class Movie : IResource, IMetadata, IOnMerge, IThumbnails
public class Movie : IResource, IMetadata, IOnMerge, IThumbnails, IAddedDate
{
/// <inheritdoc />
public int Id { get; set; }
@ -83,6 +83,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public DateTime? AirDate { get; set; }
/// <inheritdoc />
public DateTime AddedDate { get; set; }
/// <inheritdoc />
public Image? Poster { get; set; }

View File

@ -29,7 +29,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A season of a <see cref="Show"/>.
/// </summary>
public class Season : IResource, IMetadata, IThumbnails
public class Season : IResource, IMetadata, IThumbnails, IAddedDate
{
/// <inheritdoc />
public int Id { get; set; }
@ -95,6 +95,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public DateTime? StartDate { get; set; }
/// <inheritdoc />
public DateTime AddedDate { get; set; }
/// <summary>
/// The ending date of this season.
/// </summary>

View File

@ -29,7 +29,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A series or a movie.
/// </summary>
public class Show : IResource, IMetadata, IOnMerge, IThumbnails
public class Show : IResource, IMetadata, IOnMerge, IThumbnails, IAddedDate
{
/// <inheritdoc />
public int Id { get; set; }
@ -84,6 +84,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public DateTime? EndAir { get; set; }
/// <inheritdoc />
public DateTime AddedDate { get; set; }
/// <inheritdoc />
public Image? Poster { get; set; }

View File

@ -28,7 +28,7 @@ namespace Kyoo.Abstractions.Models
/// <summary>
/// A single user of the app.
/// </summary>
public class User : IResource
public class User : IResource, IAddedDate
{
/// <inheritdoc />
public int Id { get; set; }
@ -58,6 +58,9 @@ namespace Kyoo.Abstractions.Models
/// </summary>
public string[] Permissions { get; set; } = Array.Empty<string>();
/// <inheritdoc />
public DateTime AddedDate { get; set; }
/// <summary>
/// A logo is a small image representing the resource.
/// </summary>

View File

@ -190,6 +190,14 @@ namespace Kyoo.Postgresql
.OwnsOne(x => x.Logo);
}
private static void _HasAddedDate<T>(ModelBuilder modelBuilder)
where T : class, IAddedDate
{
modelBuilder.Entity<T>()
.Property(x => x.AddedDate)
.HasDefaultValueSql("now() at time zone 'utc'");
}
/// <summary>
/// Create a many to many relationship between the two entities.
/// The resulting relationship will have an available <see cref="AddLinks{T1,T2}"/> method.
@ -285,6 +293,14 @@ namespace Kyoo.Postgresql
_HasImages<Episode>(modelBuilder);
_HasImages<People>(modelBuilder);
_HasAddedDate<LibraryItem>(modelBuilder);
_HasAddedDate<Collection>(modelBuilder);
_HasAddedDate<Movie>(modelBuilder);
_HasAddedDate<Show>(modelBuilder);
_HasAddedDate<Season>(modelBuilder);
_HasAddedDate<Episode>(modelBuilder);
_HasAddedDate<User>(modelBuilder);
modelBuilder.Entity<User>().OwnsOne(x => x.Logo);
modelBuilder.Entity<WatchedEpisode>()

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,139 @@
// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Kyoo.Postgresql.Migrations
{
/// <inheritdoc />
public partial class AddedDate : Items
{
public static void CreateItemView(MigrationBuilder migrationBuilder)
{
// language=PostgreSQL
migrationBuilder.Sql(@"
CREATE VIEW library_items AS
SELECT
s.id, s.slug, s.name, s.tagline, s.aliases, s.overview, s.tags, s.genres, s.status,
s.start_air, s.end_air, s.poster_source, s.poster_blurhash, s.thumbnail_source, s.thumbnail_blurhash,
s.logo_source, s.logo_blurhash, s.trailer, s.external_id, s.start_air AS air_date, NULL as path,
'show'::item_kind AS kind, added_date
FROM shows AS s
UNION ALL
SELECT
-m.id, m.slug, m.name, m.tagline, m.aliases, m.overview, m.tags, m.genres, m.status,
m.air_date as start_air, m.air_date as end_air, m.poster_source, m.poster_blurhash, m.thumbnail_source,
m.thumbnail_blurhash, m.logo_source, m.logo_blurhash, m.trailer, m.external_id, m.air_date, m.path,
'movie'::item_kind AS kind, added_date
FROM movies AS m
UNION ALL
SELECT
c.id + 10000 AS id, c.slug, c.name, NULL as tagline, NULL as alises, c.overview, NULL AS tags, NULL AS genres, 'unknown'::status AS status,
NULL AS start_air, NULL AS end_air, c.poster_source, c.poster_blurhash, c.thumbnail_source,
c.thumbnail_blurhash, c.logo_source, c.logo_blurhash, NULL as trailer, c.external_id, NULL AS air_date, NULL as path,
'collection'::item_kind AS kind, added_date
FROM collections AS c
");
}
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
base.Down(migrationBuilder);
migrationBuilder.AddColumn<DateTime>(
name: "added_date",
table: "users",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "now() at time zone 'utc'");
migrationBuilder.AddColumn<DateTime>(
name: "added_date",
table: "shows",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "now() at time zone 'utc'");
migrationBuilder.AddColumn<DateTime>(
name: "added_date",
table: "seasons",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "now() at time zone 'utc'");
migrationBuilder.AddColumn<DateTime>(
name: "added_date",
table: "movies",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "now() at time zone 'utc'");
migrationBuilder.AddColumn<DateTime>(
name: "added_date",
table: "episodes",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "now() at time zone 'utc'");
migrationBuilder.AddColumn<DateTime>(
name: "added_date",
table: "collections",
type: "timestamp with time zone",
nullable: false,
defaultValueSql: "now() at time zone 'utc'");
CreateItemView(migrationBuilder);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
base.Down(migrationBuilder);
migrationBuilder.DropColumn(
name: "added_date",
table: "users");
migrationBuilder.DropColumn(
name: "added_date",
table: "shows");
migrationBuilder.DropColumn(
name: "added_date",
table: "seasons");
migrationBuilder.DropColumn(
name: "added_date",
table: "movies");
migrationBuilder.DropColumn(
name: "added_date",
table: "episodes");
migrationBuilder.DropColumn(
name: "added_date",
table: "collections");
base.Up(migrationBuilder);
}
}
}

View File

@ -1,5 +1,6 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using Kyoo.Abstractions.Models;
using Kyoo.Postgresql;
using Microsoft.EntityFrameworkCore;
@ -35,6 +36,12 @@ namespace Kyoo.Postgresql.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("AddedDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("added_date")
.HasDefaultValueSql("now() at time zone 'utc'");
b.Property<string>("ExternalId")
.IsRequired()
.HasColumnType("json")
@ -78,6 +85,12 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("integer")
.HasColumnName("absolute_number");
b.Property<DateTime>("AddedDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("added_date")
.HasDefaultValueSql("now() at time zone 'utc'");
b.Property<int?>("EpisodeNumber")
.HasColumnType("integer")
.HasColumnName("episode_number");
@ -104,7 +117,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("timestamp with time zone")
.HasColumnName("release_date");
b.Property<int?>("SeasonID")
b.Property<int?>("SeasonId")
.HasColumnType("integer")
.HasColumnName("season_id");
@ -112,7 +125,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("integer")
.HasColumnName("season_number");
b.Property<int>("ShowID")
b.Property<int>("ShowId")
.HasColumnType("integer")
.HasColumnName("show_id");
@ -125,14 +138,14 @@ namespace Kyoo.Postgresql.Migrations
b.HasKey("Id")
.HasName("pk_episodes");
b.HasIndex("SeasonID")
b.HasIndex("SeasonId")
.HasDatabaseName("ix_episodes_season_id");
b.HasIndex("Slug")
.IsUnique()
.HasDatabaseName("ix_episodes_slug");
b.HasIndex("ShowID", "SeasonNumber", "EpisodeNumber", "AbsoluteNumber")
b.HasIndex("ShowId", "SeasonNumber", "EpisodeNumber", "AbsoluteNumber")
.IsUnique()
.HasDatabaseName("ix_episodes_show_id_season_number_episode_number_absolute_numb");
@ -148,6 +161,12 @@ namespace Kyoo.Postgresql.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("AddedDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("added_date")
.HasDefaultValueSql("now() at time zone 'utc'");
b.Property<DateTime?>("AirDate")
.HasColumnType("timestamp with time zone")
.HasColumnName("air_date");
@ -171,6 +190,10 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("genre[]")
.HasColumnName("genres");
b.Property<ItemKind>("Kind")
.HasColumnType("item_kind")
.HasColumnName("kind");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text")
@ -181,7 +204,6 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnName("overview");
b.Property<string>("Path")
.IsRequired()
.HasColumnType("text")
.HasColumnName("path");
@ -227,6 +249,12 @@ namespace Kyoo.Postgresql.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("AddedDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("added_date")
.HasDefaultValueSql("now() at time zone 'utc'");
b.Property<DateTime?>("AirDate")
.HasColumnType("timestamp with time zone")
.HasColumnName("air_date");
@ -390,6 +418,12 @@ namespace Kyoo.Postgresql.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("AddedDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("added_date")
.HasDefaultValueSql("now() at time zone 'utc'");
b.Property<DateTime?>("EndDate")
.HasColumnType("timestamp with time zone")
.HasColumnName("end_date");
@ -411,7 +445,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("integer")
.HasColumnName("season_number");
b.Property<int>("ShowID")
b.Property<int>("ShowId")
.HasColumnType("integer")
.HasColumnName("show_id");
@ -432,7 +466,7 @@ namespace Kyoo.Postgresql.Migrations
.IsUnique()
.HasDatabaseName("ix_seasons_slug");
b.HasIndex("ShowID", "SeasonNumber")
b.HasIndex("ShowId", "SeasonNumber")
.IsUnique()
.HasDatabaseName("ix_seasons_show_id_season_number");
@ -448,7 +482,13 @@ namespace Kyoo.Postgresql.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<string[]>("Aliases")
b.Property<DateTime>("AddedDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("added_date")
.HasDefaultValueSql("now() at time zone 'utc'");
b.Property<List<string>>("Aliases")
.IsRequired()
.HasColumnType("text[]")
.HasColumnName("aliases");
@ -462,7 +502,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("json")
.HasColumnName("external_id");
b.Property<Genre[]>("Genres")
b.Property<List<Genre>>("Genres")
.IsRequired()
.HasColumnType("genre[]")
.HasColumnName("genres");
@ -490,7 +530,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("status")
.HasColumnName("status");
b.Property<int?>("StudioID")
b.Property<int?>("StudioId")
.HasColumnType("integer")
.HasColumnName("studio_id");
@ -498,7 +538,7 @@ namespace Kyoo.Postgresql.Migrations
.HasColumnType("text")
.HasColumnName("tagline");
b.Property<string[]>("Tags")
b.Property<List<string>>("Tags")
.IsRequired()
.HasColumnType("text[]")
.HasColumnName("tags");
@ -514,7 +554,7 @@ namespace Kyoo.Postgresql.Migrations
.IsUnique()
.HasDatabaseName("ix_shows_slug");
b.HasIndex("StudioID")
b.HasIndex("StudioId")
.HasDatabaseName("ix_shows_studio_id");
b.ToTable("shows", (string)null);
@ -564,6 +604,12 @@ namespace Kyoo.Postgresql.Migrations
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<DateTime>("AddedDate")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("added_date")
.HasDefaultValueSql("now() at time zone 'utc'");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("text")
@ -771,13 +817,13 @@ namespace Kyoo.Postgresql.Migrations
{
b.HasOne("Kyoo.Abstractions.Models.Season", "Season")
.WithMany("Episodes")
.HasForeignKey("SeasonID")
.HasForeignKey("SeasonId")
.OnDelete(DeleteBehavior.Cascade)
.HasConstraintName("fk_episodes_seasons_season_id");
b.HasOne("Kyoo.Abstractions.Models.Show", "Show")
.WithMany("Episodes")
.HasForeignKey("ShowID")
.HasForeignKey("ShowId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_episodes_shows_show_id");
@ -1170,7 +1216,7 @@ namespace Kyoo.Postgresql.Migrations
{
b.HasOne("Kyoo.Abstractions.Models.Show", "Show")
.WithMany("Seasons")
.HasForeignKey("ShowID")
.HasForeignKey("ShowId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
.HasConstraintName("fk_seasons_shows_show_id");
@ -1266,7 +1312,7 @@ namespace Kyoo.Postgresql.Migrations
{
b.HasOne("Kyoo.Abstractions.Models.Studio", "Studio")
.WithMany("Shows")
.HasForeignKey("StudioID")
.HasForeignKey("StudioId")
.OnDelete(DeleteBehavior.SetNull)
.HasConstraintName("fk_shows_studios_studio_id");