diff --git a/Controllers/XFileProviderControllerBase.cs b/Controllers/XFileProviderControllerBase.cs new file mode 100644 index 0000000..df66b35 --- /dev/null +++ b/Controllers/XFileProviderControllerBase.cs @@ -0,0 +1,222 @@ +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, 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 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; + } + } + #endregion + } +} \ No newline at end of file diff --git a/DI/XDIHelperExtension.cs b/DI/XDIHelperExtension.cs index 4c2d6d8..0d21dd1 100644 --- a/DI/XDIHelperExtension.cs +++ b/DI/XDIHelperExtension.cs @@ -15,6 +15,8 @@ using xFileService.Interfaces.Dtos; using xFileService.Interfaces.Entities; using xFileService.Providers.Dtos; using xFileService.Providers.Entities; +using xFileService.Interfaces; +using xFileService.Providers; namespace xFileService.DI { @@ -213,6 +215,18 @@ namespace xFileService.DI new ServiceDescriptor(typeof(IXFileRepositoryProvider), typeof(XFileRepositoryProvider), lifeTime) ); + // + // Register Additional Service ... + services.Add( + new ServiceDescriptor(typeof(IXFileTagProvider), typeof(XFileTagProvider), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXFileTagProvided), typeof(XFileTagProvided), lifeTime) + ); + services.Add( + new ServiceDescriptor(typeof(IXFileProvider), typeof(XFileProvider), lifeTime) + ); + // Log("Service Regitration Succeed ..."); } diff --git a/Interfaces/IXFileProviderController.cs b/Interfaces/IXFileProviderController.cs index 0dd108a..32a2052 100644 --- a/Interfaces/IXFileProviderController.cs +++ b/Interfaces/IXFileProviderController.cs @@ -1,12 +1,12 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using xFileService.Models.Dtos; +using xFileService.Models.Entities; +using xFileService.Providers.Dtos; +using xIdentityService.Interfaces; +using xTagService.Interfaces; namespace xFileService.Interfaces { - public interface IXFileProviderController - { - - } + public interface IXFileProviderController : IXBaseProvidedHasTagController, IXBaseReferencedController + { } } \ No newline at end of file diff --git a/Interfaces/IXFileTagProvided.cs b/Interfaces/IXFileTagProvided.cs new file mode 100644 index 0000000..8ff8856 --- /dev/null +++ b/Interfaces/IXFileTagProvided.cs @@ -0,0 +1,11 @@ +using System; +using xFileService.Models.Dtos; +using xFileService.Models.Entities; +using xFileService.Providers.Dtos; +using xTagService.Interfaces; + +namespace xFileService.Interfaces +{ + public interface IXFileTagProvided : IXTagProvided + { } +} \ No newline at end of file diff --git a/Providers/XFileTagProvided.cs b/Providers/XFileTagProvided.cs new file mode 100644 index 0000000..d676604 --- /dev/null +++ b/Providers/XFileTagProvided.cs @@ -0,0 +1,56 @@ +using System; +using xCommons.Extensions; +using xFileService.Interfaces; +using xFileService.Interfaces.Dtos; +using xFileService.Models.Dtos; +using xFileService.Models.Entities; +using xFileService.Providers.Dtos; +using xIdentityModels.Models; +using xTagService.Interfaces; +using xTagService.Providers; + +namespace xFileService.Providers +{ + public class XFileTagProvided : XBaseTagProvided, IXFileTagProvided + { + public XFileTagProvided( + IXFileTagProvider tagProvider, + IXFileServiceProvider provider + ) : base( + provider: provider, + tagProvider: tagProvider, + permittedRoles: new string[] { "admin" } + ) + { } + + public override bool IsOwned( + XFileDto item, + XUserClaimsInfoDto userInfo + ) + { + // + var result = + !item.IsNullOrDefault() && + !userInfo.IsNullOrDefault() && + item.OwnerId == userInfo.UserId; + + // + return result; + } + + public override bool IsOwned( + XFile item, + XUserClaimsInfoDto userInfo + ) + { + // + var result = + !item.IsNullOrDefault() && + !userInfo.IsNullOrDefault() && + item.OwnerId == userInfo.UserId; + + // + return result; + } + } +} \ No newline at end of file diff --git a/Providers/XFileTagProvider.cs b/Providers/XFileTagProvider.cs index 5810890..12a9a50 100644 --- a/Providers/XFileTagProvider.cs +++ b/Providers/XFileTagProvider.cs @@ -9,7 +9,6 @@ namespace xFileService.Providers public class XFileTagProvider : XBaseTagProvider, IXFileTagProvider { public XFileTagProvider( - string providedFor, IXTagServiceProvider tagProvider ) : base( providedFor: nameof(XFile),