using MediaBrowser.Common.Implementations.HttpServer;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using ServiceStack.ServiceHost;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MediaBrowser.Api.Library
{
///
/// Class GetDefaultVirtualFolders
///
[Route("/Library/VirtualFolders", "GET")]
[Route("/Users/{UserId}/VirtualFolders", "GET")]
public class GetVirtualFolders : IReturn>
{
///
/// Gets or sets the user id.
///
/// The user id.
public string UserId { get; set; }
}
[Route("/Library/VirtualFolders/{Name}", "POST")]
[Route("/Users/{UserId}/VirtualFolders/{Name}", "POST")]
public class AddVirtualFolder : IReturnVoid
{
///
/// Gets or sets the user id.
///
/// The user id.
public string UserId { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string Name { get; set; }
}
[Route("/Library/VirtualFolders/{Name}", "DELETE")]
[Route("/Users/{UserId}/VirtualFolders/{Name}", "DELETE")]
public class RemoveVirtualFolder : IReturnVoid
{
///
/// Gets or sets the user id.
///
/// The user id.
public string UserId { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string Name { get; set; }
}
[Route("/Library/VirtualFolders/{Name}/Name", "POST")]
[Route("/Users/{UserId}/VirtualFolders/{Name}/Name", "POST")]
public class RenameVirtualFolder : IReturnVoid
{
///
/// Gets or sets the user id.
///
/// The user id.
public string UserId { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string Name { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string NewName { get; set; }
}
[Route("/Library/VirtualFolders/{Name}/Paths", "POST")]
[Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "POST")]
public class AddMediaPath : IReturnVoid
{
///
/// Gets or sets the user id.
///
/// The user id.
public string UserId { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string Name { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string Path { get; set; }
}
[Route("/Library/VirtualFolders/{Name}/Paths", "DELETE")]
[Route("/Users/{UserId}/VirtualFolders/{Name}/Paths", "DELETE")]
public class RemoveMediaPath : IReturnVoid
{
///
/// Gets or sets the user id.
///
/// The user id.
public string UserId { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string Name { get; set; }
///
/// Gets or sets the name.
///
/// The name.
public string Path { get; set; }
}
///
/// Class LibraryStructureService
///
public class LibraryStructureService : BaseRestService
{
///
/// The _app paths
///
private readonly IServerApplicationPaths _appPaths;
///
/// The _user manager
///
private readonly IUserManager _userManager;
///
/// The _library manager
///
private readonly ILibraryManager _libraryManager;
///
/// Initializes a new instance of the class.
///
/// The app paths.
/// The user manager.
/// appHost
public LibraryStructureService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager)
{
if (appPaths == null)
{
throw new ArgumentNullException("appPaths");
}
_userManager = userManager;
_appPaths = appPaths;
_libraryManager = libraryManager;
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetVirtualFolders request)
{
if (string.IsNullOrEmpty(request.UserId))
{
var result = _libraryManager.GetDefaultVirtualFolders().ToList();
return ToOptimizedResult(result);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
var result = _libraryManager.GetVirtualFolders(user).ToList();
return ToOptimizedResult(result);
}
}
///
/// Posts the specified request.
///
/// The request.
public void Post(AddVirtualFolder request)
{
if (string.IsNullOrEmpty(request.UserId))
{
LibraryHelpers.AddVirtualFolder(request.Name, null, _appPaths);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
LibraryHelpers.AddVirtualFolder(request.Name, user, _appPaths);
}
}
///
/// Posts the specified request.
///
/// The request.
public void Post(RenameVirtualFolder request)
{
if (string.IsNullOrEmpty(request.UserId))
{
LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, null, _appPaths);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
LibraryHelpers.RenameVirtualFolder(request.Name, request.NewName, user, _appPaths);
}
}
///
/// Deletes the specified request.
///
/// The request.
public void Delete(RemoveVirtualFolder request)
{
if (string.IsNullOrEmpty(request.UserId))
{
LibraryHelpers.RemoveVirtualFolder(request.Name, null, _appPaths);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
LibraryHelpers.RemoveVirtualFolder(request.Name, user, _appPaths);
}
}
///
/// Posts the specified request.
///
/// The request.
public void Post(AddMediaPath request)
{
if (string.IsNullOrEmpty(request.UserId))
{
LibraryHelpers.AddMediaPath(request.Name, request.Path, null, _appPaths);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
LibraryHelpers.AddMediaPath(request.Name, request.Path, user, _appPaths);
}
}
///
/// Deletes the specified request.
///
/// The request.
public void Delete(RemoveMediaPath request)
{
if (string.IsNullOrEmpty(request.UserId))
{
LibraryHelpers.RemoveMediaPath(request.Name, request.Path, null, _appPaths);
}
else
{
var user = _userManager.GetUserById(new Guid(request.UserId));
LibraryHelpers.RemoveMediaPath(request.Name, request.Path, user, _appPaths);
}
}
}
}