mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Install dapper and use it for library items
This commit is contained in:
parent
bc66d35497
commit
e8f2a72516
@ -18,50 +18,152 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Data.Common;
|
||||
using System.IO;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Abstractions.Models.Exceptions;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
using Kyoo.Postgresql;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Kyoo.Core.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// A local repository to handle library items.
|
||||
/// </summary>
|
||||
public class LibraryItemRepository : LocalRepository<LibraryItem>
|
||||
public class LibraryItemRepository : IRepository<LibraryItem>
|
||||
{
|
||||
/// <summary>
|
||||
/// The database handle
|
||||
/// </summary>
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly DbConnection _database;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Sort<LibraryItem> DefaultSort => new Sort<LibraryItem>.By(x => x.Name);
|
||||
protected Sort<LibraryItem> DefaultSort => new Sort<LibraryItem>.By(x => x.Name);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new <see cref="LibraryItemRepository"/>.
|
||||
/// </summary>
|
||||
/// <param name="database">The database instance</param>
|
||||
/// <param name="thumbs">The thumbnail manager used to store images.</param>
|
||||
public LibraryItemRepository(DatabaseContext database, IThumbnailsManager thumbs)
|
||||
: base(database, thumbs)
|
||||
public Type RepositoryType => typeof(LibraryItem);
|
||||
|
||||
public LibraryItemRepository(DbConnection database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task<ICollection<LibraryItem>> Search(string query, Include<LibraryItem>? include = default)
|
||||
/// <inheritdoc/>
|
||||
public virtual async Task<LibraryItem> Get(int id, Include<LibraryItem>? include = default)
|
||||
{
|
||||
return await Sort(
|
||||
AddIncludes(_database.LibraryItems, include)
|
||||
.Where(_database.Like<LibraryItem>(x => x.Name, $"%{query}%"))
|
||||
)
|
||||
.Take(20)
|
||||
.ToListAsync();
|
||||
LibraryItem? ret = await GetOrDefault(id, include);
|
||||
if (ret == null)
|
||||
throw new ItemNotFoundException($"No {nameof(LibraryItem)} found with the id {id}");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual async Task<LibraryItem> Get(string slug, Include<LibraryItem>? include = default)
|
||||
{
|
||||
LibraryItem? ret = await GetOrDefault(slug, include);
|
||||
if (ret == null)
|
||||
throw new ItemNotFoundException($"No {nameof(LibraryItem)} found with the slug {slug}");
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual async Task<LibraryItem> Get(
|
||||
Expression<Func<LibraryItem, bool>> where,
|
||||
Include<LibraryItem>? include = default)
|
||||
{
|
||||
LibraryItem? ret = await GetOrDefault(where, include: include);
|
||||
if (ret == null)
|
||||
throw new ItemNotFoundException($"No {nameof(LibraryItem)} found with the given predicate.");
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Task<LibraryItem?> GetOrDefault(int id, Include<LibraryItem>? include = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<LibraryItem?> GetOrDefault(string slug, Include<LibraryItem>? include = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<LibraryItem?> GetOrDefault(Expression<Func<LibraryItem, bool>> where, Include<LibraryItem>? include = null, Sort<LibraryItem>? sortBy = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<ICollection<LibraryItem>> GetAll(
|
||||
Expression<Func<LibraryItem, bool>>? where = null,
|
||||
Sort<LibraryItem>? sort = null,
|
||||
Pagination? limit = null,
|
||||
Include<LibraryItem>? include = null)
|
||||
{
|
||||
List<IResource> ret = new(limit.Limit);
|
||||
|
||||
// language=PostgreSQL
|
||||
string sql = @"
|
||||
select s.*, m.*, c.* from shows as s full outer join (
|
||||
select * from movies
|
||||
) as m on false
|
||||
full outer join (
|
||||
select * from collections
|
||||
) as c on false
|
||||
";
|
||||
|
||||
var data = await _database.QueryAsync<IResource>(sql, new[] { typeof(Show), typeof(Movie), typeof(Collection) }, items =>
|
||||
{
|
||||
if (items[0] is Show show && show.Id != 0)
|
||||
return show;
|
||||
if (items[1] is Movie movie && movie.Id != 0)
|
||||
return movie;
|
||||
if (items[2] is Collection collection && collection.Id != 0)
|
||||
return collection;
|
||||
throw new InvalidDataException();
|
||||
}, where);
|
||||
|
||||
// await using DbDataReader reader = await _database.ExecuteReaderAsync(sql);
|
||||
// int kindOrdinal = reader.GetOrdinal("kind");
|
||||
// var showParser = reader.GetRowParser<IResource>(typeof(Show));
|
||||
// var movieParser = reader.GetRowParser<IResource>(typeof(Movie));
|
||||
// var collectionParser = reader.GetRowParser<IResource>(typeof(Collection));
|
||||
//
|
||||
// while (await reader.ReadAsync())
|
||||
// {
|
||||
// ItemKind type = await reader.GetFieldValueAsync<ItemKind>(kindOrdinal);
|
||||
// ret.Add(type switch
|
||||
// {
|
||||
// ItemKind.Show => showParser(reader),
|
||||
// ItemKind.Movie => movieParser(reader),
|
||||
// ItemKind.Collection => collectionParser(reader),
|
||||
// _ => throw new InvalidDataException(),
|
||||
// });
|
||||
// }
|
||||
throw new NotImplementedException();
|
||||
// return ret;
|
||||
}
|
||||
|
||||
public Task<int> GetCount(Expression<Func<LibraryItem, bool>>? where = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<ICollection<LibraryItem>> FromIds(IList<int> ids, Include<LibraryItem>? include = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task DeleteAll(Expression<Func<LibraryItem, bool>> where)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
/// <inheritdoc />
|
||||
public async Task<ICollection<LibraryItem>> Search(string query, Include<LibraryItem>? include = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
// return await Sort(
|
||||
// AddIncludes(_database.LibraryItems, include)
|
||||
// .Where(_database.Like<LibraryItem>(x => x.Name, $"%{query}%"))
|
||||
// )
|
||||
// .Take(20)
|
||||
// .ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<ICollection<LibraryItem>> GetAllOfCollection(
|
||||
@ -71,48 +173,49 @@ namespace Kyoo.Core.Controllers
|
||||
Pagination? limit = default,
|
||||
Include<LibraryItem>? include = default)
|
||||
{
|
||||
return await ApplyFilters(
|
||||
_database.LibraryItems
|
||||
.Where(item =>
|
||||
_database.Movies
|
||||
.Where(x => x.Id == -item.Id)
|
||||
.Any(x => x.Collections!.AsQueryable().Any(selector))
|
||||
|| _database.Shows
|
||||
.Where(x => x.Id == item.Id)
|
||||
.Any(x => x.Collections!.AsQueryable().Any(selector))
|
||||
),
|
||||
where,
|
||||
sort,
|
||||
limit,
|
||||
include);
|
||||
throw new NotImplementedException();
|
||||
// return await ApplyFilters(
|
||||
// _database.LibraryItems
|
||||
// .Where(item =>
|
||||
// _database.Movies
|
||||
// .Where(x => x.Id == -item.Id)
|
||||
// .Any(x => x.Collections!.AsQueryable().Any(selector))
|
||||
// || _database.Shows
|
||||
// .Where(x => x.Id == item.Id)
|
||||
// .Any(x => x.Collections!.AsQueryable().Any(selector))
|
||||
// ),
|
||||
// where,
|
||||
// sort,
|
||||
// limit,
|
||||
// include);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<LibraryItem> Create(LibraryItem obj)
|
||||
public Task<LibraryItem> Create(LibraryItem obj)
|
||||
=> throw new InvalidOperationException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<LibraryItem> CreateIfNotExists(LibraryItem obj)
|
||||
public Task<LibraryItem> CreateIfNotExists(LibraryItem obj)
|
||||
=> throw new InvalidOperationException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<LibraryItem> Edit(LibraryItem edited)
|
||||
public Task<LibraryItem> Edit(LibraryItem edited)
|
||||
=> throw new InvalidOperationException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<LibraryItem> Patch(int id, Func<LibraryItem, Task<bool>> patch)
|
||||
public Task<LibraryItem> Patch(int id, Func<LibraryItem, Task<bool>> patch)
|
||||
=> throw new InvalidOperationException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task Delete(int id)
|
||||
public Task Delete(int id)
|
||||
=> throw new InvalidOperationException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task Delete(string slug)
|
||||
public Task Delete(string slug)
|
||||
=> throw new InvalidOperationException();
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task Delete(LibraryItem obj)
|
||||
public Task Delete(LibraryItem obj)
|
||||
=> throw new InvalidOperationException();
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
<ItemGroup>
|
||||
<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="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" />
|
||||
|
@ -6,6 +6,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.1.21" />
|
||||
<PackageReference Include="EFCore.NamingConventions" Version="7.0.2" />
|
||||
<PackageReference Include="EntityFrameworkCore.Projectables" Version="4.1.4-prebeta" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
|
||||
|
@ -17,8 +17,14 @@
|
||||
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Text.RegularExpressions;
|
||||
using Dapper;
|
||||
using EFCore.NamingConventions.Internal;
|
||||
using Kyoo.Abstractions.Controllers;
|
||||
using Kyoo.Abstractions.Models;
|
||||
using Kyoo.Postgresql.Utils;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@ -69,29 +75,44 @@ namespace Kyoo.Postgresql
|
||||
using NpgsqlConnection conn = (NpgsqlConnection)context.Database.GetDbConnection();
|
||||
conn.Open();
|
||||
conn.ReloadTypes();
|
||||
|
||||
SqlMapper.TypeMapProvider = (type) =>
|
||||
{
|
||||
return new CustomPropertyTypeMap(type, (type, name) =>
|
||||
{
|
||||
string newName = Regex.Replace(name, "(^|_)([a-z])", (match) => match.Groups[2].Value.ToUpperInvariant());
|
||||
// TODO: Add images handling here (name: poster_source, newName: PosterSource) should set Poster.Source
|
||||
return type.GetProperty(newName)!;
|
||||
});
|
||||
};
|
||||
SqlMapper.AddTypeHandler(typeof(Dictionary<string, MetadataId>), new JsonTypeHandler<Dictionary<string, MetadataId>>());
|
||||
SqlMapper.AddTypeHandler(typeof(List<string>), new ListTypeHandler<string>());
|
||||
SqlMapper.AddTypeHandler(typeof(List<Genre>), new ListTypeHandler<Genre>());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Configure(IServiceCollection services)
|
||||
{
|
||||
DbConnectionStringBuilder builder = new()
|
||||
{
|
||||
["USER ID"] = _configuration.GetValue("POSTGRES_USER", "KyooUser"),
|
||||
["PASSWORD"] = _configuration.GetValue("POSTGRES_PASSWORD", "KyooPassword"),
|
||||
["SERVER"] = _configuration.GetValue("POSTGRES_SERVER", "db"),
|
||||
["PORT"] = _configuration.GetValue("POSTGRES_PORT", "5432"),
|
||||
["DATABASE"] = _configuration.GetValue("POSTGRES_DB", "kyooDB"),
|
||||
["POOLING"] = "true",
|
||||
["MAXPOOLSIZE"] = "95",
|
||||
["TIMEOUT"] = "30"
|
||||
};
|
||||
|
||||
services.AddDbContext<DatabaseContext, PostgresContext>(x =>
|
||||
{
|
||||
DbConnectionStringBuilder builder = new()
|
||||
{
|
||||
["USER ID"] = _configuration.GetValue("POSTGRES_USER", "KyooUser"),
|
||||
["PASSWORD"] = _configuration.GetValue("POSTGRES_PASSWORD", "KyooPassword"),
|
||||
["SERVER"] = _configuration.GetValue("POSTGRES_SERVER", "db"),
|
||||
["PORT"] = _configuration.GetValue("POSTGRES_PORT", "5432"),
|
||||
["DATABASE"] = _configuration.GetValue("POSTGRES_DB", "kyooDB"),
|
||||
["POOLING"] = "true",
|
||||
["MAXPOOLSIZE"] = "95",
|
||||
["TIMEOUT"] = "30"
|
||||
};
|
||||
x.UseNpgsql(builder.ConnectionString)
|
||||
.UseProjectables();
|
||||
if (_environment.IsDevelopment())
|
||||
x.EnableDetailedErrors().EnableSensitiveDataLogging();
|
||||
}, ServiceLifetime.Transient);
|
||||
services.AddTransient<DbConnection>((_) => new NpgsqlConnection(builder.ConnectionString));
|
||||
|
||||
services.AddHealthChecks().AddDbContextCheck<DatabaseContext>();
|
||||
}
|
||||
|
42
back/src/Kyoo.Postgresql/Utils/JsonTypeHandler.cs
Normal file
42
back/src/Kyoo.Postgresql/Utils/JsonTypeHandler.cs
Normal file
@ -0,0 +1,42 @@
|
||||
// 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.Data;
|
||||
using Newtonsoft.Json;
|
||||
using Npgsql;
|
||||
using NpgsqlTypes;
|
||||
using static Dapper.SqlMapper;
|
||||
|
||||
namespace Kyoo.Postgresql.Utils;
|
||||
|
||||
public class JsonTypeHandler<T> : TypeHandler<T>
|
||||
where T : class
|
||||
{
|
||||
public override T? Parse(object value)
|
||||
{
|
||||
if (value is string str)
|
||||
return JsonConvert.DeserializeObject<T>(str);
|
||||
return default;
|
||||
}
|
||||
|
||||
public override void SetValue(IDbDataParameter parameter, T? value)
|
||||
{
|
||||
parameter.Value = JsonConvert.SerializeObject(value);
|
||||
((NpgsqlParameter)parameter).NpgsqlDbType = NpgsqlDbType.Jsonb;
|
||||
}
|
||||
}
|
21
back/src/Kyoo.Postgresql/Utils/ListTypeHandler.cs
Normal file
21
back/src/Kyoo.Postgresql/Utils/ListTypeHandler.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using Dapper;
|
||||
|
||||
namespace Kyoo.Postgresql.Utils;
|
||||
|
||||
// See https://github.com/DapperLib/Dapper/issues/1424
|
||||
public class ListTypeHandler<T> : SqlMapper.TypeHandler<List<T>>
|
||||
{
|
||||
public override List<T> Parse(object value)
|
||||
{
|
||||
T[] typedValue = (T[])value; // looks like Dapper did not indicate the property type to Npgsql, so it defaults to string[] (default CLR type for text[] PostgreSQL type)
|
||||
return typedValue?.ToList() ?? new();
|
||||
}
|
||||
|
||||
public override void SetValue(IDbDataParameter parameter, List<T>? value)
|
||||
{
|
||||
parameter.Value = value; // no need to convert to string[] in this direction
|
||||
}
|
||||
}
|
@ -54,7 +54,7 @@ namespace Kyoo.Tests.Database
|
||||
MovieRepository movies = new(_NewContext(), studio, people, thumbs.Object);
|
||||
ShowRepository show = new(_NewContext(), studio, people, thumbs.Object);
|
||||
SeasonRepository season = new(_NewContext(), thumbs.Object);
|
||||
LibraryItemRepository libraryItem = new(_NewContext(), thumbs.Object);
|
||||
LibraryItemRepository libraryItem = new(_NewContext());
|
||||
EpisodeRepository episode = new(_NewContext(), show, thumbs.Object);
|
||||
UserRepository user = new(_NewContext(), thumbs.Object);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user