Fix enums handling with dapper

This commit is contained in:
Zoe Roux 2024-03-26 00:53:16 +01:00
parent 8ddf22d661
commit ecbf1f5db5
No known key found for this signature in database
3 changed files with 8 additions and 6 deletions

View File

@ -79,12 +79,12 @@ public class Movie
/// <summary> /// <summary>
/// A list of tags that match this movie. /// A list of tags that match this movie.
/// </summary> /// </summary>
public string[] Tags { get; set; } = Array.Empty<string>(); public string[] Tags { get; set; } = [];
/// <summary> /// <summary>
/// The list of genres (themes) this show has. /// The list of genres (themes) this show has.
/// </summary> /// </summary>
public Genre[] Genres { get; set; } = Array.Empty<Genre>(); public List<Genre> Genres { get; set; } = [];
/// <summary> /// <summary>
/// Is this show airing, not aired yet or finished? /// Is this show airing, not aired yet or finished?

View File

@ -17,8 +17,8 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Data.Common; using System.Data.Common;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -56,7 +56,7 @@ public class PostgresModule(IConfiguration configuration, IWebHostEnvironment en
dsBuilder.MapEnum<Status>(); dsBuilder.MapEnum<Status>();
dsBuilder.MapEnum<Genre>(); dsBuilder.MapEnum<Genre>();
dsBuilder.MapEnum<WatchStatus>(); dsBuilder.MapEnum<WatchStatus>();
var dataSource = dsBuilder.Build(); NpgsqlDataSource dataSource = dsBuilder.Build();
services.AddDbContext<DatabaseContext, PostgresContext>( services.AddDbContext<DatabaseContext, PostgresContext>(
x => x =>
@ -67,7 +67,9 @@ public class PostgresModule(IConfiguration configuration, IWebHostEnvironment en
}, },
ServiceLifetime.Transient ServiceLifetime.Transient
); );
services.AddTransient<DbConnection>((_) => new NpgsqlConnection(builder.ConnectionString)); services.AddTransient(
(services) => services.GetRequiredService<DatabaseContext>().Database.GetDbConnection()
);
services.AddHealthChecks().AddDbContextCheck<DatabaseContext>(); services.AddHealthChecks().AddDbContextCheck<DatabaseContext>();
} }

View File

@ -29,7 +29,7 @@ public class ListTypeHandler<T> : SqlMapper.TypeHandler<List<T>>
public override List<T> Parse(object value) 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) 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(); return typedValue?.ToList() ?? [];
} }
public override void SetValue(IDbDataParameter parameter, List<T>? value) public override void SetValue(IDbDataParameter parameter, List<T>? value)