2020-07-05 02:04:14 -07:00
|
|
|
|
using System;
|
2020-09-05 16:11:44 -07:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-07-05 02:04:14 -07:00
|
|
|
|
using System.Linq;
|
2020-07-06 08:39:45 -07:00
|
|
|
|
using Jellyfin.Api.Constants;
|
2020-07-05 02:04:14 -07:00
|
|
|
|
using Jellyfin.Api.Extensions;
|
|
|
|
|
using Jellyfin.Api.Helpers;
|
|
|
|
|
using Jellyfin.Data.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
2020-10-09 16:52:39 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2020-07-05 02:04:14 -07:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2020-07-06 08:39:45 -07:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-07-05 02:04:14 -07:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The music genres controller.
|
|
|
|
|
/// </summary>
|
2020-07-06 08:39:45 -07:00
|
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-07-05 02:04:14 -07:00
|
|
|
|
public class MusicGenresController : BaseJellyfinApiController
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="MusicGenresController"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_dtoService = dtoService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)]
|
2020-09-06 08:07:27 -07:00
|
|
|
|
public ActionResult<BaseItemDto> GetMusicGenre([FromRoute, Required] string genreName, [FromQuery] Guid? userId)
|
2020-07-05 02:04:14 -07:00
|
|
|
|
{
|
|
|
|
|
var dtoOptions = new DtoOptions().AddClientFields(Request);
|
|
|
|
|
|
|
|
|
|
MusicGenre item;
|
|
|
|
|
|
|
|
|
|
if (genreName.IndexOf(BaseItem.SlugChar, StringComparison.OrdinalIgnoreCase) != -1)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemFromSlugName<MusicGenre>(_libraryManager, genreName, dtoOptions);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item = _libraryManager.GetMusicGenre(genreName);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 08:10:51 -07:00
|
|
|
|
if (userId.HasValue && !userId.Equals(Guid.Empty))
|
2020-07-05 02:04:14 -07:00
|
|
|
|
{
|
2020-07-07 08:10:51 -07:00
|
|
|
|
var user = _userManager.GetUserById(userId.Value);
|
2020-07-05 02:04:14 -07:00
|
|
|
|
|
|
|
|
|
return _dtoService.GetBaseItemDto(item, dtoOptions, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _dtoService.GetBaseItemDto(item, dtoOptions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private T GetItemFromSlugName<T>(ILibraryManager libraryManager, string name, DtoOptions dtoOptions)
|
|
|
|
|
where T : BaseItem, new()
|
|
|
|
|
{
|
|
|
|
|
var result = libraryManager.GetItemList(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
Name = name.Replace(BaseItem.SlugChar, '&'),
|
|
|
|
|
IncludeItemTypes = new[] { typeof(T).Name },
|
|
|
|
|
DtoOptions = dtoOptions
|
|
|
|
|
}).OfType<T>().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
result ??= libraryManager.GetItemList(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
Name = name.Replace(BaseItem.SlugChar, '/'),
|
|
|
|
|
IncludeItemTypes = new[] { typeof(T).Name },
|
|
|
|
|
DtoOptions = dtoOptions
|
|
|
|
|
}).OfType<T>().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
result ??= libraryManager.GetItemList(new InternalItemsQuery
|
|
|
|
|
{
|
|
|
|
|
Name = name.Replace(BaseItem.SlugChar, '?'),
|
|
|
|
|
IncludeItemTypes = new[] { typeof(T).Name },
|
|
|
|
|
DtoOptions = dtoOptions
|
|
|
|
|
}).OfType<T>().FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|