last ...
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,712 @@
|
||||
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 xCommons.Configurations;
|
||||
using xCommons.Controllers;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xDataService.Interfaces;
|
||||
using xExceptions.Constants;
|
||||
using xModels.Base;
|
||||
using xModels.Dtos;
|
||||
|
||||
namespace xDataService.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// an Abstract Controller for Providing Entity Dto Repository Service Actions Implementatin ...
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
/// <typeparam name="TDto"></typeparam>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
public abstract class XBaseEntityDtoController<TEntity, TDto, TKey> : XBaseController, IXEntityDtoControllerActions<TEntity, TDto, TKey>
|
||||
where TEntity : XBaseEntity<TKey>
|
||||
where TDto : XBaseEntityDto<TKey>
|
||||
{
|
||||
//
|
||||
#region Properties ...
|
||||
public IXBaseRepositoryService<TEntity, TDto, TKey> RepositoryService { get; }
|
||||
public Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> DefaultOrderBuilder { get; }
|
||||
public Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> DefaultIncludeBuilder { get; }
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Constructor ...
|
||||
protected XBaseEntityDtoController(
|
||||
ILogger<XBaseEntityDtoController<TEntity, TDto, TKey>> logger,
|
||||
XAppConfiguration appConfiguration,
|
||||
XValidationProvider validationProvider,
|
||||
IXBaseRepositoryService<TEntity, TDto, TKey> repositoryService,
|
||||
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> defaultOrderBuilder = null,
|
||||
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> defaultIncludeBuilder = null
|
||||
) : base(
|
||||
logger,
|
||||
appConfiguration,
|
||||
validationProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
RepositoryService = repositoryService;
|
||||
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<TDto>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto> 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 ...
|
||||
/// <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<TDto>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto>>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto>>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto>>> 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 ...
|
||||
/// <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<TDto>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto> 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 ...
|
||||
/// <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 RepositoryService
|
||||
.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<TDto>> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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<TDto> 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user