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>
/// A list of tags that match this movie.
/// </summary>
public string[] Tags { get; set; } = Array.Empty<string>();
public string[] Tags { get; set; } = [];
/// <summary>
/// The list of genres (themes) this show has.
/// </summary>
public Genre[] Genres { get; set; } = Array.Empty<Genre>();
public List<Genre> Genres { get; set; } = [];
/// <summary>
/// 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/>.
using System.Data.Common;
using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@ -56,7 +56,7 @@ public class PostgresModule(IConfiguration configuration, IWebHostEnvironment en
dsBuilder.MapEnum<Status>();
dsBuilder.MapEnum<Genre>();
dsBuilder.MapEnum<WatchStatus>();
var dataSource = dsBuilder.Build();
NpgsqlDataSource dataSource = dsBuilder.Build();
services.AddDbContext<DatabaseContext, PostgresContext>(
x =>
@ -67,7 +67,9 @@ public class PostgresModule(IConfiguration configuration, IWebHostEnvironment en
},
ServiceLifetime.Transient
);
services.AddTransient<DbConnection>((_) => new NpgsqlConnection(builder.ConnectionString));
services.AddTransient(
(services) => services.GetRequiredService<DatabaseContext>().Database.GetDbConnection()
);
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)
{
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)