jellyfin/Jellyfin.Api/Controllers/MusicGenresController.cs

205 lines
8.8 KiB
C#
Raw Normal View History

2020-11-21 13:26:03 +00:00
using System;
2020-09-05 23:11:44 +00:00
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Jellyfin.Api.Extensions;
using Jellyfin.Api.Helpers;
2020-11-09 21:53:23 +00:00
using Jellyfin.Api.ModelBinders;
using Jellyfin.Data.Entities;
2021-02-11 00:09:23 +00:00
using Jellyfin.Data.Enums;
using Jellyfin.Extensions;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
2020-10-09 23:52:39 +00:00
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
2020-07-06 15:39:45 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers;
/// <summary>
/// The music genres controller.
/// </summary>
2023-02-08 22:55:26 +00:00
[Authorize]
2023-01-31 11:18:10 +00:00
public class MusicGenresController : BaseJellyfinApiController
{
2023-01-31 11:18:10 +00:00
private readonly ILibraryManager _libraryManager;
private readonly IDtoService _dtoService;
private readonly IUserManager _userManager;
/// <summary>
2023-01-31 11:18:10 +00:00
/// Initializes a new instance of the <see cref="MusicGenresController"/> class.
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="libraryManager">Instance of <see cref="ILibraryManager"/> interface.</param>
/// <param name="userManager">Instance of <see cref="IUserManager"/> interface.</param>
/// <param name="dtoService">Instance of <see cref="IDtoService"/> interface.</param>
public MusicGenresController(
ILibraryManager libraryManager,
IUserManager userManager,
IDtoService dtoService)
{
2023-01-31 11:18:10 +00:00
_libraryManager = libraryManager;
_userManager = userManager;
_dtoService = dtoService;
}
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets all music genres from a given item, folder, or the entire library.
/// </summary>
/// <param name="startIndex">Optional. The record index to start at. All items with a lower index will be dropped from the results.</param>
/// <param name="limit">Optional. The maximum number of records to return.</param>
/// <param name="searchTerm">The search term.</param>
/// <param name="parentId">Specify this to localize the search to a specific item or folder. Omit to use the root.</param>
/// <param name="fields">Optional. Specify additional fields of information to return in the output.</param>
/// <param name="excludeItemTypes">Optional. If specified, results will be filtered out based on item type. This allows multiple, comma delimited.</param>
/// <param name="includeItemTypes">Optional. If specified, results will be filtered in based on item type. This allows multiple, comma delimited.</param>
/// <param name="isFavorite">Optional filter by items that are marked as favorite, or not.</param>
/// <param name="imageTypeLimit">Optional, the max number of images to return, per image type.</param>
/// <param name="enableImageTypes">Optional. The image types to include in the output.</param>
/// <param name="userId">User id.</param>
/// <param name="nameStartsWithOrGreater">Optional filter by items whose name is sorted equally or greater than a given input string.</param>
/// <param name="nameStartsWith">Optional filter by items whose name is sorted equally than a given input string.</param>
/// <param name="nameLessThan">Optional filter by items whose name is equally or lesser than a given input string.</param>
/// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited.</param>
/// <param name="sortOrder">Sort Order - Ascending,Descending.</param>
/// <param name="enableImages">Optional, include image information in output.</param>
/// <param name="enableTotalRecordCount">Optional. Include total record count.</param>
/// <response code="200">Music genres returned.</response>
/// <returns>An <see cref="OkResult"/> containing the queryresult of music genres.</returns>
[HttpGet]
[Obsolete("Use GetGenres instead")]
public ActionResult<QueryResult<BaseItemDto>> GetMusicGenres(
[FromQuery] int? startIndex,
[FromQuery] int? limit,
[FromQuery] string? searchTerm,
[FromQuery] Guid? parentId,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemFields[] fields,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] excludeItemTypes,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] BaseItemKind[] includeItemTypes,
[FromQuery] bool? isFavorite,
[FromQuery] int? imageTypeLimit,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ImageType[] enableImageTypes,
[FromQuery] Guid? userId,
[FromQuery] string? nameStartsWithOrGreater,
[FromQuery] string? nameStartsWith,
[FromQuery] string? nameLessThan,
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] ItemSortBy[] sortBy,
2023-01-31 11:18:10 +00:00
[FromQuery, ModelBinder(typeof(CommaDelimitedArrayModelBinder))] SortOrder[] sortOrder,
[FromQuery] bool? enableImages = true,
[FromQuery] bool enableTotalRecordCount = true)
{
2023-02-17 22:16:08 +00:00
userId = RequestHelpers.GetUserId(User, userId);
2023-01-31 11:18:10 +00:00
var dtoOptions = new DtoOptions { Fields = fields }
.AddClientFields(User)
.AddAdditionalDtoOptions(enableImages, false, imageTypeLimit, enableImageTypes);
2020-11-06 08:35:23 +00:00
User? user = userId.IsNullOrEmpty()
2023-01-31 11:18:10 +00:00
? null
: _userManager.GetUserById(userId.Value);
2020-11-06 08:35:23 +00:00
2023-01-31 11:18:10 +00:00
var parentItem = _libraryManager.GetParentItem(parentId, userId);
2020-11-06 08:35:23 +00:00
2023-01-31 11:18:10 +00:00
var query = new InternalItemsQuery(user)
{
ExcludeItemTypes = excludeItemTypes,
IncludeItemTypes = includeItemTypes,
StartIndex = startIndex,
Limit = limit,
IsFavorite = isFavorite,
NameLessThan = nameLessThan,
NameStartsWith = nameStartsWith,
NameStartsWithOrGreater = nameStartsWithOrGreater,
DtoOptions = dtoOptions,
SearchTerm = searchTerm,
EnableTotalRecordCount = enableTotalRecordCount,
OrderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder)
};
if (parentId.HasValue)
{
if (parentItem is Folder)
2020-11-06 08:35:23 +00:00
{
2023-01-31 11:18:10 +00:00
query.AncestorIds = new[] { parentId.Value };
}
else
2020-11-06 08:35:23 +00:00
{
2023-01-31 11:18:10 +00:00
query.ItemIds = new[] { parentId.Value };
2020-11-06 08:35:23 +00:00
}
}
2023-01-31 11:18:10 +00:00
var result = _libraryManager.GetMusicGenres(query);
2023-01-31 11:18:10 +00:00
var shouldIncludeItemTypes = includeItemTypes.Length != 0;
return RequestHelpers.CreateQueryResult(result, dtoOptions, _dtoService, shouldIncludeItemTypes, user);
}
2023-01-31 11:18:10 +00:00
/// <summary>
/// Gets a music genre, by name.
/// </summary>
/// <param name="genreName">The genre name.</param>
/// <param name="userId">Optional. Filter by user id, and attach user data.</param>
/// <returns>An <see cref="OkResult"/> containing a <see cref="BaseItemDto"/> with the music genre.</returns>
[HttpGet("{genreName}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<BaseItemDto> GetMusicGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
{
2023-02-17 22:16:08 +00:00
userId = RequestHelpers.GetUserId(User, userId);
2023-01-31 11:18:10 +00:00
var dtoOptions = new DtoOptions().AddClientFields(User);
2023-01-31 11:18:10 +00:00
MusicGenre? item;
if (genreName.Contains(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase))
2023-01-31 11:18:10 +00:00
{
item = GetItemFromSlugName<MusicGenre>(_libraryManager, genreName, dtoOptions, BaseItemKind.MusicGenre);
}
else
{
item = _libraryManager.GetMusicGenre(genreName);
}
if (item is null)
{
return NotFound();
}
if (!userId.IsNullOrEmpty())
2023-01-31 11:18:10 +00:00
{
var user = _userManager.GetUserById(userId.Value);
return _dtoService.GetBaseItemDto(item, dtoOptions, user);
}
2023-01-31 11:18:10 +00:00
return _dtoService.GetBaseItemDto(item, dtoOptions);
}
private T? GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions, BaseItemKind baseItemKind)
where T : BaseItem, new()
{
var result = libraryManager.GetItemList(new InternalItemsQuery
{
2023-01-31 11:18:10 +00:00
Name = name.Replace(BaseItem.SlugChar, '&'),
IncludeItemTypes = new[] { baseItemKind },
DtoOptions = dtoOptions
}).OfType<T>().FirstOrDefault();
2023-01-31 11:18:10 +00:00
result ??= libraryManager.GetItemList(new InternalItemsQuery
{
Name = name.Replace(BaseItem.SlugChar, '/'),
IncludeItemTypes = new[] { baseItemKind },
DtoOptions = dtoOptions
}).OfType<T>().FirstOrDefault();
2023-01-31 11:18:10 +00:00
result ??= libraryManager.GetItemList(new InternalItemsQuery
{
Name = name.Replace(BaseItem.SlugChar, '?'),
IncludeItemTypes = new[] { baseItemKind },
DtoOptions = dtoOptions
}).OfType<T>().FirstOrDefault();
2023-01-31 11:18:10 +00:00
return result;
}
}