POC for library items includes

This commit is contained in:
Zoe Roux 2023-11-18 20:57:11 +01:00
parent 0034f93caa
commit c5a2a05af6
4 changed files with 34 additions and 16 deletions

4
.pg_format Normal file
View File

@ -0,0 +1,4 @@
tabs=1
function-case=1 #lowercase
keyword-case=1
type-case=1

View File

@ -25,6 +25,7 @@ using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Dapper;
using InterpolatedSql.Dapper;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Exceptions;
@ -97,9 +98,9 @@ namespace Kyoo.Core.Controllers
return sort switch
{
// TODO: Implement default sort by
Sort<T>.Default => "",
Sort<T>.By(string key, bool desc) => $"coalesce({tables.Select(x => $"{x}.{key}")}) {(desc ? "desc" : "asc")}",
Sort<T>.Random(var seed) => $"md5({seed} || coalesce({tables.Select(x => $"{x}.id")}))",
Sort<T>.Default => $"coalesce({string.Join(", ", tables.Select(x => $"{x}.name"))})",
Sort<T>.By(string key, bool desc) => $"coalesce({string.Join(", ", tables.Select(x => $"{x}.{key}"))}) {(desc ? "desc" : "asc")}",
Sort<T>.Random(var seed) => $"md5({seed} || coalesce({string.Join(", ", tables.Select(x => $"{x}.id"))}))",
Sort<T>.Conglomerate(var list) => string.Join(", ", list.Select(x => ProcessSort(x, tables))),
_ => throw new SwitchExpressionException(),
};
@ -112,19 +113,30 @@ namespace Kyoo.Core.Controllers
Include<LibraryItem>? include = null)
{
// language=PostgreSQL
string sql = @"
select s.*, m.*, c.*, st.* from shows as s full outer join (
select * from movies
) as m on false
IDapperSqlCommand query = _database.SqlBuilder($"""
select
s.*,
m.*,
c.*,
st.*
from
shows as s
full outer join (
select * from collections
) as c on false
select
*
from
movies) as m on false
full outer join (
select
*
from
collections) as c on false
left join studios as st on st.id = coalesce(s.studio_id, m.studio_id)
order by @SortBy
limit @Take
";
order by {ProcessSort(sort, new[] { "s", "m", "c" }):raw}
limit {limit.Limit}
""").Build();
var data = await _database.QueryAsync<IResource>(sql, new[] { typeof(Show), typeof(Movie), typeof(Collection), typeof(Studio) }, items =>
var data = await query.QueryAsync<IResource>(new[] { typeof(Show), typeof(Movie), typeof(Collection), typeof(Studio) }, items =>
{
var studio = items[3] as Studio;
if (items[0] is Show show && show.Id != 0)
@ -134,7 +146,7 @@ namespace Kyoo.Core.Controllers
if (items[2] is Collection collection && collection.Id != 0)
return collection;
throw new InvalidDataException();
}, new { SortBy = ProcessSort(sort, new[] { "s", "m", "c" }), Take = limit.Limit });
});
// await using DbDataReader reader = await _database.ExecuteReaderAsync(sql);
// int kindOrdinal = reader.GetOrdinal("kind");

View File

@ -9,6 +9,7 @@
<PackageReference Include="AspNetCore.Proxy" Version="4.4.0" />
<PackageReference Include="Blurhash.SkiaSharp" Version="2.0.0" />
<PackageReference Include="Dapper" Version="2.1.21" />
<PackageReference Include="InterpolatedSql.Dapper" Version="2.1.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.9" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.12" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

View File

@ -92,6 +92,7 @@ services:
- "5432:5432"
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
command: ["postgres", "-c", "log_statement=all"]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 5s