From a7916ebd8a252fdb30df56b33bdbc8c645f8f48e Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Wed, 6 May 2026 04:41:27 +0330 Subject: [PATCH] last ... --- Controllers/XIBaseController.cs | 1 - Controllers/XIBaseDomainController.cs | 712 ++++++++++++++++++++++++++ Controllers/XIBaseEntityController.cs | 1 - 3 files changed, 712 insertions(+), 2 deletions(-) create mode 100644 Controllers/XIBaseDomainController.cs diff --git a/Controllers/XIBaseController.cs b/Controllers/XIBaseController.cs index eefea77..4355f75 100644 --- a/Controllers/XIBaseController.cs +++ b/Controllers/XIBaseController.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.ObjectPool; using xCommons.Configurations; using xCommons.Constants; using xCommons.Controllers; diff --git a/Controllers/XIBaseDomainController.cs b/Controllers/XIBaseDomainController.cs new file mode 100644 index 0000000..2e9703d --- /dev/null +++ b/Controllers/XIBaseDomainController.cs @@ -0,0 +1,712 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore.Query; +using Microsoft.Extensions.Logging; +using xCommons.Attributes; +using xCommons.Configurations; +using xCommons.Extensions; +using xCommons.Providers; +using xDataService.Interfaces; +using xExceptions.Constants; +using xIdentityService.Interfaces; +using xModels.Base; +using xModels.Dtos; + +namespace xIdentityService.Controllers +{ + [Authorize] + [RequireXPowered(false)] + public abstract class XIBaseDomainController : XIBaseController, IXDomainControllerActions + where TEntity : XBaseEntity + where TDto : XBaseEntityDto + { + // + #region Properties ... + public IXBaseRepositoryService RepositoryService { get; } + public Func, IOrderedQueryable> DefaultOrderBuilder { get; } + public Func, IIncludableQueryable> DefaultIncludeBuilder { get; } + #endregion + + // + #region Constructor ... + protected XIBaseDomainController( + ILogger logger, + XAppConfiguration appConfiguration, + IXIdentityProvider identityProvider, + XValidationProvider validationProvider, + IXBaseRepositoryService repositoryService, + Func, IOrderedQueryable> defaultOrderBuilder = null, + Func, IIncludableQueryable> defaultIncludeBuilder = null + ) : base( + logger, + appConfiguration, + identityProvider, + validationProvider + ) + { + // + RepositoryService = repositoryService; + DefaultOrderBuilder = defaultOrderBuilder; + DefaultIncludeBuilder = defaultIncludeBuilder; + } + #endregion + + // + #region Actions ... + // + #region Add ... + /// + /// Add Specified Item ... + /// + /// + /// + /// + /// + /// + [HttpPost] + public virtual async Task> Add( + [FromBody] TDto item, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + if (!ModelState.IsValid) + { + XException.InvalidArgs.Throw(); + } + ValidationProvider.NotNull(item); + + // + // Get Result ... + var result = await RepositoryService + .AddAsync( + item, + saveChanges: true, + cancellationToken: cancellationToken + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Add or Update Specified Item ... + /// + /// + /// + /// + /// + /// + [HttpPost("AddOrUpdate")] + public virtual async Task> AddOrUpdate( + [FromBody] TDto item, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + if (!ModelState.IsValid) + { + XException.InvalidArgs.Throw(); + } + ValidationProvider.NotNull(item); + + // + // Get Result ... + var result = await RepositoryService + .AddOrUpdateAsync( + item, + saveChanges: true, + cancellationToken: cancellationToken + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Add a Collection of Items ... + /// + /// + /// + /// + /// + /// + [HttpPost("AddMany")] + public virtual async Task AddMany( + [FromBody] XBaseRangeRequest request, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + if (!ModelState.IsValid) + { + XException.InvalidArgs.Throw(); + } + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(request) + .AddNotZeroChilds(request.Items) + .ValidateGroupAsync(); + + // + // Get Result ... + await RepositoryService + .AddRangeAsync( + request.Items, + saveChanges: true, + cancellationToken: cancellationToken + ); + + // + return Ok(); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + + // + #region Retrieve ... + /// + /// Get Specified Item ... + /// + /// + /// + /// + /// + /// + /// + /// + [HttpGet("{id}")] + public virtual async Task> Get( + [FromRoute] TKey id, + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + ValidationProvider.NotNull(id); + + // + // Get Result ... + var result = await RepositoryService + .GetAsync( + id, + includeBuilder: useDefaultIncludes + ? DefaultIncludeBuilder + : null, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Get All Items ... + /// + /// + /// + /// + /// + /// + /// + /// + [HttpGet] + public virtual async Task>> GetAll( + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultOrders = false, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Get Result ... + var result = await RepositoryService + .GetAllAsync( + orderBuilder: useDefaultOrders + ? DefaultOrderBuilder + : null, + includeBuilder: useDefaultIncludes + ? DefaultIncludeBuilder + : null, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Find Specified Item ... + /// + /// + /// + /// + /// + /// + /// + /// + [HttpGet("FindOne/{query}")] + public virtual async Task> FindOne( + [FromRoute] string query, + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(query); + + // + // Get Result ... + var result = await RepositoryService + .FindOneAsync( + predicate: t => + t.PropValuesContains(query), + includeBuilder: useDefaultIncludes + ? DefaultIncludeBuilder + : null, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Find Many Items ... + /// + /// + /// + /// + /// + /// + /// + /// + /// + [HttpGet("FindMany/{query}")] + public virtual async Task>> FindMany( + [FromRoute] string query, + [FromQuery] bool ignoreSoftDeleteds = true, + [FromQuery] bool useDefaultOrders = false, + [FromQuery] bool useDefaultIncludes = false, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + ValidationProvider.NotEmpty(query); + + // + // Get Result ... + var result = await RepositoryService + .FindManyAsync( + predicate: t => + t.PropValuesContains(query), + orderBuilder: useDefaultOrders + ? DefaultOrderBuilder + : null, + includeBuilder: useDefaultIncludes + ? DefaultIncludeBuilder + : null, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Retrieve Items Based on Query Mechanism ... + /// + /// + /// + /// + /// + /// + /// + /// + /// + [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 Args ... + ValidationProvider.NotNull(query); + + // + // Get Result ... + var result = await RepositoryService + .QueryAsync( + query: query, + orderBuilder: useDefaultOrders + ? DefaultOrderBuilder + : null, + includeBuilder: useDefaultIncludes + ? DefaultIncludeBuilder + : null, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + + // + #region Update ... + /// + /// Update Item ... + /// + /// + /// + /// + /// + /// + /// + [HttpPut("{id}")] + public virtual async Task> Update( + [FromRoute] TKey id, + [FromBody] TDto item, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + if (!ModelState.IsValid) + { + XException.InvalidArgs.Throw(); + } + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(id, item) + .ValidateGroupAsync(); + + // + // Get Result ... + var result = await RepositoryService + .UpdateAsync( + id: id, + item: item, + saveChanges: true, + cancellationToken: cancellationToken + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Update a Collection of items ... + /// + /// + /// + /// + /// + /// + [HttpPost("UpdateMany")] + public virtual async Task> UpdateMany( + [FromBody] XBaseRangeRequest request, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + if (!ModelState.IsValid) + { + XException.InvalidArgs.Throw(); + } + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(request) + .AddNotZeroChilds(request.Items) + .ValidateGroupAsync(); + + // + // Get Result ... + var result = await RepositoryService + .UpdateRangeAsync( + saveChanges: true, + items: request.Items, + cancellationToken: cancellationToken + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + + // + #region Exists ... + /// + ///Check an Item Exists or not ... + /// + /// + /// + /// + /// + /// + /// + [HttpGet("{id}/IsExists")] + public virtual async Task> IsExists( + [FromRoute] TKey id, + [FromQuery] bool ignoreSoftDeleteds = true, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + ValidationProvider.NotNull(id); + + // + // Get Result ... + var result = await RepositoryService + .IsExistsAsync( + id: id, + cancellationToken: cancellationToken, + ignoreSoftDeleteds: ignoreSoftDeleteds + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + + // + #region Remove ... + /// + /// Remove an Items ... + /// + /// + /// + /// + /// + /// + /// + [HttpDelete("{id}")] + public virtual async Task> Remove( + [FromRoute] TKey id, + [FromQuery] bool softDelete = true, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + ValidationProvider.NotNull(id); + + // + // Get Result ... + var result = await RepositoryService + .RemoveAsync( + id: id, + saveChanges: true, + softDelete: softDelete, + cancellationToken: cancellationToken + ); + + // + return Ok(result + .ToDynamicObject()); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + + /// + /// Remove Many Items ... + /// + /// + /// + /// + /// + /// + /// + [HttpPost("RemoveMany")] + public virtual async Task RemoveMany( + [FromBody] XBaseRangeRequest request, + [FromQuery] bool softDelete = true, + CancellationToken cancellationToken = default + ) + { + // + try + { + // + // Validate Args ... + await ValidationProvider + .GroupValidationBuilder() + .AddNotNull(request) + .AddNotZeroChilds(request.Items) + .ValidateGroupAsync(); + + // + // Get Result ... + await RepositoryService + .RemoveRangeAsync( + saveChanges: true, + items: request.Items, + softDelete: softDelete, + cancellationToken: cancellationToken + ); + + // + return Ok(); + } + catch (Exception ex) + { + // + var result = GetExceptionActionResult(ex); + return result; + } + } + #endregion + #endregion + } +} \ No newline at end of file diff --git a/Controllers/XIBaseEntityController.cs b/Controllers/XIBaseEntityController.cs index ac56a04..f52e243 100644 --- a/Controllers/XIBaseEntityController.cs +++ b/Controllers/XIBaseEntityController.cs @@ -11,7 +11,6 @@ using xCommons.Attributes; using xCommons.Configurations; using xCommons.Extensions; using xCommons.Providers; -using xDataService.Controllers; using xDataService.Interfaces; using xExceptions.Constants; using xIdentityService.Interfaces;