move InMeilisearchFormat to MeiliSync.cs

This commit is contained in:
Scott Merchant 2024-06-10 18:34:35 +09:30
parent 505c51923f
commit d683943eb3
2 changed files with 18 additions and 18 deletions

View File

@ -1,4 +1,3 @@
using System.Collections;
using System.ComponentModel.DataAnnotations;
using Kyoo.Abstractions.Models.Utils;
using static System.Text.Json.JsonNamingPolicy;
@ -46,22 +45,7 @@ internal static class FilterExtensionMethods
};
}
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)
public static long ToUnixTimeSeconds(this DateOnly date)
{
return new DateTimeOffset(date.ToDateTime(new TimeOnly())).ToUnixTimeSeconds();
}

View File

@ -16,6 +16,7 @@
// 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.Collections;
using System.Dynamic;
using System.Reflection;
using Kyoo.Abstractions.Controllers;
@ -62,7 +63,7 @@ public class MeiliSync
foreach (PropertyInfo property in item.GetType().GetProperties())
dictionary.Add(
CamelCase.ConvertName(property.Name),
property.GetValue(item).InMeilisearchFormat()
ConvertToMeilisearchFormat(property.GetValue(item))
);
dictionary.Add("ref", $"{kind}-{item.Id}");
expando.kind = kind;
@ -79,4 +80,19 @@ public class MeiliSync
}
return _client.Index(index).DeleteOneDocumentAsync(id.ToString());
}
private object? ConvertToMeilisearchFormat(object? value)
{
return value switch
{
null => null,
string => value,
Enum => value.ToString(),
IEnumerable enumerable
=> enumerable.Cast<object>().Select(ConvertToMeilisearchFormat).ToArray(),
DateTimeOffset dateTime => dateTime.ToUnixTimeSeconds(),
DateOnly date => date.ToUnixTimeSeconds(),
_ => value
};
}
}