Skip to content

Commit

Permalink
Add Support for GetBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Jan 1, 2024
1 parent 2ff198a commit 6ed3a49
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.90</Version>
<Version>0.0.0.91</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,6 +89,18 @@ public virtual Task<MessageContract<TResponseContract>> GetByUniqueIdentity(GetB
return ContractLogic.GetByUniqueIdentity(request, request.Type, OnGetQuery(), cancellationToken);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public virtual Task<MessageContract<TResponseContract>> GetBy(GetByRequestContract<TId> request, CancellationToken cancellationToken = default)
{
return ContractLogic.GetBy(request, OnGetQuery(), cancellationToken);
}

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.90</Version>
<Version>0.0.0.91</Version>
<Description>asp core servces.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.90</Version>
<Version>0.0.0.91</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,client,clients</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using EasyMicroservices.Cores.DataTypes;
using EasyMicroservices.Cores.Interfaces;

namespace EasyMicroservices.Cores.Contracts.Requests;
/// <summary>
///
/// </summary>
/// <typeparam name="TId"></typeparam>
public class GetByRequestContract<TId> : IUniqueIdentitySchema, IIdSchema<TId>
{
/// <summary>
///
/// </summary>
public TId Id { get; set; }
/// <summary>
///
/// </summary>
public string UniqueIdentity { get; set; }
/// <summary>
///
/// </summary>
public GetUniqueIdentityType? UniqueIdentityType { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.90</Version>
<Version>0.0.0.91</Version>
<Description>core contracts.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,contract,contracts,dto,dtos</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ public interface IContractReadableLogic<TEntity, TContract, TId> : IReadableLogi
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<MessageContract<TContract>> GetBy(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = default, CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<MessageContract<TContract>> GetBy(GetByRequestContract<TId> request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = default, CancellationToken cancellationToken = default);
/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,39 @@ public async Task<MessageContract<TEntity>> GetBy<TEntity>(IEasyReadableQueryabl
return result;
}


/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TContract"></typeparam>
/// <typeparam name="TId"></typeparam>
/// <param name="easyReadableQueryable"></param>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<MessageContract<TContract>> GetBy<TEntity, TContract, TId>(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, GetByRequestContract<TId> request, Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> query = default, CancellationToken cancellationToken = default)
where TEntity : class
{
var uniqueIdentityPermission = await HasUniqueIdentityPermission<TEntity>(request.UniqueIdentity);
if (!uniqueIdentityPermission)
return uniqueIdentityPermission.ToContract<TContract>();
if (!request.Id.Equals(default(TId)))
{
easyReadableQueryable = easyReadableQueryable.ConvertToReadable(easyReadableQueryable.Where(x => ((IIdSchema<TId>)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<TContract>();
var result = await MapAsync<TContract, TEntity>(entityResult.Result);
return result;
}

/// <summary>
///
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -96,6 +98,17 @@ public async Task<MessageContract<TResponseContract>> GetBy(Expression<Func<TEnt
return await GetBy<TEntity, TResponseContract>(_easyReadableQueryable, predicate, func, cancellationToken);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<MessageContract<TResponseContract>> GetBy(GetByRequestContract<TResponseContract> request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = default, CancellationToken cancellationToken = default)
{
throw new Exception("GetBy is not supported in DatabaseMappedLogicBase, you can use IdSchemaDatabaseMappedLogicBase or override this GetById method");
}

/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ public async Task<MessageContract<TResponseContract>> GetBy(Expression<Func<TEnt
return await GetBy<TEntity, TResponseContract>(_easyReadableQueryable, predicate, func, cancellationToken);
}

/// <summary>
/// /
/// </summary>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<MessageContract<TResponseContract>> GetBy(GetByRequestContract<TId> request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = default, CancellationToken cancellationToken = default)
{
Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> func = UpdateFunctionQuery(query);
return GetBy<TEntity, TResponseContract, TId>(_easyReadableQueryable, request, func, cancellationToken);
}

TId MapToTId<T>(T result)
{
if (result is IIdSchema<TId> schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.90</Version>
<Version>0.0.0.91</Version>
<Description>core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.90</Version>
<Version>0.0.0.91</Version>
<Description>ef core of database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.0.90</Version>
<Version>0.0.0.91</Version>
<Description>ef core of Relational database.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags>
Expand Down

0 comments on commit 6ed3a49

Please sign in to comment.