222 lines
7.2 KiB
C#
222 lines
7.2 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore.Query;
|
|
using Microsoft.Extensions.Logging;
|
|
using xCommons.Configurations;
|
|
using xCommons.Extensions;
|
|
using xCommons.Providers;
|
|
using xExceptions.Constants;
|
|
using xFileService.Interfaces;
|
|
using xFileService.Models.Dtos;
|
|
using xFileService.Models.Entities;
|
|
using xFileService.Providers.Dtos;
|
|
using xIdentityService.Interfaces;
|
|
using xTagService.Controllers;
|
|
|
|
namespace xFileService.Controllers
|
|
{
|
|
public abstract class XFileProviderControllerBase : XBaseProvidedHasTagControllerBase<XFile, XFileDto, Guid, XFileDtoHub>, IXFileProviderController
|
|
{
|
|
private readonly IXFileProvider provider;
|
|
|
|
protected XFileProviderControllerBase(
|
|
ILogger<XFileProviderControllerBase> logger,
|
|
XAppConfiguration appConfiguration,
|
|
IXIdentityProvider identityProvider,
|
|
XValidationProvider validationProvider,
|
|
IXFileProvider provider,
|
|
IXFileTagProvided tagProvided,
|
|
Func<IQueryable<XFile>, IOrderedQueryable<XFile>> defaultOrderBuilder = null,
|
|
Func<IQueryable<XFile>, IIncludableQueryable<XFile, object>> defaultIncludeBuilder = null
|
|
) : base(
|
|
logger,
|
|
appConfiguration,
|
|
identityProvider,
|
|
validationProvider,
|
|
provider,
|
|
tagProvided,
|
|
defaultOrderBuilder,
|
|
defaultIncludeBuilder
|
|
)
|
|
{
|
|
this.provider = provider;
|
|
}
|
|
|
|
//
|
|
#region Reference ...
|
|
/// <summary>
|
|
/// Check an Item Has Reference to Specified ProvideFor and Specified forProvidedId ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}/Reference/IsProvidedFor")]
|
|
public virtual async Task<ActionResult<bool>> IsProvidedFor(
|
|
[FromRoute] Guid id,
|
|
[FromQuery] string providedFor,
|
|
[FromQuery] object forProvidedId,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (!ModelState.IsValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
await ValidationProvider
|
|
.GroupValidationBuilder()
|
|
.AddNotNull(id)
|
|
.AddNotEmpty(providedFor)
|
|
.AddNotNull(forProvidedId)
|
|
.ValidateGroupAsync();
|
|
|
|
//
|
|
var result = await provider.IsProvidedFor(
|
|
id: id,
|
|
providedFor: providedFor,
|
|
forProvidedId: forProvidedId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a Reference to Item for Specified ProvideFor and Specified forProvidedId ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="forIndex"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("{id}/Reference")]
|
|
public virtual async Task<ActionResult> AddReference(
|
|
[FromRoute] Guid id,
|
|
[FromQuery] string providedFor,
|
|
[FromQuery] object forProvidedId,
|
|
[FromQuery] int forIndex = 0,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (!ModelState.IsValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
await ValidationProvider
|
|
.GroupValidationBuilder()
|
|
.AddNotNull(id)
|
|
.AddNotEmpty(providedFor)
|
|
.AddNotNull(forProvidedId)
|
|
.ValidateGroupAsync();
|
|
|
|
//
|
|
var userInfo = await GetUserInfo();
|
|
var connectionId = GetConnectionId();
|
|
|
|
//
|
|
await provider.AddReference(
|
|
id: id,
|
|
forIndex: forIndex,
|
|
userInfo: userInfo,
|
|
providedFor: providedFor,
|
|
connectionId: connectionId,
|
|
forProvidedId: forProvidedId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Remove a Reference from Item for Specified ProvideFor and Specified forProvidedId ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="providedFor"></param>
|
|
/// <param name="forProvidedId"></param>
|
|
/// <param name="forIndex"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{id}/Reference")]
|
|
public virtual async Task<ActionResult> RemoveReference(
|
|
[FromRoute] Guid id,
|
|
[FromQuery] string providedFor,
|
|
[FromQuery] object forProvidedId,
|
|
[FromQuery] int forIndex = 0,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
// Validate ...
|
|
if (!ModelState.IsValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
await ValidationProvider
|
|
.GroupValidationBuilder()
|
|
.AddNotNull(id)
|
|
.AddNotEmpty(providedFor)
|
|
.AddNotNull(forProvidedId)
|
|
.ValidateGroupAsync();
|
|
|
|
//
|
|
var userInfo = await GetUserInfo();
|
|
var connectionId = GetConnectionId();
|
|
|
|
//
|
|
await provider.RemoveReference(
|
|
id: id,
|
|
forIndex: forIndex,
|
|
userInfo: userInfo,
|
|
providedFor: providedFor,
|
|
connectionId: connectionId,
|
|
forProvidedId: forProvidedId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
return Ok();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//
|
|
var result = GetExceptionActionResult(ex);
|
|
return result;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |