// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// Kyoo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see .
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Kyoo.Abstractions.Models.Attributes;
namespace Kyoo.Abstractions.Models
{
///
/// An interface applied to resources containing external metadata.
///
public interface IMetadata
{
///
/// The link to metadata providers that this show has. See for more information.
///
[EditableRelation]
[LoadableRelation]
public ICollection ExternalIDs { get; set; }
}
///
/// A static class containing extensions method for every class.
/// This allow one to use metadata more easily.
///
public static class MetadataExtension
{
///
/// Retrieve the internal provider's ID of an item using it's provider slug.
///
///
/// This method will never return anything if the are not loaded.
///
/// An instance of to retrieve the ID from.
/// The slug of the provider
/// The field of the asked provider.
[CanBeNull]
public static string GetID(this IMetadata self, string provider)
{
return self.ExternalIDs?.FirstOrDefault(x => x.Provider.Slug == provider)?.DataID;
}
///
/// Retrieve the internal provider's ID of an item using it's provider slug.
/// If the ID could be found, it is converted to the type and true is returned.
///
///
/// This method will never succeed if the are not loaded.
///
/// An instance of to retrieve the ID from.
/// The slug of the provider
///
/// The field of the asked provider parsed
/// and converted to the type.
/// It is only relevant if this method returns true.
///
/// The type to convert the to.
/// true if this method succeeded, false otherwise.
public static bool TryGetID(this IMetadata self, string provider, out T id)
{
string dataID = self.ExternalIDs?.FirstOrDefault(x => x.Provider.Slug == provider)?.DataID;
if (dataID == null)
{
id = default;
return false;
}
try
{
id = (T)Convert.ChangeType(dataID, typeof(T));
}
catch
{
id = default;
return false;
}
return true;
}
}
}