From 65ba20c98d9e22bea156f9ec5c939b1e14cc3853 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Sat, 6 Jun 2026 08:25:52 +0330 Subject: [PATCH] fix BaseItemProvider Controller Stuffs ... --- ...XBaseItemResourceProviderControllerBase.cs | 398 ++++++++++++++++++ .../IXBaseItemResourceProviderController.cs | 117 +++++ 2 files changed, 515 insertions(+) create mode 100644 Controllers/XBaseItemResourceProviderControllerBase.cs create mode 100644 Interfaces/IXBaseItemResourceProviderController.cs diff --git a/Controllers/XBaseItemResourceProviderControllerBase.cs b/Controllers/XBaseItemResourceProviderControllerBase.cs new file mode 100644 index 0000000..9c1f303 --- /dev/null +++ b/Controllers/XBaseItemResourceProviderControllerBase.cs @@ -0,0 +1,398 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using xCommons.Configurations; +using xCommons.Extensions; +using xCommons.Providers; +using xExceptions.Constants; +using xIdentityService.Controllers; +using xIdentityService.Interfaces; +using xModels.Dtos; +using xStringService.Interfaces; +using xStringService.Models.Dtos; + +namespace xStringService.Controllers +{ + public abstract class XBaseItemResourceProviderControllerBase : XBaseIdentityApiV1Controller, IXBaseItemResourceProviderController + { + private readonly IXItemResourceProvider provider; + + protected XBaseItemResourceProviderControllerBase( + ILogger logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider, + IXItemResourceProvider provider + ) : base( + logger, + appConfiguration, + identityProvider, + validationProvider + ) + { + this.provider = provider; + } + + // + #region Actions ... + /// + /// Retrieve a List of Exists Languages ... + /// + /// if null or empty, check all languages in Item Side + /// + /// + [HttpGet("Languages")] + public virtual async Task>> GetLanguages( + [FromQuery] object identifier, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + var result = Enumerable.Empty(); + if (!identifier.IsNull()) + { + // + result = await provider.GetLanguages( + identifier: identifier, + cancellationToken: cancellationToken + ); + } + else + { + // + result = await provider.GetLanguages( + cancellationToken: cancellationToken + ); + } + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Retrieve all Exists Items ... + /// + /// Specified Resource Title + /// + /// + [HttpGet("ResourceLocales")] + public virtual async Task>> GetResourceLocales( + [FromQuery] object identifier, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + var result = Enumerable.Empty(); + if (!identifier.IsNull()) + { + // + result = await provider.GetResourceLocales( + identifier: identifier, + cancellationToken: cancellationToken + ); + } + else + { + // + result = await provider.GetResourceLocales( + cancellationToken: cancellationToken + ); + } + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Get all Exists Items for Specified identitifer ... + /// + /// + /// + /// + [HttpGet("Locales/{identifier}")] + public virtual async Task>> GetLocales( + [FromRoute] object identifier, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + + // + return Ok(); + } + catch (Exception ex) + { + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Remove Specified Language Locale of Specified Identifier ... + /// + /// + /// if null or empty Remove all Locales + /// + /// + [HttpDelete("Locales/{identifier}")] + public virtual async Task> RemoveLocale( + [FromRoute] object identifier, + [FromQuery] string language, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate ... + var isValid = !identifier.IsNull(); + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + // Retrieve Connection ID ... + var connectionId = GetConnectionId(); + + // + XResourceDto result = null; + if (!language.IsNullOrEmpty()) + { + // + result = await provider.RemoveLocale( + language: language, + identifier: identifier, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + } + else + { + // + await provider.RemoveLocales( + identifier: identifier, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + } + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Check Specified ocale Exists for Specified Identifier ... + /// + /// + /// + /// + /// + [HttpGet("Locales/{identifier}/{language}/Has")] + public virtual async Task> HasLocale( + [FromRoute] object identifier, + [FromRoute] string language, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotEmpty(language) + .AddNotNull(identifier) + .ValidateGroupAsync(); + + // + // Calculate Result ... + var result = await provider.HasLocale( + language: language, + identifier: identifier, + cancellationToken: cancellationToken + ); + + // + return Ok(result); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Get all Exists Locales Representaion based on Specified Identitifer + /// as XResourceDto ... + /// + /// + /// + /// + [HttpGet("Resource/{identifier}")] + public virtual async Task> GetResource( + [FromRoute] object identifier, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate ... + ValidationProvider.NotNull(identifier); + + // + var result = await provider.GetResource( + identifier: identifier, + cancellationToken: cancellationToken + ); + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Add Or Update Locale for Specified Identitifer ... + /// + /// + /// + /// + /// + [HttpPost("Locales/{identifier}")] + public virtual async Task> AddOrUpdateLocale( + [FromRoute] object identifier, + [FromBody] XLocaleResourceDto locale, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(identifier) + .AddNotNull(locale) + .AddNotEmpty(locale.Value) + .AddNotEmpty(locale.Language) + .ValidateGroupAsync(); + + // + // Retrieve Connection ID ... + var connectionId = GetConnectionId(); + + // + var result = await provider.AddOrUpdateLocale( + locale: locale, + identifier: identifier, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Update a Resource State ... + /// + /// + /// + /// + /// + [HttpPost("Resource/{identifier}")] + public virtual async Task> AddOrUpdateResource( + [FromRoute] object identifier, + [FromBody] XResourceDto resource, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(identifier) + .AddNotNull(resource) + .ValidateGroupAsync(); + + // + // Retrieve Connection ID ... + var connectionId = GetConnectionId(); + + // + var result = await provider.AddOrUpdateResource( + resource: resource, + identifier: identifier, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + + // + return Ok(result.ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + } +} \ No newline at end of file diff --git a/Interfaces/IXBaseItemResourceProviderController.cs b/Interfaces/IXBaseItemResourceProviderController.cs new file mode 100644 index 0000000..dab12bb --- /dev/null +++ b/Interfaces/IXBaseItemResourceProviderController.cs @@ -0,0 +1,117 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using xModels.Dtos; +using xStringService.Models.Dtos; + +namespace xStringService.Interfaces +{ + public interface IXBaseItemResourceProviderController + { + /// + /// Retrieve a List of Exists Languages ... + /// + /// if null or empty, check all languages in Item Side + /// + /// + [HttpGet("Languages")] + Task>> GetLanguages( + [FromQuery] object identifier, + CancellationToken cancellationToken = default + ); + + /// + /// Retrieve all Exists Items ... + /// + /// Specified Resource Title + /// + /// + [HttpGet("ResourceLocales")] + Task>> GetResourceLocales( + [FromQuery] object identifier, + CancellationToken cancellationToken = default + ); + + /// + /// Get all Exists Items for Specified identitifer ... + /// + /// + /// + /// + [HttpGet("Locales/{identifier}")] + Task>> GetLocales( + [FromRoute] object identifier, + CancellationToken cancellationToken = default + ); + + /// + /// Remove Specified Language Locale of Specified Identifier ... + /// + /// + /// if null or empty Remove all Locales + /// + /// + [HttpDelete("Locales/{identifier}")] + Task> RemoveLocale( + [FromRoute] object identifier, + [FromQuery] string language, + CancellationToken cancellationToken = default + ); + + /// + /// Check Specified ocale Exists for Specified Identifier ... + /// + /// + /// + /// + /// + [HttpGet("Locales/{identifier}/{language}/Has")] + Task> HasLocale( + [FromRoute] object identifier, + [FromRoute] string language, + CancellationToken cancellationToken = default + ); + + /// + /// Get all Exists Locales Representaion based on Specified Identitifer + /// as XResourceDto ... + /// + /// + /// + /// + [HttpGet("Resource/{identifier}")] + Task> GetResource( + [FromRoute] object identifier, + CancellationToken cancellationToken = default + ); + + /// + /// Add Or Update Locale for Specified Identitifer ... + /// + /// + /// + /// + /// + [HttpPost("Locales/{identifier}")] + Task> AddOrUpdateLocale( + [FromRoute] object identifier, + [FromBody] XLocaleResourceDto locale, + CancellationToken cancellationToken = default + ); + + /// + /// Update a Resource State ... + /// + /// + /// + /// + /// + [HttpPost("Resource/{identifier}")] + Task> AddOrUpdateResource( + [FromRoute] object identifier, + [FromBody] XResourceDto resource, + CancellationToken cancellationToken = default + ); + } +} \ No newline at end of file