From 6ed3a4903c26880783ad5be8c79920663ee184ab Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Mon, 1 Jan 2024 21:04:10 +0330 Subject: [PATCH] Add Support for GetBy --- .../EasyMicroservices.Cores.AspCoreApi.csproj | 2 +- .../ReadableQueryServiceController.cs | 13 ++++++++ ...ces.Cores.AspEntityFrameworkCoreApi.csproj | 2 +- .../EasyMicroservices.Cores.Clients.csproj | 2 +- .../Requests/GetByRequestContract.cs | 23 +++++++++++++ .../EasyMicroservices.Cores.Contracts.csproj | 2 +- .../Database/Interfaces/IReadableLogic.cs | 9 ++++- .../Logics/DatabaseLogicInfrastructure.cs | 33 +++++++++++++++++++ .../Logics/DatabaseMappedLogicBase.cs | 13 ++++++++ .../Logics/IdSchemaDatabaseMappedLogicBase.cs | 13 ++++++++ .../EasyMicroservices.Cores.Database.csproj | 2 +- ...oservices.Cores.EntityFrameworkCore.csproj | 2 +- ...ores.Relational.EntityFrameworkCore.csproj | 2 +- 13 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 src/CSharp/EasyMicroservices.Cores.Contracts/Contracts/Requests/GetByRequestContract.cs diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj index e46e0ba..6525e57 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/EasyMicroservices.Cores.AspCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.90 + 0.0.0.91 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet diff --git a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/ReadableQueryServiceController.cs b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/ReadableQueryServiceController.cs index 40139fc..67278e1 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspCoreApi/ReadableQueryServiceController.cs +++ b/src/CSharp/EasyMicroservices.Cores.AspCoreApi/ReadableQueryServiceController.cs @@ -2,6 +2,7 @@ using EasyMicroservices.Cores.Contracts.Requests; using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.Cores.Interfaces; +using EasyMicroservices.Database.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.AspNetCore.Mvc; using System; @@ -88,6 +89,18 @@ public virtual Task> GetByUniqueIdentity(GetB return ContractLogic.GetByUniqueIdentity(request, request.Type, OnGetQuery(), cancellationToken); } + /// + /// + /// + /// + /// + /// + [HttpPost] + public virtual Task> GetBy(GetByRequestContract request, CancellationToken cancellationToken = default) + { + return ContractLogic.GetBy(request, OnGetQuery(), cancellationToken); + } + /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj index 8b99b50..55d8905 100644 --- a/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj +++ b/src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/EasyMicroservices.Cores.AspEntityFrameworkCoreApi.csproj @@ -4,7 +4,7 @@ net6.0;net7.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.90 + 0.0.0.91 asp core servces. EasyMicroservices@gmail.com core,cores,base,database,services,asp,aspnet,aspcore,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj index 21fef8d..1efd83b 100644 --- a/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Clients/EasyMicroservices.Cores.Clients.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.90 + 0.0.0.91 core of database. EasyMicroservices@gmail.com core,cores,base,client,clients diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/Contracts/Requests/GetByRequestContract.cs b/src/CSharp/EasyMicroservices.Cores.Contracts/Contracts/Requests/GetByRequestContract.cs new file mode 100644 index 0000000..060f1c8 --- /dev/null +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/Contracts/Requests/GetByRequestContract.cs @@ -0,0 +1,23 @@ +using EasyMicroservices.Cores.DataTypes; +using EasyMicroservices.Cores.Interfaces; + +namespace EasyMicroservices.Cores.Contracts.Requests; +/// +/// +/// +/// +public class GetByRequestContract : IUniqueIdentitySchema, IIdSchema +{ + /// + /// + /// + public TId Id { get; set; } + /// + /// + /// + public string UniqueIdentity { get; set; } + /// + /// + /// + public GetUniqueIdentityType? UniqueIdentityType { get; set; } +} diff --git a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj index 017840b..e409409 100644 --- a/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Contracts/EasyMicroservices.Cores.Contracts.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.90 + 0.0.0.91 core contracts. EasyMicroservices@gmail.com core,cores,base,contract,contracts,dto,dtos diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs index b59f1ab..6fbe74d 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Interfaces/IReadableLogic.cs @@ -56,7 +56,14 @@ public interface IContractReadableLogic : IReadableLogi /// /// Task> GetBy(Expression> predicate, Func, IQueryable> query = default, CancellationToken cancellationToken = default); - + /// + /// + /// + /// + /// + /// + /// + Task> GetBy(GetByRequestContract request, Func, IQueryable> query = default, CancellationToken cancellationToken = default); /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs index 6451416..175f1e1 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseLogicInfrastructure.cs @@ -211,6 +211,39 @@ public async Task> GetBy(IEasyReadableQueryabl return result; } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public async Task> GetBy(IEasyReadableQueryableAsync easyReadableQueryable, GetByRequestContract request, Func, IEasyReadableQueryableAsync> query = default, CancellationToken cancellationToken = default) + where TEntity : class + { + var uniqueIdentityPermission = await HasUniqueIdentityPermission(request.UniqueIdentity); + if (!uniqueIdentityPermission) + return uniqueIdentityPermission.ToContract(); + if (!request.Id.Equals(default(TId))) + { + easyReadableQueryable = easyReadableQueryable.ConvertToReadable(easyReadableQueryable.Where(x => ((IIdSchema)x).Id.Equals(request.Id))); + } + if (request.UniqueIdentity.HasValue() && typeof(IUniqueIdentitySchema).IsAssignableFrom(typeof(TEntity))) + { + easyReadableQueryable = await UniqueIdentityQueryMaker(easyReadableQueryable, request.UniqueIdentity, request.UniqueIdentityType ?? GetUniqueIdentityType.All); + } + var entityResult = await GetBy(easyReadableQueryable, query, false, cancellationToken); + if (!entityResult) + return entityResult.ToContract(); + var result = await MapAsync(entityResult.Result); + return result; + } + /// /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs index e852b43..26d5ac2 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/DatabaseMappedLogicBase.cs @@ -6,8 +6,10 @@ using EasyMicroservices.ServiceContracts; using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Linq; using System.Linq.Expressions; +using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; @@ -96,6 +98,17 @@ public async Task> GetBy(Expression(_easyReadableQueryable, predicate, func, cancellationToken); } + /// + /// + /// + /// + /// + /// + /// + public Task> GetBy(GetByRequestContract request, Func, IQueryable> query = default, CancellationToken cancellationToken = default) + { + throw new Exception("GetBy is not supported in DatabaseMappedLogicBase, you can use IdSchemaDatabaseMappedLogicBase or override this GetById method"); + } /// /// diff --git a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs index 21c6f5b..d1ba905 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs +++ b/src/CSharp/EasyMicroservices.Cores.Database/Database/Logics/IdSchemaDatabaseMappedLogicBase.cs @@ -96,6 +96,19 @@ public async Task> GetBy(Expression(_easyReadableQueryable, predicate, func, cancellationToken); } + /// + /// / + /// + /// + /// + /// + /// + public Task> GetBy(GetByRequestContract request, Func, IQueryable> query = default, CancellationToken cancellationToken = default) + { + Func, IEasyReadableQueryableAsync> func = UpdateFunctionQuery(query); + return GetBy(_easyReadableQueryable, request, func, cancellationToken); + } + TId MapToTId(T result) { if (result is IIdSchema schema) diff --git a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj index 3bd936a..1bddf6a 100644 --- a/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Database/EasyMicroservices.Cores.Database.csproj @@ -4,7 +4,7 @@ netstandard2.0;netstandard2.1;net45;net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.90 + 0.0.0.91 core of database. EasyMicroservices@gmail.com core,cores,base,database diff --git a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj index 35dda92..bca126c 100644 --- a/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.EntityFrameworkCore/EasyMicroservices.Cores.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.90 + 0.0.0.91 ef core of database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore diff --git a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj index 26271ab..c7123d8 100644 --- a/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj +++ b/src/CSharp/EasyMicroservices.Cores.Relational.EntityFrameworkCore/EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj @@ -4,7 +4,7 @@ net6.0;net8.0 AnyCPU;x64;x86 EasyMicroservices - 0.0.0.90 + 0.0.0.91 ef core of Relational database. EasyMicroservices@gmail.com core,cores,base,database,ef,efcore,Relational