Files
xIdentityService/Controllers/XIBaseEntityController.cs
T
2026-05-15 02:47:52 +03:30

715 lines
24 KiB
C#

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
{
//
// TODO: Fix This ...
// [Authorize]
// [RequireXPowered(false)]
// public abstract class XIBaseEntityController<TEntity, TKey> : XIBaseController, IXEntityControllerActions<TEntity, TKey>
// where TEntity : XBaseEntity<TKey>
// {
// //
// #region Properties ...
// public IXBaseRepository<TEntity, TKey> Repository { get; }
// public Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> DefaultOrderBuilder { get; }
// public Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> DefaultIncludeBuilder { get; }
// #endregion
// //
// #region Constructor ...
// protected XIBaseEntityController(
// ILogger logger,
// XAppConfiguration appConfiguration,
// IXIdentityProvider identityProvider,
// XValidationProvider validationProvider,
// IXBaseRepository<TEntity, TKey> repository,
// Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> defaultOrderBuilder = null,
// Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> defaultIncludeBuilder = null
// ) : base(
// logger,
// appConfiguration,
// identityProvider,
// validationProvider
// )
// {
// //
// Repository = repository;
// DefaultOrderBuilder = defaultOrderBuilder;
// DefaultIncludeBuilder = defaultIncludeBuilder;
// }
// #endregion
// //
// #region Actions ...
// //
// #region Add ...
// /// <summary>
// /// Add Specified Item ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="item"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpPost]
// public virtual async Task<ActionResult<TEntity>> Add(
// [FromBody] TEntity item,
// CancellationToken cancellationToken = default
// )
// {
// //
// try
// {
// //
// // Validate Args ...
// if (!ModelState.IsValid)
// {
// XException.InvalidArgs.Throw();
// }
// ValidationProvider.NotNull(item);
// //
// // Get Result ...
// var result = await Repository
// .AddAsync(
// item,
// saveChanges: true,
// cancellationToken: cancellationToken
// );
// //
// return Ok(result
// .ToDynamicObject());
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// /// <summary>
// /// Add or Update Specified Item ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="item"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpPost("AddOrUpdate")]
// public virtual async Task<ActionResult<TEntity>> AddOrUpdate(
// [FromBody] TEntity item,
// CancellationToken cancellationToken = default
// )
// {
// //
// try
// {
// //
// // Validate Args ...
// if (!ModelState.IsValid)
// {
// XException.InvalidArgs.Throw();
// }
// ValidationProvider.NotNull(item);
// //
// // Get Result ...
// var result = await Repository
// .AddOrUpdateAsync(
// item,
// saveChanges: true,
// cancellationToken: cancellationToken
// );
// //
// return Ok(result
// .ToDynamicObject());
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// /// <summary>
// /// Add a Collection of Items ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="request"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpPost("AddMany")]
// public virtual async Task<ActionResult> AddMany(
// [FromBody] XBaseRangeRequest<TEntity> 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 Repository
// .AddRangeAsync(
// request.Items,
// saveChanges: true,
// cancellationToken: cancellationToken
// );
// //
// return Ok();
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// #endregion
// //
// #region Retrieve ...
// /// <summary>
// /// Get Specified Item ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="id"></param>
// /// <param name="ignoreSoftDeleteds"></param>
// /// <param name="useDefaultIncludes"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpGet("{id}")]
// public virtual async Task<ActionResult<TEntity>> 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 Repository
// .GetAsync(
// id,
// includeBuilder: useDefaultIncludes
// ? DefaultIncludeBuilder
// : null,
// cancellationToken: cancellationToken,
// ignoreSoftDeleteds: ignoreSoftDeleteds
// );
// //
// return Ok(result
// .ToDynamicObject());
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// /// <summary>
// /// Get All Items ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="ignoreSoftDeleteds"></param>
// /// <param name="useDefaultOrders"></param>
// /// <param name="useDefaultIncludes"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpGet]
// public virtual async Task<ActionResult<IEnumerable<TEntity>>> GetAll(
// [FromQuery] bool ignoreSoftDeleteds = true,
// [FromQuery] bool useDefaultOrders = false,
// [FromQuery] bool useDefaultIncludes = false,
// CancellationToken cancellationToken = default
// )
// {
// //
// try
// {
// //
// // Get Result ...
// var result = await Repository
// .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;
// }
// }
// /// <summary>
// /// Find Specified Item ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="query"></param>
// /// <param name="ignoreSoftDeleteds"></param>
// /// <param name="useDefaultIncludes"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpGet("FindOne/{query}")]
// public virtual async Task<ActionResult<TEntity>> 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 Repository
// .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;
// }
// }
// /// <summary>
// /// Find Many Items ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="query"></param>
// /// <param name="ignoreSoftDeleteds"></param>
// /// <param name="useDefaultOrders"></param>
// /// <param name="useDefaultIncludes"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpGet("FindMany/{query}")]
// public virtual async Task<ActionResult<IEnumerable<TEntity>>> 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 Repository
// .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;
// }
// }
// /// <summary>
// /// Retrieve Items Based on Query Mechanism ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="query"></param>
// /// <param name="ignoreSoftDeleteds"></param>
// /// <param name="useDefaultOrders"></param>
// /// <param name="useDefaultIncludes"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpGet("Query")]
// public virtual async Task<ActionResult<XQueryResult<TEntity>>> 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 Repository
// .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 ...
// /// <summary>
// /// Update Item ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="id"></param>
// /// <param name="item"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpPut("{id}")]
// public virtual async Task<ActionResult<TEntity>> Update(
// [FromRoute] TKey id,
// [FromBody] TEntity 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 Repository
// .UpdateAsync(
// id: id,
// item: item,
// saveChanges: true,
// cancellationToken: cancellationToken
// );
// //
// return Ok(result
// .ToDynamicObject());
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// /// <summary>
// /// Update a Collection of items ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="request"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpPost("UpdateMany")]
// public virtual async Task<ActionResult<bool>> UpdateMany(
// [FromBody] XBaseRangeRequest<TEntity> 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 Repository
// .UpdateRangeAsync(
// saveChanges: true,
// items: request.Items,
// cancellationToken: cancellationToken
// );
// //
// return Ok(result
// .ToDynamicObject());
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// #endregion
// //
// #region Exists ...
// /// <summary>
// ///Check an Item Exists or not ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="id"></param>
// /// <param name="ignoreSoftDeleteds"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpGet("{id}/IsExists")]
// public virtual async Task<ActionResult<bool>> IsExists(
// [FromRoute] TKey id,
// [FromQuery] bool ignoreSoftDeleteds = true,
// CancellationToken cancellationToken = default
// )
// {
// //
// try
// {
// //
// // Validate Args ...
// ValidationProvider.NotNull(id);
// //
// // Get Result ...
// var result = await Repository
// .IsExistsAsync(
// id: id,
// cancellationToken: cancellationToken,
// ignoreSoftDeleteds: ignoreSoftDeleteds
// );
// //
// return Ok(result
// .ToDynamicObject());
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// #endregion
// //
// #region Remove ...
// /// <summary>
// /// Remove an Items ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="id"></param>
// /// <param name="softDelete"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpDelete("{id}")]
// public virtual async Task<ActionResult<TEntity>> Remove(
// [FromRoute] TKey id,
// [FromQuery] bool softDelete = true,
// CancellationToken cancellationToken = default
// )
// {
// //
// try
// {
// //
// // Validate Args ...
// ValidationProvider.NotNull(id);
// //
// // Get Result ...
// var result = await Repository
// .RemoveAsync(
// id: id,
// saveChanges: true,
// softDelete: softDelete,
// cancellationToken: cancellationToken
// );
// //
// return Ok(result
// .ToDynamicObject());
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// /// <summary>
// /// Remove Many Items ...
// /// </summary>
// /// <remarks>
// /// </remarks>
// /// <param name="request"></param>
// /// <param name="softDelete"></param>
// /// <param name="cancellationToken"></param>
// /// <returns></returns>
// [HttpPost("RemoveMany")]
// public virtual async Task<ActionResult> RemoveMany(
// [FromBody] XBaseRangeRequest<TEntity> request,
// [FromQuery] bool softDelete = true,
// CancellationToken cancellationToken = default
// )
// {
// //
// try
// {
// //
// // Validate Args ...
// await ValidationProvider
// .GroupValidationBuilder()
// .AddNotNull(request)
// .AddNotZeroChilds(request.Items)
// .ValidateGroupAsync();
// //
// // Get Result ...
// await Repository
// .RemoveRangeAsync(
// saveChanges: true,
// items: request.Items,
// softDelete: softDelete,
// cancellationToken: cancellationToken
// );
// //
// return Ok();
// }
// catch (Exception ex)
// {
// //
// var result = GetExceptionActionResult(ex);
// return result;
// }
// }
// #endregion
// #endregion
// }
}