using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore.Query; using Microsoft.Extensions.Logging; using xCategoryService.Interfaces; using xCategoryService.Models.Dtos; using xCategoryService.Models.Entities; using xCategoryService.Providers.Dtos; using xCommons.Configurations; using xCommons.Extensions; using xCommons.Providers; using xExceptions.Constants; using xIdentityService.Interfaces; using xModels.Dtos; using xPushService.Controllers; namespace xCategoryService.Controllers { public abstract class XSubCategoryProviderControllerBase : XBaseProvidedController, IXSubCategoryProviderController { private readonly IXSubCategoryProvider provider; protected XSubCategoryProviderControllerBase( ILogger logger, XAppConfiguration appConfiguration, IXIdentityProvider identityProvider, XValidationProvider validationProvider, IXSubCategoryProvider provider, Func, IOrderedQueryable> defaultOrderBuilder = null, Func, IIncludableQueryable> defaultIncludeBuilder = null ) : base( logger, appConfiguration, identityProvider, validationProvider, provider, defaultOrderBuilder, defaultIncludeBuilder ) { this.provider = provider; } // #region Actions ... /// /// Check Has Specified Locale or not ... /// /// /// /// /// [HttpGet("Resources/{id}/HasLocale")] public virtual async Task> HasLocale( [FromRoute] int id, [FromQuery] string language, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || !id.IsValidIntId()) { XException.InvalidArgs.Throw(); } ValidationProvider.NotEmpty(language); // var result = await provider.HasLocale( id: id, language: language, cancellationToken: cancellationToken ); // return Ok(result); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Load an Item by all of it's Related Translations ... /// /// /// /// [HttpGet("Resources/{id}")] public virtual async Task> GetResource( [FromRoute] int id, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || !id.IsValidIntId()) { XException.InvalidArgs.Throw(); } // var result = await provider.GetResource( id: id, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Add Category Item for Specified Language ... /// /// /// /// /// [HttpPost("Resources")] public virtual async Task> Add( [FromBody] XSubCategoryDto item, [FromQuery] string language = null, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotNull(item) .AddNotEmpty(language) .AddNotEmpty(item.Title) .AddNotEmpty(item.Description) .ValidateGroupAsync(); // // Reading Requirements ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await provider.Add( item: item, language: language, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Update a Category Item for Specified Language ... /// /// /// /// /// /// [HttpPut("Resources/{id}")] public virtual async Task> Update( [FromRoute] int id, [FromBody] XSubCategoryDto item, [FromQuery] string language = null, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid || !id.IsValidIntId()) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotNull(item) .AddNotEmpty(language) .AddNotEmpty(item.Title) .AddNotEmpty(item.Description) .ValidateGroupAsync(); // // Reading Requirements ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await provider.Update( id: id, item: item, language: language, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Refresh a Resource ... /// /// /// /// [HttpPost("Resources/Refresh")] public virtual async Task> Refresh( [FromBody] XSubCategoryResourceDto resource, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotNull(resource) .AddNotNull(resource.Item) .ValidateGroupAsync(); // // Reading Requirements ... var userInfo = await GetUserInfo(); var connectionId = GetConnectionId(); // var result = await provider.Refresh( resource: resource, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Get All Resources ... /// /// /// [HttpGet("Resources/All")] public virtual async Task>> GetAllResources( CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // var result = await provider.GetAllResources( cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Find One Resource ... /// /// /// /// [HttpGet("Resources/Find/One")] public virtual async Task> FindOneResource( [FromQuery] string query, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotEmpty(query) .ValidateGroupAsync(); // var result = await provider.FindOneResource( cancellationToken: cancellationToken, predicate: x => x.PropValuesContains(query) ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Find Many Resource ... /// /// /// /// [HttpGet("Resources/Find/Many")] public virtual async Task>> FindManyResource( [FromQuery] string query, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotEmpty(query) .ValidateGroupAsync(); // var result = await provider.FindManyResource( cancellationToken: cancellationToken, predicate: x => x.PropValuesContains(query), orderBuilder: x => x.OrderBy(y => y.Item.Id) ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Retrieve Resources by Query Pattern ... /// /// /// /// [HttpGet("Resources/Query")] public virtual async Task>> QueryResources( [FromQuery] XQuery query = null, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } await ValidationProvider .GroupValidationBuilder() .AddNotNull(query) .ValidateGroupAsync(); // var result = await provider.QueryResources( query: query, predicate: null, cancellationToken: cancellationToken, orderBuilder: x => x.OrderBy(y => y.Item.Id) ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion } }