#nullable enable
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Jellyfin.Api.Controllers
{
///
/// Display Preferences Controller.
///
[Authenticated]
public class DisplayPreferencesController : BaseJellyfinApiController
{
private readonly IDisplayPreferencesRepository _displayPreferencesRepository;
///
/// Initializes a new instance of the class.
///
/// Instance of interface.
public DisplayPreferencesController(IDisplayPreferencesRepository displayPreferencesRepository)
{
_displayPreferencesRepository = displayPreferencesRepository;
}
///
/// Get Display Preferences.
///
/// Display preferences id.
/// User id.
/// Client.
/// Display Preferences.
[HttpGet("{DisplayPreferencesId}")]
[ProducesResponseType(typeof(DisplayPreferences), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult GetDisplayPreferences(
[FromRoute] string displayPreferencesId,
[FromQuery] [Required] string userId,
[FromQuery] [Required] string client)
{
try
{
var result = _displayPreferencesRepository.GetDisplayPreferences(displayPreferencesId, userId, client);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
///
/// Update Display Preferences.
///
/// Display preferences id.
/// User Id.
/// Client.
/// New Display Preferences object.
/// Status.
[HttpPost("{DisplayPreferencesId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ModelStateDictionary), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
public IActionResult UpdateDisplayPreferences(
[FromRoute] string displayPreferencesId,
[FromQuery, BindRequired] string userId,
[FromQuery, BindRequired] string client,
[FromBody, BindRequired] DisplayPreferences displayPreferences)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (displayPreferencesId == null)
{
// do nothing.
}
_displayPreferencesRepository.SaveDisplayPreferences(
displayPreferences,
userId,
client,
CancellationToken.None);
return Ok();
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
}
}
}
}