-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: store tmdb keywords and production countries
…because I need them for Shokofin/Jellyfin.
- Loading branch information
Showing
15 changed files
with
292 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
Shoko.Server/Databases/NHIbernate/TmdbProductionCountryConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using NHibernate.SqlTypes; | ||
using NHibernate.UserTypes; | ||
using System.Data; | ||
using System.Data.Common; | ||
using NHibernate; | ||
using NHibernate.Engine; | ||
using System.Globalization; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Shoko.Server.Extensions; | ||
using Shoko.Server.Models.TMDB; | ||
|
||
#nullable enable | ||
namespace Shoko.Server.Databases.NHibernate; | ||
|
||
public class TmdbProductionCountryConverter : TypeConverter, IUserType | ||
{ | ||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType) | ||
=> sourceType?.FullName switch | ||
{ | ||
nameof(List<TMDB_ProductionCountry>) => true, | ||
nameof(String) => true, | ||
_ => false | ||
}; | ||
|
||
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||
=> destinationType?.FullName switch | ||
{ | ||
nameof(String) => true, | ||
_ => false, | ||
}; | ||
|
||
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) | ||
=> value switch | ||
{ | ||
null => [], | ||
string i => i.Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Select(s => TMDB_ProductionCountry.FromString(s)).ToList(), | ||
List<TMDB_ProductionCountry> l => l, | ||
_ => throw new ArgumentException($"DestinationType must be {nameof(String)}.") | ||
}; | ||
|
||
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType) | ||
=> value switch | ||
{ | ||
null => string.Empty, | ||
string i => i, | ||
List<TMDB_ProductionCountry> l => l.Select(r => r.ToString()).Join('|'), | ||
_ => throw new ArgumentException($"DestinationType must be {typeof(List<TMDB_ProductionCountry>).FullName}."), | ||
}; | ||
|
||
public override object CreateInstance(ITypeDescriptorContext? context, IDictionary? propertyValues) | ||
=> true; | ||
|
||
#region IUserType Members | ||
|
||
public object Assemble(object cached, object owner) | ||
=> DeepCopy(cached); | ||
|
||
public object DeepCopy(object value) | ||
=> value; | ||
|
||
public object Disassemble(object value) | ||
=> DeepCopy(value); | ||
|
||
public int GetHashCode(object x) | ||
=> x == null ? base.GetHashCode() : x.GetHashCode(); | ||
|
||
public bool IsMutable | ||
=> true; | ||
|
||
public object? NullSafeGet(DbDataReader rs, string[] names, ISessionImplementor impl, object owner) | ||
=> ConvertFrom(null, null, NHibernateUtil.String.NullSafeGet(rs, names[0], impl)); | ||
|
||
public void NullSafeSet(DbCommand cmd, object value, int index, ISessionImplementor session) | ||
=> ((IDataParameter)cmd.Parameters[index]).Value = value == null ? DBNull.Value : ConvertTo(null, null, value, typeof(List<TMDB_ProductionCountry>)); | ||
|
||
public object Replace(object original, object target, object owner) | ||
=> original; | ||
|
||
public Type ReturnedType | ||
=> typeof(List<TMDB_ProductionCountry>); | ||
|
||
public SqlType[] SqlTypes | ||
=> new[] { NHibernateUtil.String.SqlType }; | ||
|
||
bool IUserType.Equals(object x, object y) | ||
=> ReferenceEquals(x, y) || (x != null && y != null && x.Equals(y)); | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.