API: Documenting the track's api

This commit is contained in:
Zoe Roux 2021-09-23 13:49:25 +02:00
parent e4d703223c
commit 38ec742db6
3 changed files with 56 additions and 40 deletions

View File

@ -52,7 +52,7 @@ namespace Kyoo.Abstractions.Models
Subtitle = 3, Subtitle = 3,
/// <summary> /// <summary>
/// The stream is an attachement (a font, an image or something else). /// The stream is an attachment (a font, an image or something else).
/// Only fonts are handled by kyoo but they are not saved to the database. /// Only fonts are handled by kyoo but they are not saved to the database.
/// </summary> /// </summary>
Attachment = 4 Attachment = 4

View File

@ -16,58 +16,69 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models;
using Kyoo.Abstractions.Models.Exceptions; using Kyoo.Abstractions.Models.Attributes;
using Kyoo.Abstractions.Models.Permissions; using Kyoo.Abstractions.Models.Permissions;
using Kyoo.Abstractions.Models.Utils;
using Kyoo.Core.Models.Options; using Kyoo.Core.Models.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using static Kyoo.Abstractions.Models.Utils.Constants;
namespace Kyoo.Core.Api namespace Kyoo.Core.Api
{ {
[Route("api/track")] /// <summary>
/// Information about one or multiple <see cref="Track"/>.
/// </summary>
[Route("api/tracks")] [Route("api/tracks")]
[Route("api/track", Order = AlternativeRoute)]
[ApiController] [ApiController]
[PartialPermission(nameof(Track))] [PartialPermission(nameof(Track))]
[ApiDefinition("Tracks", Group = ResourcesGroup)]
public class TrackApi : CrudApi<Track> public class TrackApi : CrudApi<Track>
{ {
/// <summary>
/// The library manager used to modify or retrieve information in the data store.
/// </summary>
private readonly ILibraryManager _libraryManager; private readonly ILibraryManager _libraryManager;
/// <summary>
/// Create a new <see cref="TrackApi"/>.
/// </summary>
/// <param name="libraryManager">
/// The library manager used to modify or retrieve information in the data store.
/// </param>
/// <param name="options">
/// Options used to retrieve the base URL of Kyoo.
/// </param>
public TrackApi(ILibraryManager libraryManager, IOptions<BasicOptions> options) public TrackApi(ILibraryManager libraryManager, IOptions<BasicOptions> options)
: base(libraryManager.TrackRepository, options.Value.PublicUrl) : base(libraryManager.TrackRepository, options.Value.PublicUrl)
{ {
_libraryManager = libraryManager; _libraryManager = libraryManager;
} }
[HttpGet("{id:int}/episode")] /// <summary>
/// Get track's episode
/// </summary>
/// <remarks>
/// Get the episode that uses this track.
/// </remarks>
/// <param name="identifier">The ID or slug of the <see cref="Track"/>.</param>
/// <returns>The episode that uses this track.</returns>
/// <response code="404">No track with the given ID or slug could be found.</response>
[HttpGet("{identifier:id}/episode")]
[PartialPermission(Kind.Read)] [PartialPermission(Kind.Read)]
public async Task<ActionResult<Episode>> GetEpisode(int id) [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Episode>> GetEpisode(Identifier identifier)
{ {
try Episode ret = await _libraryManager.GetOrDefault(identifier.IsContainedIn<Episode, Track>(x => x.Tracks));
{ if (ret == null)
return await _libraryManager.Get<Episode>(x => x.Tracks.Any(y => y.ID == id));
}
catch (ItemNotFoundException)
{
return NotFound(); return NotFound();
} return ret;
}
[HttpGet("{slug}/episode")]
[PartialPermission(Kind.Read)]
public async Task<ActionResult<Episode>> GetEpisode(string slug)
{
try
{
return await _libraryManager.Get<Episode>(x => x.Tracks.Any(y => y.Slug == slug));
}
catch (ItemNotFoundException)
{
return NotFound();
}
} }
} }
} }

View File

@ -241,20 +241,25 @@ namespace Kyoo.Tests
}, },
{ {
typeof(Track), typeof(Track),
() => new Track () =>
{ {
ID = 1, Track ret = new()
EpisodeID = 1, {
Codec = "subrip", ID = 1,
Language = "eng", EpisodeID = 1,
Path = "/path", Codec = "subrip",
Title = "Subtitle track", Language = "eng",
Type = StreamType.Subtitle, Path = "/path",
EpisodeSlug = Get<Episode>().Slug, Title = "Subtitle track",
IsDefault = true, Episode = Get<Episode>(),
IsExternal = false, Type = StreamType.Subtitle,
IsForced = false, IsDefault = true,
TrackIndex = 1 IsExternal = false,
IsForced = false,
TrackIndex = 1
};
ret.Episode = null;
return ret;
} }
}, },
{ {