Add news view in the db

This commit is contained in:
Zoe Roux 2023-10-30 00:54:58 +01:00
parent 44521d0d5f
commit 5a6bb57fd5
9 changed files with 1977 additions and 26 deletions

View File

@ -95,6 +95,11 @@ namespace Kyoo.Abstractions.Models
/// </summary> /// </summary>
public DateTime? AirDate { get; set; } public DateTime? AirDate { get; set; }
/// <summary>
/// The date this movie aired.
/// </summary>
public DateTime? ReleaseDate => AirDate;
/// <inheritdoc /> /// <inheritdoc />
public DateTime AddedDate { get; set; } public DateTime AddedDate { get; set; }

View File

@ -79,7 +79,6 @@ namespace Kyoo.Core.Controllers
{ {
return await Sort( return await Sort(
_database.Episodes _database.Episodes
.Include(x => x.Show)
.Where(x => x.EpisodeNumber != null || x.AbsoluteNumber != null) .Where(x => x.EpisodeNumber != null || x.AbsoluteNumber != null)
.Where(_database.Like<Episode>(x => x.Name!, $"%{query}%")) .Where(_database.Like<Episode>(x => x.Name!, $"%{query}%"))
) )

View File

@ -31,7 +31,7 @@ namespace Kyoo.Core.Controllers
public class NewsRepository : LocalRepository<News> public class NewsRepository : LocalRepository<News>
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override Sort<News> DefaultSort => new Sort<News>.By(x => x.AddedDate); protected override Sort<News> DefaultSort => new Sort<News>.By(x => x.AddedDate, true);
public NewsRepository(DatabaseContext database, IThumbnailsManager thumbs) public NewsRepository(DatabaseContext database, IThumbnailsManager thumbs)
: base(database, thumbs) : base(database, thumbs)

View File

@ -50,9 +50,9 @@ namespace Kyoo.Core.Api
[FromQuery] Dictionary<string, string> where, [FromQuery] Dictionary<string, string> where,
[FromQuery] Pagination pagination) [FromQuery] Pagination pagination)
{ {
ICollection<News> resources = await _news.GetNews( ICollection<News> resources = await _news.GetAll(
ApiHelper.ParseWhere<News>(where), ApiHelper.ParseWhere<News>(where),
pagination limit: pagination
); );
return Page(resources, pagination.Limit); return Page(resources, pagination.Limit);

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,60 @@
// 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 Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Kyoo.Postgresql.Migrations
{
/// <inheritdoc />
public partial class News : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("Npgsql:Enum:news_kind", "episode,movie");
// language=PostgreSQL
migrationBuilder.Sql(@"
CREATE VIEW news AS
SELECT
e.id, e.slug, e.name, NULL AS tagline, '{}' AS aliases, e.path, e.overview, '{}' AS tags, '{}' AS genres,
NULL AS status, e.release_date AS air_date, e.poster_source, e.poster_blurhash, e.thumbnail_source, e.thumbnail_blurhash,
e.logo_source,e.logo_blurhash, NULL AS trailer, e.external_id, e.season_number, e.episode_number, e.absolute_number,
'episode'::news_kind AS kind, e.added_date
FROM episodes AS e
UNION ALL
SELECT
-m.id, m.slug, m.name, m.tagline, m.aliases, m.path, m.overview, m.tags, m.genres,
m.status, m.air_date, m.poster_source, m.poster_blurhash, m.thumbnail_source, m.thumbnail_blurhash,
m.logo_source, m.logo_blurhash, m.trailer, m.external_id, NULL AS season_number, NULL AS episode_number, NULL as absolute_number,
'movie'::news_kind AS kind, m.added_date
FROM movies AS m
");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
// language=PostgreSQL
migrationBuilder.Sql(@"DROP VIEW news");
}
}
}

View File

@ -19,11 +19,12 @@ namespace Kyoo.Postgresql.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "7.0.9") .HasAnnotation("ProductVersion", "7.0.12")
.HasAnnotation("Relational:MaxIdentifierLength", 63); .HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "genre", new[] { "action", "adventure", "animation", "comedy", "crime", "documentary", "drama", "family", "fantasy", "history", "horror", "music", "mystery", "romance", "science_fiction", "thriller", "war", "western" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "genre", new[] { "action", "adventure", "animation", "comedy", "crime", "documentary", "drama", "family", "fantasy", "history", "horror", "music", "mystery", "romance", "science_fiction", "thriller", "war", "western" });
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "item_kind", new[] { "show", "movie", "collection" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "item_kind", new[] { "show", "movie", "collection" });
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "news_kind", new[] { "episode", "movie" });
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "status", new[] { "unknown", "finished", "airing", "planned" }); NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "status", new[] { "unknown", "finished", "airing", "planned" });
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@ -328,6 +329,98 @@ namespace Kyoo.Postgresql.Migrations
b.ToTable("movies", (string)null); b.ToTable("movies", (string)null);
}); });
modelBuilder.Entity("Kyoo.Abstractions.Models.News", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasColumnName("id");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int?>("AbsoluteNumber")
.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<DateTime?>("AirDate")
.HasColumnType("timestamp with time zone")
.HasColumnName("air_date");
b.Property<string[]>("Aliases")
.IsRequired()
.HasColumnType("text[]")
.HasColumnName("aliases");
b.Property<int?>("EpisodeNumber")
.HasColumnType("integer")
.HasColumnName("episode_number");
b.Property<string>("ExternalId")
.IsRequired()
.HasColumnType("json")
.HasColumnName("external_id");
b.Property<Genre[]>("Genres")
.IsRequired()
.HasColumnType("genre[]")
.HasColumnName("genres");
b.Property<NewsKind>("Kind")
.HasColumnType("news_kind")
.HasColumnName("kind");
b.Property<string>("Name")
.HasColumnType("text")
.HasColumnName("name");
b.Property<string>("Overview")
.HasColumnType("text")
.HasColumnName("overview");
b.Property<string>("Path")
.IsRequired()
.HasColumnType("text")
.HasColumnName("path");
b.Property<int?>("SeasonNumber")
.HasColumnType("integer")
.HasColumnName("season_number");
b.Property<string>("Slug")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("character varying(256)")
.HasColumnName("slug");
b.Property<Status?>("Status")
.HasColumnType("status")
.HasColumnName("status");
b.Property<string>("Tagline")
.HasColumnType("text")
.HasColumnName("tagline");
b.Property<string[]>("Tags")
.IsRequired()
.HasColumnType("text[]")
.HasColumnName("tags");
b.Property<string>("Trailer")
.HasColumnType("text")
.HasColumnName("trailer");
b.HasKey("Id")
.HasName("pk_news");
b.ToTable("news", (string)null);
});
modelBuilder.Entity("Kyoo.Abstractions.Models.People", b => modelBuilder.Entity("Kyoo.Abstractions.Models.People", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -1099,6 +1192,93 @@ namespace Kyoo.Postgresql.Migrations
b.Navigation("Thumbnail"); b.Navigation("Thumbnail");
}); });
modelBuilder.Entity("Kyoo.Abstractions.Models.News", b =>
{
b.OwnsOne("Kyoo.Abstractions.Models.Image", "Logo", b1 =>
{
b1.Property<int>("NewsId")
.HasColumnType("integer")
.HasColumnName("id");
b1.Property<string>("Blurhash")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)")
.HasColumnName("logo_blurhash");
b1.Property<string>("Source")
.IsRequired()
.HasColumnType("text")
.HasColumnName("logo_source");
b1.HasKey("NewsId");
b1.ToTable("news");
b1.WithOwner()
.HasForeignKey("NewsId")
.HasConstraintName("fk_news_news_id");
});
b.OwnsOne("Kyoo.Abstractions.Models.Image", "Poster", b1 =>
{
b1.Property<int>("NewsId")
.HasColumnType("integer")
.HasColumnName("id");
b1.Property<string>("Blurhash")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)")
.HasColumnName("poster_blurhash");
b1.Property<string>("Source")
.IsRequired()
.HasColumnType("text")
.HasColumnName("poster_source");
b1.HasKey("NewsId");
b1.ToTable("news");
b1.WithOwner()
.HasForeignKey("NewsId")
.HasConstraintName("fk_news_news_id");
});
b.OwnsOne("Kyoo.Abstractions.Models.Image", "Thumbnail", b1 =>
{
b1.Property<int>("NewsId")
.HasColumnType("integer")
.HasColumnName("id");
b1.Property<string>("Blurhash")
.IsRequired()
.HasMaxLength(32)
.HasColumnType("character varying(32)")
.HasColumnName("thumbnail_blurhash");
b1.Property<string>("Source")
.IsRequired()
.HasColumnType("text")
.HasColumnName("thumbnail_source");
b1.HasKey("NewsId");
b1.ToTable("news");
b1.WithOwner()
.HasForeignKey("NewsId")
.HasConstraintName("fk_news_news_id");
});
b.Navigation("Logo");
b.Navigation("Poster");
b.Navigation("Thumbnail");
});
modelBuilder.Entity("Kyoo.Abstractions.Models.People", b => modelBuilder.Entity("Kyoo.Abstractions.Models.People", b =>
{ {
b.OwnsOne("Kyoo.Abstractions.Models.Image", "Logo", b1 => b.OwnsOne("Kyoo.Abstractions.Models.Image", "Logo", b1 =>

View File

@ -284,9 +284,6 @@ namespace Kyoo.Tests.Database
value.EpisodeNumber = 56; value.EpisodeNumber = 56;
await _repository.Create(value); await _repository.Create(value);
ICollection<Episode> ret = await _repository.Search(query); ICollection<Episode> ret = await _repository.Search(query);
value.Show = TestSample.Get<Show>();
value.Show.AddedDate = DateTime.UnixEpoch;
ret.First().Show.AddedDate = DateTime.UnixEpoch;
KAssert.DeepEqual(value, ret.First()); KAssert.DeepEqual(value, ret.First());
} }

View File

@ -38,24 +38,6 @@ namespace Kyoo.Tests
[AssertionMethod] [AssertionMethod]
public static void DeepEqual<T>(T expected, T value) public static void DeepEqual<T>(T expected, T value)
{ {
if (expected is IResource res and IThumbnails thumbs)
{
if (thumbs.Poster != null)
thumbs.Poster.Path = $"/{expected.GetType().Name.ToLower()}/{res.Slug}/poster";
if (thumbs.Thumbnail != null)
thumbs.Thumbnail.Path = $"/{expected.GetType().Name.ToLower()}/{res.Slug}/thumbnail";
if (thumbs.Logo != null)
thumbs.Logo.Path = $"/{expected.GetType().Name.ToLower()}/{res.Slug}/logo";
}
if (value is IResource resV and IThumbnails thumbsV)
{
if (thumbsV.Poster != null)
thumbsV.Poster.Path = $"/{value.GetType().Name.ToLower()}/{resV.Slug}/poster";
if (thumbsV.Thumbnail != null)
thumbsV.Thumbnail.Path = $"/{value.GetType().Name.ToLower()}/{resV.Slug}/thumbnail";
if (thumbsV.Logo != null)
thumbsV.Logo.Path = $"/{value.GetType().Name.ToLower()}/{resV.Slug}/logo";
}
if (expected is IAddedDate ea && value is IAddedDate va) if (expected is IAddedDate ea && value is IAddedDate va)
{ {
ea.AddedDate = DateTime.UnixEpoch; ea.AddedDate = DateTime.UnixEpoch;