Adding more documentation

This commit is contained in:
Zoe Roux 2021-06-06 21:10:00 +02:00
parent c7569691e2
commit 988135ec12
4 changed files with 115 additions and 8 deletions

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Linq.Expressions; using System.Linq.Expressions;
using JetBrains.Annotations;
using Kyoo.Models.Attributes; using Kyoo.Models.Attributes;
namespace Kyoo.Models namespace Kyoo.Models
@ -59,12 +58,23 @@ namespace Kyoo.Models
/// By default, the http path for this poster is returned from the public API. /// By default, the http path for this poster is returned from the public API.
/// This can be disabled using the internal query flag. /// This can be disabled using the internal query flag.
/// </summary> /// </summary>
[SerializeAs("{HOST}/api/{_type}/{Slug}/poster")] public string Poster { get; set; } [SerializeAs("{HOST}/api/{Type}/{Slug}/poster")] public string Poster { get; set; }
[UsedImplicitly] private string _type => Type == ItemType.Collection ? "collection" : "show";
/// <summary>
/// The type of this item (ether a collection, a show or a movie).
/// </summary>
public ItemType Type { get; set; } public ItemType Type { get; set; }
/// <summary>
/// Create a new, empty <see cref="LibraryItem"/>.
/// </summary>
public LibraryItem() {} public LibraryItem() {}
/// <summary>
/// Create a <see cref="LibraryItem"/> from a show.
/// </summary>
/// <param name="show">The show that this library item should represent.</param>
public LibraryItem(Show show) public LibraryItem(Show show)
{ {
ID = show.ID; ID = show.ID;
@ -78,6 +88,10 @@ namespace Kyoo.Models
Type = show.IsMovie ? ItemType.Movie : ItemType.Show; Type = show.IsMovie ? ItemType.Movie : ItemType.Show;
} }
/// <summary>
/// Create a <see cref="LibraryItem"/> from a collection
/// </summary>
/// <param name="collection">The collection that this library item should represent.</param>
public LibraryItem(Collection collection) public LibraryItem(Collection collection)
{ {
ID = -collection.ID; ID = -collection.ID;
@ -91,6 +105,9 @@ namespace Kyoo.Models
Type = ItemType.Collection; Type = ItemType.Collection;
} }
/// <summary>
/// An expression to create a <see cref="LibraryItem"/> representing a show.
/// </summary>
public static Expression<Func<Show, LibraryItem>> FromShow => x => new LibraryItem public static Expression<Func<Show, LibraryItem>> FromShow => x => new LibraryItem
{ {
ID = x.ID, ID = x.ID,
@ -104,6 +121,9 @@ namespace Kyoo.Models
Type = x.IsMovie ? ItemType.Movie : ItemType.Show Type = x.IsMovie ? ItemType.Movie : ItemType.Show
}; };
/// <summary>
/// An expression to create a <see cref="LibraryItem"/> representing a collection.
/// </summary>
public static Expression<Func<Collection, LibraryItem>> FromCollection => x => new LibraryItem public static Expression<Func<Collection, LibraryItem>> FromCollection => x => new LibraryItem
{ {
ID = -x.ID, ID = -x.ID,

View File

@ -3,30 +3,62 @@ using System.Linq.Expressions;
namespace Kyoo.Models namespace Kyoo.Models
{ {
/// <summary>
/// A class representing a link between two resources.
/// </summary>
/// <remarks>
/// Links should only be used on the data layer and not on other application code.
/// </remarks>
public class Link public class Link
{ {
/// <summary>
/// The ID of the first item of the link.
/// The first item of the link should be the one to own the link.
/// </summary>
public int FirstID { get; set; } public int FirstID { get; set; }
/// <summary>
/// The ID of the second item of this link
/// The second item of the link should be the owned resource.
/// </summary>
public int SecondID { get; set; } public int SecondID { get; set; }
/// <summary>
/// Create a new typeless <see cref="Link"/>.
/// </summary>
public Link() {} public Link() {}
/// <summary>
/// Create a new typeless <see cref="Link"/> with two IDs.
/// </summary>
/// <param name="firstID">The ID of the first resource</param>
/// <param name="secondID">The ID of the second resource</param>
public Link(int firstID, int secondID) public Link(int firstID, int secondID)
{ {
FirstID = firstID; FirstID = firstID;
SecondID = secondID; SecondID = secondID;
} }
/// <summary>
/// Create a new typeless <see cref="Link"/> between two resources.
/// </summary>
/// <param name="first">The first resource</param>
/// <param name="second">The second resource</param>
public Link(IResource first, IResource second) public Link(IResource first, IResource second)
{ {
FirstID = first.ID; FirstID = first.ID;
SecondID = second.ID; SecondID = second.ID;
} }
public static Link Create(IResource first, IResource second) /// <summary>
{ /// Create a new typed link between two resources.
return new(first, second); /// This method can be used instead of the constructor to make use of generic parameters deduction.
} /// </summary>
/// <param name="first">The first resource</param>
/// <param name="second">The second resource</param>
/// <typeparam name="T">The type of the first resource</typeparam>
/// <typeparam name="T2">The type of the second resource</typeparam>
/// <returns>A newly created typed link with both resources</returns>
public static Link<T, T2> Create<T, T2>(T first, T2 second) public static Link<T, T2> Create<T, T2>(T first, T2 second)
where T : class, IResource where T : class, IResource
where T2 : class, IResource where T2 : class, IResource
@ -34,6 +66,16 @@ namespace Kyoo.Models
return new(first, second); return new(first, second);
} }
/// <summary>
/// Create a new typed link between two resources without storing references to resources.
/// This is the same as <see cref="Create{T,T2}"/> but this method does not set <see cref="Link{T1,T2}.First"/>
/// and <see cref="Link{T1,T2}.Second"/> fields. Only IDs are stored and not references.
/// </summary>
/// <param name="first">The first resource</param>
/// <param name="second">The second resource</param>
/// <typeparam name="T">The type of the first resource</typeparam>
/// <typeparam name="T2">The type of the second resource</typeparam>
/// <returns>A newly created typed link with both resources</returns>
public static Link<T, T2> UCreate<T, T2>(T first, T2 second) public static Link<T, T2> UCreate<T, T2>(T first, T2 second)
where T : class, IResource where T : class, IResource
where T2 : class, IResource where T2 : class, IResource
@ -41,6 +83,9 @@ namespace Kyoo.Models
return new(first, second, true); return new(first, second, true);
} }
/// <summary>
/// The expression to retrieve the unique ID of a Link. This is an aggregate of the two resources IDs.
/// </summary>
public static Expression<Func<Link, object>> PrimaryKey public static Expression<Func<Link, object>> PrimaryKey
{ {
get get
@ -50,16 +95,41 @@ namespace Kyoo.Models
} }
} }
/// <summary>
/// A strongly typed link between two resources.
/// </summary>
/// <typeparam name="T1">The type of the first resource</typeparam>
/// <typeparam name="T2">The type of the second resource</typeparam>
public class Link<T1, T2> : Link public class Link<T1, T2> : Link
where T1 : class, IResource where T1 : class, IResource
where T2 : class, IResource where T2 : class, IResource
{ {
/// <summary>
/// A reference of the first resource.
/// </summary>
public T1 First { get; set; } public T1 First { get; set; }
/// <summary>
/// A reference to the second resource.
/// </summary>
public T2 Second { get; set; } public T2 Second { get; set; }
/// <summary>
/// Create a new, empty, typed <see cref="Link{T1,T2}"/>.
/// </summary>
public Link() {} public Link() {}
/// <summary>
/// Create a new typed link with two resources.
/// </summary>
/// <param name="first">The first resource</param>
/// <param name="second">The second resource</param>
/// <param name="privateItems">
/// True if no reference to resources should be kept, false otherwise.
/// The default is false (references are kept).
/// </param>
public Link(T1 first, T2 second, bool privateItems = false) public Link(T1 first, T2 second, bool privateItems = false)
: base(first, second) : base(first, second)
{ {
@ -69,10 +139,18 @@ namespace Kyoo.Models
Second = second; Second = second;
} }
/// <summary>
/// Create a new typed link with IDs only.
/// </summary>
/// <param name="firstID">The ID of the first resource</param>
/// <param name="secondID">The ID of the second resource</param>
public Link(int firstID, int secondID) public Link(int firstID, int secondID)
: base(firstID, secondID) : base(firstID, secondID)
{ } { }
/// <summary>
/// The expression to retrieve the unique ID of a typed Link. This is an aggregate of the two resources IDs.
/// </summary>
public new static Expression<Func<Link<T1, T2>, object>> PrimaryKey public new static Expression<Func<Link<T1, T2>, object>> PrimaryKey
{ {
get get

View File

@ -2,9 +2,16 @@ using Kyoo.Models.Attributes;
namespace Kyoo.Models namespace Kyoo.Models
{ {
/// <summary>
/// ID and link of an item on an external provider.
/// </summary>
public class MetadataID public class MetadataID
{ {
/// <summary>
/// The unique ID of this metadata. This is the equivalent of <see cref="IResource.ID"/>.
/// </summary>
[SerializeIgnore] public int ID { get; set; } [SerializeIgnore] public int ID { get; set; }
[SerializeIgnore] public int ProviderID { get; set; } [SerializeIgnore] public int ProviderID { get; set; }
public Provider Provider {get; set; } public Provider Provider {get; set; }

View File

@ -16,6 +16,8 @@ namespace Kyoo.Api
{ {
[Route("api/show")] [Route("api/show")]
[Route("api/shows")] [Route("api/shows")]
[Route("api/movie")]
[Route("api/movies")]
[ApiController] [ApiController]
[PartialPermission(nameof(ShowApi))] [PartialPermission(nameof(ShowApi))]
public class ShowApi : CrudApi<Show> public class ShowApi : CrudApi<Show>