2023-01-06 08:45:06 +01:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Umbraco.Cms.Api.Management.ViewModels.Folder;
|
|
|
|
|
|
using Umbraco.Cms.Core;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Cms.Api.Management.Controllers;
|
|
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
public abstract class FolderManagementControllerBase<TStatus> : ManagementApiControllerBase
|
|
|
|
|
|
where TStatus : Enum
|
2023-01-06 08:45:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
protected FolderManagementControllerBase(IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
|
|
|
|
|
|
=> _backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
|
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected async Task<IActionResult> GetFolderAsync(Guid key)
|
2023-01-06 08:45:06 +01:00
|
|
|
|
{
|
2023-02-01 08:38:36 +01:00
|
|
|
|
EntityContainer? container = await GetContainerAsync(key);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
if (container == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return NotFound($"Could not find the folder with key: {key}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
EntityContainer? parentContainer = await GetParentContainerAsync(container);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
|
|
|
|
|
// we could implement a mapper for this but it seems rather overkill at this point
|
2023-03-13 10:49:21 +01:00
|
|
|
|
return Ok(new FolderReponseModel
|
2023-01-06 08:45:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
Name = container.Name!,
|
|
|
|
|
|
Key = container.Key,
|
|
|
|
|
|
ParentKey = parentContainer?.Key
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected async Task<IActionResult> CreateFolderAsync<TCreatedActionController>(
|
2023-03-13 10:49:21 +01:00
|
|
|
|
CreateFolderRequestModel createFolderRequestModel,
|
2023-01-06 08:45:06 +01:00
|
|
|
|
Expression<Func<TCreatedActionController, string>> createdAction)
|
|
|
|
|
|
{
|
2023-03-13 10:49:21 +01:00
|
|
|
|
var container = new EntityContainer(ContainerObjectType) { Name = createFolderRequestModel.Name };
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
Attempt<EntityContainer, TStatus> result = await CreateContainerAsync(
|
|
|
|
|
|
container,
|
2023-03-13 10:49:21 +01:00
|
|
|
|
createFolderRequestModel.ParentKey,
|
2023-01-06 08:45:06 +01:00
|
|
|
|
CurrentUserId(_backOfficeSecurityAccessor));
|
|
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
return result.Success
|
|
|
|
|
|
? CreatedAtAction(createdAction, result.Result.Key)
|
|
|
|
|
|
: OperationStatusResult(result.Status);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-13 10:49:21 +01:00
|
|
|
|
protected async Task<IActionResult> UpdateFolderAsync(Guid key, UpdateFolderReponseModel updateFolderReponseModel)
|
2023-01-06 08:45:06 +01:00
|
|
|
|
{
|
2023-02-01 08:38:36 +01:00
|
|
|
|
EntityContainer? container = await GetContainerAsync(key);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
if (container == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return NotFound($"Could not find the folder with key: {key}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-13 10:49:21 +01:00
|
|
|
|
container.Name = updateFolderReponseModel.Name;
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
Attempt<EntityContainer, TStatus> result = await UpdateContainerAsync(container, CurrentUserId(_backOfficeSecurityAccessor));
|
|
|
|
|
|
return result.Success
|
|
|
|
|
|
? Ok()
|
|
|
|
|
|
: OperationStatusResult(result.Status);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected async Task<IActionResult> DeleteFolderAsync(Guid key)
|
2023-01-06 08:45:06 +01:00
|
|
|
|
{
|
2023-02-01 08:38:36 +01:00
|
|
|
|
Attempt<EntityContainer?, TStatus> result = await DeleteContainerAsync(key, CurrentUserId(_backOfficeSecurityAccessor));
|
|
|
|
|
|
return result.Success
|
|
|
|
|
|
? Ok()
|
|
|
|
|
|
: OperationStatusResult(result.Status);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected abstract Guid ContainerObjectType { get; }
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract Task<EntityContainer?> GetContainerAsync(Guid key);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected abstract Task<EntityContainer?> GetParentContainerAsync(EntityContainer container);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected abstract Task<Attempt<EntityContainer, TStatus>> CreateContainerAsync(EntityContainer container, Guid? parentId, int userId);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected abstract Task<Attempt<EntityContainer, TStatus>> UpdateContainerAsync(EntityContainer container, int userId);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected abstract Task<Attempt<EntityContainer?, TStatus>> DeleteContainerAsync(Guid id, int userId);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
|
2023-02-01 08:38:36 +01:00
|
|
|
|
protected abstract IActionResult OperationStatusResult(TStatus status);
|
2023-01-06 08:45:06 +01:00
|
|
|
|
}
|