using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore.Query; using Microsoft.Extensions.Logging; using xCommons.Configurations; using xCommons.Extensions; using xCommons.Providers; using xDataService.Models; 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, IXFileProviderController { private readonly IXFileProvider provider; protected XFileProviderControllerBase( ILogger logger, XAppConfiguration appConfiguration, IXIdentityProvider identityProvider, XValidationProvider validationProvider, IXFileProvider provider, IXFileTagProvided tagProvided, Func, IOrderedQueryable> defaultOrderBuilder = null, Func, IIncludableQueryable> defaultIncludeBuilder = null ) : base( logger, appConfiguration, identityProvider, validationProvider, provider, tagProvided, defaultOrderBuilder, defaultIncludeBuilder ) { this.provider = provider; } // #region Tools ... /// /// Stream Specified File ... /// /// /// /// [HttpGet("{id}/Stream/ById")] public virtual async Task Stream( [FromRoute] Guid id, CancellationToken cancellationToken = default ) { // try { // var result = await provider.Stream( id: id, cancellationToken: cancellationToken ); // return result; } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Stream Specified File ... /// /// /// /// [HttpGet("{name}/Stream/ByName")] public virtual async Task Stream( [FromRoute] string name, CancellationToken cancellationToken = default ) { // try { // var result = await provider.Stream( name: name, cancellationToken: cancellationToken ); // return result; } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Download Specified File ... /// /// /// /// [HttpGet("{id}/Download/ById")] public virtual async Task Download( [FromRoute] Guid id, CancellationToken cancellationToken = default ) { // try { // var result = await provider.Download( id: id, cancellationToken: cancellationToken ); // return result; } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Download Specified File ... /// /// /// /// [HttpGet("{name}/Download/ByName")] public virtual async Task Download( string name, CancellationToken cancellationToken = default ) { // try { // var result = await provider.Download( name: name, cancellationToken: cancellationToken ); // return result; } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Upload Files ... /// /// /// /// [HttpPost("Upload")] [RequestSizeLimit(966_367_641)] public virtual async Task>> Upload( [FromForm] IFormFileCollection files, CancellationToken cancellationToken = default ) { // try { // // Retrieve User Info ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await provider.Upload( files: files, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Remove Specified Files ... /// /// /// /// /// /// [HttpDelete("")] public virtual async Task>> Remove( [FromQuery] string ids, CancellationToken cancellationToken = default ) { // try { // // Retrieve User Info ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await provider.Remove( ids: ids, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion // #region Reference ... /// /// Check an Item Has Reference to Specified ProvideFor and Specified forProvidedId ... /// /// /// /// /// /// [HttpGet("{id}/Reference/IsProvidedFor")] public virtual async Task> 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; } } /// /// Add a Reference to Item for Specified ProvideFor and Specified forProvidedId ... /// /// /// /// /// /// /// [HttpPost("{id}/Reference")] public virtual async Task 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; } } /// /// Remove a Reference from Item for Specified ProvideFor and Specified forProvidedId ... /// /// /// /// /// /// /// [HttpDelete("{id}/Reference")] public virtual async Task 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; } } /// /// Retrieve References of Specified Itemfor providedFor ... /// /// /// /// /// /// [HttpGet("{id}/References")] public virtual async Task>>> GetReferences( [FromRoute] Guid id, [FromQuery] string providedFor, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotNull(id) .AddNotEmpty(providedFor) .ValidateGroupAsync(); // var result = await provider.GetReferences( id: id, providedFor: providedFor, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion } }