mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Fix format issues
This commit is contained in:
parent
abffdedf3e
commit
9b8fb7596f
@ -1,10 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Kyoo.Abstractions.Models.Utils;
|
||||
using static System.Text.Json.JsonNamingPolicy;
|
||||
|
||||
namespace Kyoo.Meiliseach;
|
||||
|
||||
public static class FilterExtensionMethods
|
||||
internal static class FilterExtensionMethods
|
||||
{
|
||||
public static string? CreateMeilisearchFilter<T>(this Filter<T>? filter)
|
||||
{
|
||||
@ -14,17 +15,48 @@ public static class FilterExtensionMethods
|
||||
=> $"({and.First.CreateMeilisearchFilter()}) AND ({and.Second.CreateMeilisearchFilter()})",
|
||||
Filter<T>.Or or
|
||||
=> $"({or.First.CreateMeilisearchFilter()}) OR ({or.Second.CreateMeilisearchFilter()})",
|
||||
Filter<T>.Gt gt => $"{CamelCase.ConvertName(gt.Property)} > '{gt.Value}'",
|
||||
Filter<T>.Lt lt => $"{CamelCase.ConvertName(lt.Property)} < '{lt.Value}'",
|
||||
Filter<T>.Ge ge => $"{CamelCase.ConvertName(ge.Property)} >= '{ge.Value}'",
|
||||
Filter<T>.Le le => $"{CamelCase.ConvertName(le.Property)} <= '{le.Value}'",
|
||||
Filter<T>.Eq eq => $"{CamelCase.ConvertName(eq.Property)} = '{eq.Value}'",
|
||||
Filter<T>.Has has => $"{CamelCase.ConvertName(has.Property)} = '{has.Value}'",
|
||||
Filter<T>.Ne ne => $"{CamelCase.ConvertName(ne.Property)} != '{ne.Value}'",
|
||||
Filter<T>.Gt gt => $"{CamelCase.ConvertName(gt.Property)} > {gt.Value.InMeilsearchFilterFormat()}",
|
||||
Filter<T>.Lt lt => $"{CamelCase.ConvertName(lt.Property)} < {lt.Value.InMeilsearchFilterFormat()}",
|
||||
Filter<T>.Ge ge => $"{CamelCase.ConvertName(ge.Property)} >= {ge.Value.InMeilsearchFilterFormat()}",
|
||||
Filter<T>.Le le => $"{CamelCase.ConvertName(le.Property)} <= {le.Value.InMeilsearchFilterFormat()}",
|
||||
Filter<T>.Eq eq => $"{CamelCase.ConvertName(eq.Property)} = {eq.Value.InMeilsearchFilterFormat()}",
|
||||
Filter<T>.Has has => $"{CamelCase.ConvertName(has.Property)} = {has.Value.InMeilsearchFilterFormat()}",
|
||||
Filter<T>.Ne ne => $"{CamelCase.ConvertName(ne.Property)} != {ne.Value.InMeilsearchFilterFormat()}",
|
||||
Filter<T>.Not not => $"NOT ({not.Filter.CreateMeilisearchFilter()})",
|
||||
Filter<T>.CmpRandom
|
||||
=> throw new ValidationException("Random comparison is not supported."),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
private static object? InMeilsearchFilterFormat(this object? value)
|
||||
{
|
||||
return value switch
|
||||
{
|
||||
null => null,
|
||||
string s => s.Any(char.IsWhiteSpace) ? $"\"{s}\"" : s,
|
||||
DateTimeOffset dateTime => dateTime.ToUnixTimeSeconds(),
|
||||
DateOnly date => date.ToUnixTimeSeconds(),
|
||||
_ => value
|
||||
};
|
||||
}
|
||||
|
||||
public static object? InMeilisearchFormat(this object? value)
|
||||
{
|
||||
return value switch
|
||||
{
|
||||
null => null,
|
||||
string => value,
|
||||
Enum => value.ToString(),
|
||||
IEnumerable enumerable => enumerable.Cast<object>().Select(InMeilisearchFormat).ToArray(),
|
||||
DateTimeOffset dateTime => dateTime.ToUnixTimeSeconds(),
|
||||
DateOnly date => date.ToUnixTimeSeconds(),
|
||||
_ => value
|
||||
};
|
||||
}
|
||||
|
||||
private static long ToUnixTimeSeconds(this DateOnly date)
|
||||
{
|
||||
return new DateTimeOffset(date.ToDateTime(new TimeOnly())).ToUnixTimeSeconds();
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ public class MeiliSync
|
||||
var dictionary = (IDictionary<string, object?>)expando;
|
||||
|
||||
foreach (PropertyInfo property in item.GetType().GetProperties())
|
||||
dictionary.Add(CamelCase.ConvertName(property.Name), property.GetValue(item));
|
||||
dictionary.Add(CamelCase.ConvertName(property.Name), property.GetValue(item).InMeilisearchFormat());
|
||||
dictionary.Add("ref", $"{kind}-{item.Id}");
|
||||
expando.kind = kind;
|
||||
return _client.Index(index).AddDocumentsAsync(new[] { expando });
|
||||
|
Loading…
x
Reference in New Issue
Block a user