Files
mixtape/zero.Web/Controllers/MediaController.cs
T

105 lines
2.7 KiB
C#
Raw Normal View History

2020-05-31 15:01:28 +02:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
2020-05-31 13:39:47 +02:00
using System;
2020-05-15 14:23:47 +02:00
using System.Collections.Generic;
2020-05-18 12:31:58 +02:00
using System.Linq;
2020-05-15 14:23:47 +02:00
using System.Threading.Tasks;
using zero.Core.Api;
using zero.Core.Entities;
using zero.Core.Identity;
using zero.Core.Options;
using zero.Web.Models;
namespace zero.Web.Controllers
{
[ZeroAuthorize(Permissions.Sections.Media, PermissionsValue.True)]
public class MediaController : BackofficeController
{
IMediaApi Api;
2020-05-18 11:53:23 +02:00
IMediaFolderApi MediaFolderApi;
2020-05-31 15:01:28 +02:00
IMediaUploadApi MediaUploadApi;
2020-05-18 11:53:23 +02:00
2020-05-31 15:01:28 +02:00
public MediaController(IMediaApi api, IMediaFolderApi mediaFolderApi, IMediaUploadApi mediaUploadApi)
2020-05-15 14:23:47 +02:00
{
Api = api;
2020-05-18 11:53:23 +02:00
MediaFolderApi = mediaFolderApi;
2020-05-31 15:01:28 +02:00
MediaUploadApi = mediaUploadApi;
2020-05-15 14:23:47 +02:00
}
/// <summary>
/// Get media item by id
/// </summary>
public async Task<IActionResult> GetById([FromQuery] string id)
{
return await As<Media, MediaEditModel>(await Api.GetById(id));
}
2020-06-02 12:08:38 +02:00
/// <summary>
/// Get media item by id
/// </summary>
public async Task<IActionResult> GetByIds([FromQuery] string[] ids)
{
return Json(await Api.GetById(ids));
}
2020-05-15 14:23:47 +02:00
/// <summary>
/// Get all media items + folders
/// </summary>
public async Task<IActionResult> GetAll([FromQuery] MediaListQuery query)
{
ListResult<MediaListModel> items = await Mapper.Map<Media, MediaListModel>(await Api.GetByQuery(query));
2020-06-02 12:52:58 +02:00
IList<MediaFolder> hierarchy = null;
2020-05-18 13:25:32 +02:00
IEnumerable<MediaListModel> folders = new List<MediaListModel>();
2020-05-31 13:39:47 +02:00
MediaFolder folder = null;
2020-05-15 14:23:47 +02:00
2020-05-18 12:31:58 +02:00
if (query.Page < 2)
{
2020-05-18 13:25:32 +02:00
folders = await Mapper.Map<MediaFolder, MediaListModel>(await MediaFolderApi.GetAll(query.FolderId));
2020-05-18 12:31:58 +02:00
}
2020-05-31 13:39:47 +02:00
if (!String.IsNullOrEmpty(query.FolderId))
{
folder = await MediaFolderApi.GetById(query.FolderId);
2020-06-02 12:52:58 +02:00
hierarchy = await MediaFolderApi.GetHierarchy(query.FolderId);
2020-05-31 13:39:47 +02:00
}
2020-06-02 12:52:58 +02:00
return Json(new MediaListResultModel(items, folders, folder, hierarchy));
2020-05-15 14:23:47 +02:00
}
/// <summary>
/// Save a media item
/// </summary>
public async Task<IActionResult> Save([FromBody] MediaEditModel model)
{
Media entity = await Mapper.Map(model, await Api.GetById(model.Id));
return await As<Media, MediaEditModel>(await Api.Save(entity));
}
2020-05-31 15:01:28 +02:00
/// <summary>
/// Upload a file
/// </summary>
public async Task<IActionResult> Upload(IFormFile file, string folderId)
{
Media media = await MediaUploadApi.Upload(file, folderId);
return Json(await Api.Save(media));
}
2020-05-15 14:23:47 +02:00
/// <summary>
/// Deletes a media item
/// </summary>
public async Task<IActionResult> Delete([FromQuery] string id)
{
return await As<Media, MediaEditModel>(await Api.Delete(id));
}
}
}