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 } }