#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.Attributes;
using Lumina.Presentation.Web.Common.Enums.Authorization;
using Lumina.Presentation.Web.Common.Models.Common;
using Lumina.Presentation.Web.Common.Models.Libraries;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.Controllers.Library.Management;
///
/// Controller for media libraries related operations.
///
[Authorize]
[Route("{culture}/libraries/manage")]
public class LibraryManagementController : Controller
{
private readonly IApiHttpClient _apiHttpClient;
///
/// Initializes a new instance of the class.
///
/// Injected service for interactions with the API.
public LibraryManagementController(IApiHttpClient apiHttpClient)
{
_apiHttpClient = apiHttpClient;
}
///
/// Displays the view for displaying the list of media libraries.
///
/// Cancellation token that can be used to stop the execution.
[HttpGet("")]
public IActionResult Index()
{
return View("/Views/Library/Management/Index.cshtml");
}
///
/// Displays the view for adding a media library.
///
[HttpGet("item")]
public IActionResult AddLibrary()
{
LibraryModel library = new();
return View("/Views/Library/Management/Item.cshtml", library);
}
///
/// Displays the view for editing a media library.
///
/// The id of the media library to edit.
/// Cancellation token that can be used to stop the execution.
[HttpGet("item/{id}")]
public async Task EditLibrary(Guid id, CancellationToken cancellationToken = default)
{
LibraryModel response = await _apiHttpClient.GetAsync($"libraries/{id}", cancellationToken).ConfigureAwait(false);
return View("/Views/Library/Management/Item.cshtml", response);
}
///
/// Gets the list of media libraries.
///
/// Cancellation token that can be used to stop the execution.
[HttpGet("api-get-libraries")]
public async Task GetLibraries(CancellationToken cancellationToken = default)
{
LibraryModel[] response = await _apiHttpClient.GetAsync($"libraries/", cancellationToken).ConfigureAwait(false);
return Json(new { success = true, data = response });
}
///
/// Gets the list of enabled media libraries.
///
/// Cancellation token that can be used to stop the execution.
[HttpGet("api-get-enabled-libraries")]
public async Task GetEnabledLibraries(CancellationToken cancellationToken = default)
{
LibraryModel[] response = await _apiHttpClient.GetAsync($"libraries/enabled/", cancellationToken).ConfigureAwait(false);
return Json(new { success = true, data = response });
}
///
/// Adds a new media library, or updates an existing one.
///
/// The model containing the new library data.
/// Cancellation token that can be used to stop the execution.
[ValidateAntiForgeryToken]
[HttpPost("api-item")]
[RequirePermission(AuthorizationPermission.CanCreateLibraries)]
public async Task SaveLibrary([FromBody] LibraryModel data, CancellationToken cancellationToken)
{
// call different API endpoints based on whether this is a new library or an existing one
LibraryModel response = data.Id.HasValue
? await _apiHttpClient.PutAsync($"libraries/{data.Id}", data, cancellationToken).ConfigureAwait(false)
: await _apiHttpClient.PostAsync("libraries", data, cancellationToken).ConfigureAwait(false);
return Json(new { success = true, data = response });
}
///
/// Deletes a media library.
///
/// The id of the media library to delete.
/// Cancellation token that can be used to stop the execution.
[HttpDelete("api-item/{id}")]
public async Task DeleteLibrary(Guid id, CancellationToken cancellationToken = default)
{
await _apiHttpClient.DeleteAsync($"libraries/{id}", cancellationToken).ConfigureAwait(false);
return Json(new { success = true });
}
///
/// Gets the list of ongoing media library scans.
///
/// Cancellation token that can be used to stop the execution.
[HttpGet("api-get-running-library-scans")]
public async Task GetRunningLibraryScans(CancellationToken cancellationToken = default)
{
LibraryScanProgressModel[] response = await _apiHttpClient.GetAsync($"libraries/scans/running/", cancellationToken).ConfigureAwait(false);
return Json(new { success = true, data = response });
}
///
/// Initiates the scan of all media libraries.
///
/// Cancellation token that can be used to stop the execution.
[HttpPost("api-scan-libraries")]
public async Task ScanAllLibraries(CancellationToken cancellationToken = default)
{
ScanLibraryModel[] response = await _apiHttpClient.PostAsync($"libraries/scans", new EmptyModel(), cancellationToken).ConfigureAwait(false);
return Json(new { success = true, data = response });
}
///
/// Initiates the scan of a media library.
///
/// The id of the media library whose scan is started.
/// Cancellation token that can be used to stop the execution.
[HttpPost("api-scan-library/{id}")]
public async Task ScanLibrary(Guid id, CancellationToken cancellationToken = default)
{
ScanLibraryModel response = await _apiHttpClient.PostAsync($"libraries/{id}/scans", new EmptyModel(), cancellationToken).ConfigureAwait(false);
return Json(new { success = true, data = response });
}
///
/// Cancels the previously started scan of all media libraries.
///
/// Cancellation token that can be used to stop the execution.
[HttpPost("api-cancel-libraries-scan")]
public async Task CancelLibrariesScan(CancellationToken cancellationToken = default)
{
await _apiHttpClient.PostAsync($"libraries/scans/cancel", new EmptyModel(), cancellationToken).ConfigureAwait(false);
return Json(new { success = true });
}
///
/// Cancels the previously started scan of a media library.
///
/// The id of the media library whose scan is cancelled.
/// The id of the scan to cancel.
/// Cancellation token that can be used to stop the execution.
[HttpPost("{libraryId:guid}/api-cancel-library-scan/{scanId:guid}")]
public async Task CancelLibraryScan(Guid libraryId, Guid scanId, CancellationToken cancellationToken = default)
{
await _apiHttpClient.PostAsync($"libraries/{libraryId}/scans/{scanId}/cancel", new EmptyModel(), cancellationToken).ConfigureAwait(false);
return Json(new { success = true });
}
}