using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; 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 xDataService.Extensions; using xExceptions.Constants; using xIdentityService.Controllers; using xIdentityService.Interfaces; using xModels.Base; using xModels.Dtos; using xPushService.Base; using xPushService.Interfaces; namespace xPushService.Controllers { public abstract class XBaseRepositoryProviderController : XBaseIdentityApiV1Controller, IXBaseRepositoryProviderController where TEntity : XBaseEntity where THub : XBaseEntityHub { // #region Properties ... public IXBaseEntityProviderr Provider { get; } public Func, IOrderedQueryable> DefaultOrderBuilder { get; } public Func, IIncludableQueryable> DefaultIncludeBuilder { get; } #endregion protected XBaseRepositoryProviderController( ILogger logger, XAppConfiguration appConfiguration, IXIdentityProvider identityProvider, XValidationProvider validationProvider, IXBaseEntityProviderr provider, Func, IOrderedQueryable> defaultOrderBuilder, Func, IIncludableQueryable> defaultIncludeBuilder ) : base( logger: logger, appConfiguration: appConfiguration, identityProvider: identityProvider, validationProvider: validationProvider ) { // Provider = provider; DefaultOrderBuilder = defaultOrderBuilder; DefaultIncludeBuilder = defaultIncludeBuilder; } // #region Actions ... /// /// add a new Item ... /// /// /// /// /// [HttpPost] public virtual async Task> Add( [FromBody] TEntity item, [FromQuery] bool saveChanges = true, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } ValidationProvider.NotNull(item); // var connectionId = GetConnectionId(); // var result = await Provider.AddAsync( item: item, saveChanges: saveChanges, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Update an Item values ... /// /// /// /// /// /// [HttpPut("{id}")] public virtual async Task> Update( [FromRoute] TKey id, [FromBody] TEntity item, [FromQuery] bool saveChanges = true, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } ValidationProvider.NotNull(item); // var connectionId = GetConnectionId(); // var result = await Provider.UpdateAsync( id: id, item: item, saveChanges: saveChanges, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// remove an Item by it's Id ... /// /// /// /// /// /// [HttpDelete("{id}")] public virtual async Task> Remove( [FromRoute] TKey id, [FromQuery] bool softDelete = true, [FromQuery] bool saveChanges = true, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // var connectionId = GetConnectionId(); // var result = await Provider.RemoveAsync( id: id, softDelete: softDelete, saveChanges: saveChanges, connectionId: connectionId, cancellationToken: cancellationToken ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// Check an Item exists or not ... /// /// /// /// /// [HttpGet("{id}/[action]")] public virtual async Task> IsExists( [FromRoute] TKey id, [FromQuery] bool ignoreSoftDeleteds = true, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // var result = await Provider.IsExistsAsync( id: id, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return Ok(result); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// count all exists Items ... /// /// /// /// [HttpGet("Count")] public virtual async Task> Count( [FromQuery] bool ignoreSoftDeleteds = true, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // var result = await Provider.CountAsync( cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return Ok(result); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// retrieve an Item by it's Id ... /// /// /// /// /// /// [HttpGet("{id}")] public virtual async Task> Get( [FromRoute] TKey id, [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool useDefaultIncludes = false, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // var result = await Provider.GetAsync( id: id, includeBuilder: !useDefaultIncludes ? null : DefaultIncludeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// retrieve all exists Items ... /// /// /// /// /// /// [HttpGet] public virtual async Task>> GetAll( [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool useDefaultOrders = false, [FromQuery] bool useDefaultIncludes = false, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // var result = await Provider.GetAllAsync( orderBuilder: !useDefaultOrders ? null : DefaultOrderBuilder, includeBuilder: !useDefaultIncludes ? null : DefaultIncludeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// find an Item by providing a Conditional Expression ... /// /// /// /// /// /// [HttpGet("FindOne")] public virtual async Task> FindOne( [FromRoute] string query, [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool useDefaultIncludes = false, CancellationToken cancellationToken = default ) { // try { // if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } ValidationProvider.NotEmpty(query); // Expression> predicate = x => x.PropValuesContains(query); var result = await Provider.FindOneAsync( predicate: predicate, includeBuilder: !useDefaultIncludes ? null : DefaultIncludeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// find a collection of Items by proving a Conditional Expression ... /// /// /// /// /// /// /// [HttpGet("FindMany")] public virtual async Task>> FindMany( [FromRoute] string query, [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool useDefaultOrders = false, [FromQuery] bool useDefaultIncludes = false, CancellationToken cancellationToken = default ) { // try { // if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } ValidationProvider.NotEmpty(query); // Expression> predicate = x => x.PropValuesContains(query); var result = await Provider.FindManyAsync( predicate: predicate, orderBuilder: !useDefaultOrders ? null : DefaultOrderBuilder, includeBuilder: !useDefaultIncludes ? null : DefaultIncludeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } /// /// retrieve Items based on XQuery Pagination structure ... /// /// /// /// /// /// /// [HttpGet("Query")] public virtual async Task>> Query( [FromQuery] XQuery query, [FromQuery] bool ignoreSoftDeleteds = true, [FromQuery] bool useDefaultOrders = false, [FromQuery] bool useDefaultIncludes = false, CancellationToken cancellationToken = default ) { // try { // // Validate ... if (!ModelState.IsValid) { XException.InvalidArgs.Throw(); } // // Normalize ... if (query.IsNullOrDefault()) { query = query.NormalizeQuery(Provider.DataConfiguration); } // var result = await Provider.QueryAsync( query: query, predicate: null, orderBuilder: !useDefaultOrders ? null : DefaultOrderBuilder, includeBuilder: !useDefaultIncludes ? null : DefaultIncludeBuilder, cancellationToken: cancellationToken, ignoreSoftDeleteds: ignoreSoftDeleteds ); // return Ok(result.ToDynamicObject()); } catch (Exception ex) { // var result = GetExceptionActionResult(ex); return result; } } #endregion // #region NonActions ... /// /// Send Custom Push Message ... /// /// /// /// /// /// [NonAction] public virtual async Task SendPush( string action, string payLoad, string connectionId = null, CancellationToken cancellationToken = default ) { // await Provider.SendPush( action: action, payLoad: payLoad, connectionId: connectionId, cancellationToken: cancellationToken ); } #endregion } }