diff --git a/src/eQuantic.Core.Api.Crud/Handlers/CrudEndpointHandlers.cs b/src/eQuantic.Core.Api.Crud/Handlers/CrudEndpointHandlers.cs index bece1f9..b1bce39 100644 --- a/src/eQuantic.Core.Api.Crud/Handlers/CrudEndpointHandlers.cs +++ b/src/eQuantic.Core.Api.Crud/Handlers/CrudEndpointHandlers.cs @@ -38,7 +38,10 @@ public CrudEndpointHandlers(CrudOptions options) /// /// /// - public async Task, NotFound>> GetReferencedById([FromRoute] int referenceId, [FromRoute] int id, [FromServices]TService service) + public async Task, NotFound>> GetReferencedById( + [FromRoute] int referenceId, + [FromRoute] int id, + [FromServices]TService service) { var request = new ItemRequest(referenceId, id); return await GetById(request, service); @@ -50,7 +53,9 @@ public async Task, NotFound>> GetReferencedById([FromRoute] /// /// /// - public async Task, NotFound>> GetById([FromRoute] int id, [FromServices]TService service) + public async Task, NotFound>> GetById( + [FromRoute] int id, + [FromServices]TService service) { var request = new ItemRequest(id); return await GetById(request, service); @@ -66,7 +71,13 @@ public async Task, NotFound>> GetById([FromRoute] int id, [F /// /// /// - public async Task>> GetReferencedPagedList([FromRoute] int referenceId, [FromQuery] int? pageIndex, [FromQuery] int? pageSize, [FromQuery] IFiltering[]? filterBy, [FromQuery] ISorting[]? orderBy, [FromServices]TService service) + public async Task>> GetReferencedPagedList( + [FromRoute] int referenceId, + [FromQuery] int? pageIndex, + [FromQuery] int? pageSize, + [FromQuery] IFiltering[]? filterBy, + [FromQuery] ISorting[]? orderBy, + [FromServices]TService service) { var request = new PagedListRequest(referenceId, pageIndex, pageSize, filterBy, orderBy); return await GetPagedList(request, service); @@ -81,7 +92,12 @@ public async Task>> GetReferencedPagedList([FromRout /// /// /// - public async Task>> GetPagedList([FromQuery] int? pageIndex, [FromQuery] int? pageSize, [FromQuery] IFiltering[]? filterBy, [FromQuery] ISorting[]? orderBy, [FromServices]TService service) + public async Task>> GetPagedList( + [FromQuery] int? pageIndex, + [FromQuery] int? pageSize, + [FromQuery] IFiltering[]? filterBy, + [FromQuery] ISorting[]? orderBy, + [FromServices]TService service) { var request = new PagedListRequest(pageIndex, pageSize, filterBy, orderBy); return await GetPagedList(request, service); @@ -93,7 +109,9 @@ public async Task>> GetPagedList([FromQuery] int? pa /// /// /// - public async Task> Create([FromBody] TRequest request, [FromServices]TService service) + public async Task> Create( + [FromBody] TRequest request, + [FromServices]TService service) { var createRequest = new CreateRequest(request); return await Create(createRequest, service); @@ -106,7 +124,10 @@ public async Task> Create([FromBody] TRequest request, [From /// /// /// - public async Task> ReferencedCreate([FromRoute] int referenceId, [FromBody] TRequest request, [FromServices]TService service) + public async Task> ReferencedCreate( + [FromRoute] int referenceId, + [FromBody] TRequest request, + [FromServices]TService service) { var createRequest = new CreateRequest(referenceId, request); return await Create(createRequest, service); @@ -119,7 +140,10 @@ public async Task> ReferencedCreate([FromRoute] int referenc /// /// /// - public async Task> Update([FromRoute] int id, [FromBody] TRequest request, [FromServices]TService service) + public async Task> Update( + [FromRoute] int id, + [FromBody] TRequest request, + [FromServices]TService service) { var updateRequest = new UpdateRequest(id, request); return await Update(updateRequest, service); @@ -133,7 +157,11 @@ public async Task> Update([FromRoute] int id, [FromBody] /// /// /// - public async Task> ReferencedUpdate([FromRoute] int referenceId, [FromRoute] int id, [FromBody] TRequest request,[FromServices] TService service) + public async Task> ReferencedUpdate( + [FromRoute] int referenceId, + [FromRoute] int id, + [FromBody] TRequest request, + [FromServices] TService service) { var updateRequest = new UpdateRequest(referenceId, id, request); return await Update(updateRequest, service); @@ -145,7 +173,9 @@ public async Task> ReferencedUpdate([FromRoute] int refe /// /// /// - public async Task> Delete([FromRoute] int id, [FromServices]TService service) + public async Task> Delete( + [FromRoute] int id, + [FromServices]TService service) { var request = new ItemRequest(id); return await Delete(request, service); @@ -158,7 +188,10 @@ public async Task> Delete([FromRoute] int id, [FromServi /// /// /// - public async Task> ReferencedDelete([FromRoute] int referenceId, [FromRoute] int id, [FromServices] TService service) + public async Task> ReferencedDelete( + [FromRoute] int referenceId, + [FromRoute] int id, + [FromServices] TService service) { var request = new ItemRequest(referenceId, id); return await Delete(request, service); @@ -173,14 +206,17 @@ private static async Task, NotFound>> GetById(ItemRequest re return TypedResults.NotFound(); } - private static async Task>> GetPagedList(PagedListRequest request, + private static async Task>> GetPagedList( + PagedListRequest request, TService service) { var result = await service.GetPagedListAsync(request); return TypedResults.Ok(new PagedListResult(result)); } - private async Task> Create(CreateRequest request, TService service) + private async Task> Create( + CreateRequest request, + TService service) { var result = await service.CreateAsync(request); object routeValues = request is CreateRequest referencedRequest ? @@ -189,13 +225,17 @@ private async Task> Create(CreateRequest request, return TypedResults.CreatedAtRoute(result, _options.Get.Name, routeValues); } - private static async Task> Update(UpdateRequest request, TService service) + private static async Task> Update( + UpdateRequest request, + TService service) { var result = await service.UpdateAsync(request); return result ? TypedResults.Ok() : TypedResults.BadRequest(); } - private static async Task> Delete(ItemRequest request, TService service) + private static async Task> Delete( + ItemRequest request, + TService service) { var result = await service.DeleteAsync(request); return result ? TypedResults.Ok() : TypedResults.BadRequest(); diff --git a/src/eQuantic.Core.Api.Crud/eQuantic.Core.Api.Crud.csproj b/src/eQuantic.Core.Api.Crud/eQuantic.Core.Api.Crud.csproj index 9a0dd9a..8d5e685 100644 --- a/src/eQuantic.Core.Api.Crud/eQuantic.Core.Api.Crud.csproj +++ b/src/eQuantic.Core.Api.Crud/eQuantic.Core.Api.Crud.csproj @@ -4,7 +4,7 @@ eQuantic API CRUD Library 2023 eQuantic.Core.Api.Crud - 1.0.4.0 + 1.0.5.0 eQuantic Systems net7.0 eQuantic.Core.Api.Crud @@ -22,8 +22,8 @@ LICENSE https://github.com/eQuantic/core-api-crud Git - 1.0.4.0 - 1.0.4.0 + 1.0.5.0 + 1.0.5.0 Icon.png latest enable diff --git a/src/eQuantic.Core.Api/eQuantic.Core.Api.csproj b/src/eQuantic.Core.Api/eQuantic.Core.Api.csproj index 5699edb..3f242b9 100644 --- a/src/eQuantic.Core.Api/eQuantic.Core.Api.csproj +++ b/src/eQuantic.Core.Api/eQuantic.Core.Api.csproj @@ -4,7 +4,7 @@ eQuantic API Library 2023 eQuantic.Core.Api - 1.0.4.0 + 1.0.5.0 eQuantic Systems net7.0 eQuantic.Core.Api @@ -22,8 +22,8 @@ LICENSE https://github.com/eQuantic/core-api-crud Git - 1.0.4.0 - 1.0.4.0 + 1.0.5.0 + 1.0.5.0 Icon.png latest enable diff --git a/src/eQuantic.Core.Application.Crud/Services/CrudServiceBase.cs b/src/eQuantic.Core.Application.Crud/Services/CrudServiceBase.cs index a8aa7a1..04f7c15 100644 --- a/src/eQuantic.Core.Application.Crud/Services/CrudServiceBase.cs +++ b/src/eQuantic.Core.Application.Crud/Services/CrudServiceBase.cs @@ -35,16 +35,10 @@ protected CrudServiceBase(IDefaultUnitOfWork unitOfWork, IMapperFactory mapperFa { return null; } + + var result = OnMapEntity(item); - var mapper = _mapperFactory.GetMapper(); - if (mapper == null) - { - return null; - } - - var result = mapper.Map(item); - - await OnBeforeGetByIdAsync(item, result); + await OnBeforeGetByIdAsync(item, result, cancellationToken); return result; } @@ -72,31 +66,28 @@ protected CrudServiceBase(IDefaultUnitOfWork unitOfWork, IMapperFactory mapperFa .WithProperties(OnGetProperties()); }, cancellationToken)) .ToList(); - var mapper = _mapperFactory.GetMapper(); - - 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(list, count) { PageIndex = request.PageIndex, PageSize = request.PageSize }; } - + public async Task CreateAsync(CreateRequest request, CancellationToken cancellationToken = default) { - var mapper = _mapperFactory.GetMapper(); - var item = mapper?.Map(request.Body); + var item = OnMapRequest(request.Body); if (item == null) { return default; } - if (request is IReferencedRequest referencedRequest && - item is IWithReferenceId referencedItem) + await OnAfterCreateAsync(item, cancellationToken); + + if (request is IReferencedRequest referencedRequest && item is IWithReferenceId referencedItem) { referencedItem.SetReferenceId(referencedRequest.ReferenceId); } @@ -108,7 +99,8 @@ public async Task CreateAsync(CreateRequest request, Cancellation await _repository.AddAsync(item); await _repository.UnitOfWork.CommitAsync(); - + await OnBeforeCreateAsync(item, cancellationToken); + return item.Id; } @@ -120,8 +112,9 @@ public async Task UpdateAsync(UpdateRequest request, Cancellatio return false; } - var mapper = _mapperFactory.GetMapper(); - mapper?.Map(request.Body, item); + await OnAfterUpdateAsync(item, cancellationToken); + + OnMapRequest(request.Body, item); if (item is IEntityTrack itemWithTrack) { @@ -130,7 +123,8 @@ public async Task UpdateAsync(UpdateRequest request, Cancellatio await _repository.ModifyAsync(item); await _repository.UnitOfWork.CommitAsync(); - + await OnBeforeUpdateAsync(item, cancellationToken); + return true; } @@ -140,6 +134,8 @@ public async Task DeleteAsync(ItemRequest request, CancellationToken cance if (item == null) return false; + await OnAfterDeleteAsync(item, cancellationToken); + if (item is IEntityHistory itemWithHistory) { itemWithHistory.DeletedAt = DateTime.UtcNow; @@ -151,7 +147,8 @@ public async Task DeleteAsync(ItemRequest request, CancellationToken cance } await _repository.UnitOfWork.CommitAsync(); - + await OnBeforeDeleteAsync(item, cancellationToken); + return true; } @@ -159,14 +156,56 @@ protected virtual string[] OnGetProperties() { return Array.Empty(); } + + protected virtual TEntity? OnMapEntity(TDataEntity dataEntity) + { + var mapper = _mapperFactory.GetMapper(); + 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(); + return mapper?.Map(request, dataEntity); + } + + protected virtual Task OnBeforeGetByIdAsync(TDataEntity? dataEntity, TEntity? entity, CancellationToken cancellationToken = default) { return Task.CompletedTask; } protected virtual Task OnBeforeGetPagedListAsync(IEnumerable dataEntityList, - IEnumerable entityList) + IEnumerable 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; } diff --git a/src/eQuantic.Core.Application.Crud/eQuantic.Core.Application.Crud.csproj b/src/eQuantic.Core.Application.Crud/eQuantic.Core.Application.Crud.csproj index b911de2..612de17 100644 --- a/src/eQuantic.Core.Application.Crud/eQuantic.Core.Application.Crud.csproj +++ b/src/eQuantic.Core.Application.Crud/eQuantic.Core.Application.Crud.csproj @@ -4,7 +4,7 @@ eQuantic Application CRUD Library 2023 eQuantic.Core.Application.Crud - 1.0.4.0 + 1.0.5.0 eQuantic Systems netstandard2.1;net7.0 eQuantic.Core.Application.Crud @@ -22,8 +22,8 @@ LICENSE https://github.com/eQuantic/core-api-crud Git - 1.0.4.0 - 1.0.4.0 + 1.0.5.0 + 1.0.5.0 Icon.png latest enable diff --git a/src/eQuantic.Core.Application/eQuantic.Core.Application.csproj b/src/eQuantic.Core.Application/eQuantic.Core.Application.csproj index 57d4cc0..5c349f4 100644 --- a/src/eQuantic.Core.Application/eQuantic.Core.Application.csproj +++ b/src/eQuantic.Core.Application/eQuantic.Core.Application.csproj @@ -4,7 +4,7 @@ eQuantic Application Library 2023 eQuantic.Core.Application - 1.0.4.0 + 1.0.5.0 eQuantic Systems netstandard2.1;net7.0 eQuantic.Core.Application @@ -22,8 +22,8 @@ LICENSE https://github.com/eQuantic/core-api-crud Git - 1.0.4.0 - 1.0.4.0 + 1.0.5.0 + 1.0.5.0 Icon.png latest enable