Skip to content

Commit

Permalink
adding crud events
Browse files Browse the repository at this point in the history
  • Loading branch information
Edgar Mesquita authored and Edgar Mesquita committed Jul 21, 2023
1 parent c918ab8 commit 72f52b9
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 56 deletions.
68 changes: 54 additions & 14 deletions src/eQuantic.Core.Api.Crud/Handlers/CrudEndpointHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public CrudEndpointHandlers(CrudOptions<TEntity> options)
/// <param name="id"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Results<Ok<TEntity>, NotFound>> GetReferencedById([FromRoute] int referenceId, [FromRoute] int id, [FromServices]TService service)
public async Task<Results<Ok<TEntity>, NotFound>> GetReferencedById(
[FromRoute] int referenceId,
[FromRoute] int id,
[FromServices]TService service)
{
var request = new ItemRequest<int>(referenceId, id);
return await GetById(request, service);
Expand All @@ -50,7 +53,9 @@ public async Task<Results<Ok<TEntity>, NotFound>> GetReferencedById([FromRoute]
/// <param name="id"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Results<Ok<TEntity>, NotFound>> GetById([FromRoute] int id, [FromServices]TService service)
public async Task<Results<Ok<TEntity>, NotFound>> GetById(
[FromRoute] int id,
[FromServices]TService service)
{
var request = new ItemRequest(id);
return await GetById(request, service);
Expand All @@ -66,7 +71,13 @@ public async Task<Results<Ok<TEntity>, NotFound>> GetById([FromRoute] int id, [F
/// <param name="orderBy"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Ok<PagedListResult<TEntity>>> GetReferencedPagedList([FromRoute] int referenceId, [FromQuery] int? pageIndex, [FromQuery] int? pageSize, [FromQuery] IFiltering[]? filterBy, [FromQuery] ISorting[]? orderBy, [FromServices]TService service)
public async Task<Ok<PagedListResult<TEntity>>> GetReferencedPagedList(
[FromRoute] int referenceId,
[FromQuery] int? pageIndex,
[FromQuery] int? pageSize,
[FromQuery] IFiltering[]? filterBy,
[FromQuery] ISorting[]? orderBy,
[FromServices]TService service)
{
var request = new PagedListRequest<TEntity,int>(referenceId, pageIndex, pageSize, filterBy, orderBy);
return await GetPagedList(request, service);
Expand All @@ -81,7 +92,12 @@ public async Task<Ok<PagedListResult<TEntity>>> GetReferencedPagedList([FromRout
/// <param name="orderBy"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Ok<PagedListResult<TEntity>>> GetPagedList([FromQuery] int? pageIndex, [FromQuery] int? pageSize, [FromQuery] IFiltering[]? filterBy, [FromQuery] ISorting[]? orderBy, [FromServices]TService service)
public async Task<Ok<PagedListResult<TEntity>>> GetPagedList(
[FromQuery] int? pageIndex,
[FromQuery] int? pageSize,
[FromQuery] IFiltering[]? filterBy,
[FromQuery] ISorting[]? orderBy,
[FromServices]TService service)
{
var request = new PagedListRequest<TEntity>(pageIndex, pageSize, filterBy, orderBy);
return await GetPagedList(request, service);
Expand All @@ -93,7 +109,9 @@ public async Task<Ok<PagedListResult<TEntity>>> GetPagedList([FromQuery] int? pa
/// <param name="request"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<CreatedAtRoute<int>> Create([FromBody] TRequest request, [FromServices]TService service)
public async Task<CreatedAtRoute<int>> Create(
[FromBody] TRequest request,
[FromServices]TService service)
{
var createRequest = new CreateRequest<TRequest>(request);
return await Create(createRequest, service);
Expand All @@ -106,7 +124,10 @@ public async Task<CreatedAtRoute<int>> Create([FromBody] TRequest request, [From
/// <param name="request"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<CreatedAtRoute<int>> ReferencedCreate([FromRoute] int referenceId, [FromBody] TRequest request, [FromServices]TService service)
public async Task<CreatedAtRoute<int>> ReferencedCreate(
[FromRoute] int referenceId,
[FromBody] TRequest request,
[FromServices]TService service)
{
var createRequest = new CreateRequest<TRequest, int>(referenceId, request);
return await Create(createRequest, service);
Expand All @@ -119,7 +140,10 @@ public async Task<CreatedAtRoute<int>> ReferencedCreate([FromRoute] int referenc
/// <param name="request"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Results<Ok, BadRequest>> Update([FromRoute] int id, [FromBody] TRequest request, [FromServices]TService service)
public async Task<Results<Ok, BadRequest>> Update(
[FromRoute] int id,
[FromBody] TRequest request,
[FromServices]TService service)
{
var updateRequest = new UpdateRequest<TRequest>(id, request);
return await Update(updateRequest, service);
Expand All @@ -133,7 +157,11 @@ public async Task<Results<Ok, BadRequest>> Update([FromRoute] int id, [FromBody]
/// <param name="request"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Results<Ok, BadRequest>> ReferencedUpdate([FromRoute] int referenceId, [FromRoute] int id, [FromBody] TRequest request,[FromServices] TService service)
public async Task<Results<Ok, BadRequest>> ReferencedUpdate(
[FromRoute] int referenceId,
[FromRoute] int id,
[FromBody] TRequest request,
[FromServices] TService service)
{
var updateRequest = new UpdateRequest<TRequest, int>(referenceId, id, request);
return await Update(updateRequest, service);
Expand All @@ -145,7 +173,9 @@ public async Task<Results<Ok, BadRequest>> ReferencedUpdate([FromRoute] int refe
/// <param name="id"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Results<Ok, BadRequest>> Delete([FromRoute] int id, [FromServices]TService service)
public async Task<Results<Ok, BadRequest>> Delete(
[FromRoute] int id,
[FromServices]TService service)
{
var request = new ItemRequest(id);
return await Delete(request, service);
Expand All @@ -158,7 +188,10 @@ public async Task<Results<Ok, BadRequest>> Delete([FromRoute] int id, [FromServi
/// <param name="id"></param>
/// <param name="service"></param>
/// <returns></returns>
public async Task<Results<Ok, BadRequest>> ReferencedDelete([FromRoute] int referenceId, [FromRoute] int id, [FromServices] TService service)
public async Task<Results<Ok, BadRequest>> ReferencedDelete(
[FromRoute] int referenceId,
[FromRoute] int id,
[FromServices] TService service)
{
var request = new ItemRequest<int>(referenceId, id);
return await Delete(request, service);
Expand All @@ -173,14 +206,17 @@ private static async Task<Results<Ok<TEntity>, NotFound>> GetById(ItemRequest re
return TypedResults.NotFound();
}

private static async Task<Ok<PagedListResult<TEntity>>> GetPagedList(PagedListRequest<TEntity> request,
private static async Task<Ok<PagedListResult<TEntity>>> GetPagedList(
PagedListRequest<TEntity> request,
TService service)
{
var result = await service.GetPagedListAsync(request);
return TypedResults.Ok(new PagedListResult<TEntity>(result));
}

private async Task<CreatedAtRoute<int>> Create(CreateRequest<TRequest> request, TService service)
private async Task<CreatedAtRoute<int>> Create(
CreateRequest<TRequest> request,
TService service)
{
var result = await service.CreateAsync(request);
object routeValues = request is CreateRequest<TRequest, int> referencedRequest ?
Expand All @@ -189,13 +225,17 @@ private async Task<CreatedAtRoute<int>> Create(CreateRequest<TRequest> request,
return TypedResults.CreatedAtRoute(result, _options.Get.Name, routeValues);
}

private static async Task<Results<Ok, BadRequest>> Update(UpdateRequest<TRequest> request, TService service)
private static async Task<Results<Ok, BadRequest>> Update(
UpdateRequest<TRequest> request,
TService service)
{
var result = await service.UpdateAsync(request);
return result ? TypedResults.Ok() : TypedResults.BadRequest();
}

private static async Task<Results<Ok, BadRequest>> Delete(ItemRequest request, TService service)
private static async Task<Results<Ok, BadRequest>> Delete(
ItemRequest request,
TService service)
{
var result = await service.DeleteAsync(request);
return result ? TypedResults.Ok() : TypedResults.BadRequest();
Expand Down
6 changes: 3 additions & 3 deletions src/eQuantic.Core.Api.Crud/eQuantic.Core.Api.Crud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>eQuantic API CRUD Library</Description>
<Copyright>2023</Copyright>
<AssemblyTitle>eQuantic.Core.Api.Crud</AssemblyTitle>
<Version>1.0.4.0</Version>
<Version>1.0.5.0</Version>
<Authors>eQuantic Systems</Authors>
<TargetFramework>net7.0</TargetFramework>
<PackageId>eQuantic.Core.Api.Crud</PackageId>
Expand All @@ -22,8 +22,8 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/eQuantic/core-api-crud</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<PackageIcon>Icon.png</PackageIcon>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
6 changes: 3 additions & 3 deletions src/eQuantic.Core.Api/eQuantic.Core.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>eQuantic API Library</Description>
<Copyright>2023</Copyright>
<AssemblyTitle>eQuantic.Core.Api</AssemblyTitle>
<Version>1.0.4.0</Version>
<Version>1.0.5.0</Version>
<Authors>eQuantic Systems</Authors>
<TargetFramework>net7.0</TargetFramework>
<PackageId>eQuantic.Core.Api</PackageId>
Expand All @@ -22,8 +22,8 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/eQuantic/core-api-crud</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<PackageIcon>Icon.png</PackageIcon>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
99 changes: 69 additions & 30 deletions src/eQuantic.Core.Application.Crud/Services/CrudServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,10 @@ protected CrudServiceBase(IDefaultUnitOfWork unitOfWork, IMapperFactory mapperFa
{
return null;
}

var result = OnMapEntity(item);

var mapper = _mapperFactory.GetMapper<TDataEntity, TEntity>();
if (mapper == null)
{
return null;
}

var result = mapper.Map(item);

await OnBeforeGetByIdAsync(item, result);
await OnBeforeGetByIdAsync(item, result, cancellationToken);
return result;
}

Expand Down Expand Up @@ -72,31 +66,28 @@ protected CrudServiceBase(IDefaultUnitOfWork unitOfWork, IMapperFactory mapperFa
.WithProperties(OnGetProperties());
}, cancellationToken))
.ToList();
var mapper = _mapperFactory.GetMapper<TDataEntity, TEntity>();

if (mapper == null)
{
return null;
}

var list = pagedList.Select(o => mapper.Map(o)!).ToList();

var list = pagedList
.Select(dataEntity => OnMapEntity(dataEntity)!)
.Where(item => item != null)
.ToList();

await OnBeforeGetPagedListAsync(pagedList, list);
await OnBeforeGetPagedListAsync(pagedList, list, cancellationToken);

return new PagedList<TEntity>(list, count) { PageIndex = request.PageIndex, PageSize = request.PageSize };
}

public async Task<int> CreateAsync(CreateRequest<TRequest> request, CancellationToken cancellationToken = default)
{
var mapper = _mapperFactory.GetMapper<TRequest, TDataEntity>();
var item = mapper?.Map(request.Body);
var item = OnMapRequest(request.Body);
if (item == null)
{
return default;
}

if (request is IReferencedRequest<int> referencedRequest &&
item is IWithReferenceId<TDataEntity, int> referencedItem)
await OnAfterCreateAsync(item, cancellationToken);

if (request is IReferencedRequest<int> referencedRequest && item is IWithReferenceId<TDataEntity, int> referencedItem)
{
referencedItem.SetReferenceId(referencedRequest.ReferenceId);
}
Expand All @@ -108,7 +99,8 @@ public async Task<int> CreateAsync(CreateRequest<TRequest> request, Cancellation

await _repository.AddAsync(item);
await _repository.UnitOfWork.CommitAsync();

await OnBeforeCreateAsync(item, cancellationToken);

return item.Id;
}

Expand All @@ -120,8 +112,9 @@ public async Task<bool> UpdateAsync(UpdateRequest<TRequest> request, Cancellatio
return false;
}

var mapper = _mapperFactory.GetMapper<TRequest, TDataEntity>();
mapper?.Map(request.Body, item);
await OnAfterUpdateAsync(item, cancellationToken);

OnMapRequest(request.Body, item);

if (item is IEntityTrack<TUser> itemWithTrack)
{
Expand All @@ -130,7 +123,8 @@ public async Task<bool> UpdateAsync(UpdateRequest<TRequest> request, Cancellatio

await _repository.ModifyAsync(item);
await _repository.UnitOfWork.CommitAsync();

await OnBeforeUpdateAsync(item, cancellationToken);

return true;
}

Expand All @@ -140,6 +134,8 @@ public async Task<bool> DeleteAsync(ItemRequest request, CancellationToken cance
if (item == null)
return false;

await OnAfterDeleteAsync(item, cancellationToken);

if (item is IEntityHistory<TUser> itemWithHistory)
{
itemWithHistory.DeletedAt = DateTime.UtcNow;
Expand All @@ -151,22 +147,65 @@ public async Task<bool> DeleteAsync(ItemRequest request, CancellationToken cance
}

await _repository.UnitOfWork.CommitAsync();

await OnBeforeDeleteAsync(item, cancellationToken);

return true;
}

protected virtual string[] OnGetProperties()
{
return Array.Empty<string>();
}

protected virtual TEntity? OnMapEntity(TDataEntity dataEntity)
{
var mapper = _mapperFactory.GetMapper<TDataEntity, TEntity>();
return mapper?.Map(dataEntity);
}

protected virtual Task OnBeforeGetByIdAsync(TDataEntity? dataEntity, TEntity? entity)
protected virtual TDataEntity? OnMapRequest(TRequest? request, TDataEntity? dataEntity = null)
{
var mapper = _mapperFactory.GetMapper<TRequest, TDataEntity>();
return mapper?.Map(request, dataEntity);
}

protected virtual Task OnBeforeGetByIdAsync(TDataEntity? dataEntity, TEntity? entity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

protected virtual Task OnBeforeGetPagedListAsync(IEnumerable<TDataEntity> dataEntityList,
IEnumerable<TEntity> entityList)
IEnumerable<TEntity> entityList, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

protected virtual Task OnAfterCreateAsync(TDataEntity? dataEntity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

protected virtual Task OnBeforeCreateAsync(TDataEntity? dataEntity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

protected virtual Task OnAfterUpdateAsync(TDataEntity? dataEntity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

protected virtual Task OnBeforeUpdateAsync(TDataEntity? dataEntity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

protected virtual Task OnAfterDeleteAsync(TDataEntity? dataEntity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}

protected virtual Task OnBeforeDeleteAsync(TDataEntity? dataEntity, CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>eQuantic Application CRUD Library</Description>
<Copyright>2023</Copyright>
<AssemblyTitle>eQuantic.Core.Application.Crud</AssemblyTitle>
<Version>1.0.4.0</Version>
<Version>1.0.5.0</Version>
<Authors>eQuantic Systems</Authors>
<TargetFrameworks>netstandard2.1;net7.0</TargetFrameworks>
<PackageId>eQuantic.Core.Application.Crud</PackageId>
Expand All @@ -22,8 +22,8 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<RepositoryUrl>https://github.com/eQuantic/core-api-crud</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<AssemblyVersion>1.0.4.0</AssemblyVersion>
<FileVersion>1.0.4.0</FileVersion>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<PackageIcon>Icon.png</PackageIcon>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
Expand Down
Loading

0 comments on commit 72f52b9

Please sign in to comment.