// 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 System.Linq.Expressions; using System.Threading.Tasks; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models.Attributes; using Kyoo.Abstractions.Models.Permissions; using Kyoo.Abstractions.Models.Utils; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using static Kyoo.Abstractions.Models.Utils.Constants; namespace Kyoo.Core.Api { /// /// Information about one or multiple . /// [Route("shows")] [Route("show", Order = AlternativeRoute)] [ApiController] [PartialPermission(nameof(Show))] [ApiDefinition("Shows", Group = ResourcesGroup)] public class ShowApi : CrudThumbsApi { /// /// The library manager used to modify or retrieve information in the data store. /// private readonly ILibraryManager _libraryManager; /// /// Create a new . /// /// /// The library manager used to modify or retrieve information about the data store. /// /// The thumbnail manager used to retrieve images paths. public ShowApi(ILibraryManager libraryManager, IThumbnailsManager thumbs) : base(libraryManager.ShowRepository, thumbs) { _libraryManager = libraryManager; } /// /// Get seasons of this show /// /// /// List the seasons that are part of the specified show. /// /// The ID or slug of the . /// A key to sort seasons by. /// An optional list of filters. /// The number of seasons to return. /// A page of seasons. /// The filters or the sort parameters are invalid. /// No show with the given ID or slug could be found. [HttpGet("{identifier:id}/seasons")] [HttpGet("{identifier:id}/season", Order = AlternativeRoute)] [PartialPermission(Kind.Read)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> GetSeasons(Identifier identifier, [FromQuery] string sortBy, [FromQuery] Dictionary where, [FromQuery] Pagination pagination) { ICollection resources = await _libraryManager.GetAll( ApiHelper.ParseWhere(where, identifier.Matcher(x => x.ShowId, x => x.Show!.Slug)), Sort.From(sortBy), pagination ); if (!resources.Any() && await _libraryManager.GetOrDefault(identifier.IsSame()) == null) return NotFound(); return Page(resources, pagination.Limit); } /// /// Get episodes of this show /// /// /// List the episodes that are part of the specified show. /// /// The ID or slug of the . /// A key to sort episodes by. /// An optional list of filters. /// The number of episodes to return. /// A page of episodes. /// The filters or the sort parameters are invalid. /// No show with the given ID or slug could be found. [HttpGet("{identifier:id}/episodes")] [HttpGet("{identifier:id}/episode", Order = AlternativeRoute)] [PartialPermission(Kind.Read)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> GetEpisodes(Identifier identifier, [FromQuery] string sortBy, [FromQuery] Dictionary where, [FromQuery] Pagination pagination) { ICollection resources = await _libraryManager.GetAll( ApiHelper.ParseWhere(where, identifier.Matcher(x => x.ShowId, x => x.Show!.Slug)), Sort.From(sortBy), pagination ); if (!resources.Any() && await _libraryManager.GetOrDefault(identifier.IsSame()) == null) return NotFound(); return Page(resources, pagination.Limit); } /// /// Get staff /// /// /// List staff members that made this show. /// /// The ID or slug of the . /// A key to sort staff members by. /// An optional list of filters. /// The number of people to return. /// A page of people. /// The filters or the sort parameters are invalid. /// No show with the given ID or slug could be found. [HttpGet("{identifier:id}/staff")] [HttpGet("{identifier:id}/people", Order = AlternativeRoute)] [PartialPermission(Kind.Read)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> GetPeople(Identifier identifier, [FromQuery] string sortBy, [FromQuery] Dictionary where, [FromQuery] Pagination pagination) { Expression>? whereQuery = ApiHelper.ParseWhere(where); Sort sort = Sort.From(sortBy); ICollection resources = await identifier.Match( id => _libraryManager.GetPeopleFromShow(id, whereQuery, sort, pagination), slug => _libraryManager.GetPeopleFromShow(slug, whereQuery, sort, pagination) ); return Page(resources, pagination.Limit); } /// /// Get studio that made the show /// /// /// Get the studio that made the show. /// /// The ID or slug of the . /// The studio that made the show. /// No show with the given ID or slug could be found. [HttpGet("{identifier:id}/studio")] [PartialPermission(Kind.Read)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task> GetStudio(Identifier identifier) { return await _libraryManager.Get(identifier.IsContainedIn(x => x.Shows!)); } /// /// Get collections containing this show /// /// /// List the collections that contain this show. /// /// The ID or slug of the . /// A key to sort collections by. /// An optional list of filters. /// The number of collections to return. /// A page of collections. /// The filters or the sort parameters are invalid. /// No show with the given ID or slug could be found. [HttpGet("{identifier:id}/collections")] [HttpGet("{identifier:id}/collection", Order = AlternativeRoute)] [PartialPermission(Kind.Read)] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(RequestError))] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> GetCollections(Identifier identifier, [FromQuery] string sortBy, [FromQuery] Dictionary where, [FromQuery] Pagination pagination) { ICollection resources = await _libraryManager.GetAll( ApiHelper.ParseWhere(where, identifier.IsContainedIn(x => x.Shows!)), Sort.From(sortBy), pagination ); if (!resources.Any() && await _libraryManager.GetOrDefault(identifier.IsSame()) == null) return NotFound(); return Page(resources, pagination.Limit); } } }