Skip to content

Commit

Permalink
Merge pull request #15 from Ali-YousefiTelori/develop
Browse files Browse the repository at this point in the history
Refactor strucure and support for uniqueidentity queries
  • Loading branch information
Ali-YousefiTelori authored Jul 25, 2023
2 parents 0a074bc + 8ef3c2b commit 72dbdb5
Show file tree
Hide file tree
Showing 21 changed files with 292 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.9</Version>
<Version>0.0.0.10</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
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Contracts.Contracts.Requests;
using EasyMicroservices.Cores.Database.Interfaces;
using Microsoft.AspNetCore.Mvc;
using ServiceContracts;
using System;
Expand Down Expand Up @@ -46,6 +47,18 @@ public virtual Task<MessageContract<TResponseContract>> GetById(TId id, Cancella
return ContractLogic.GetById(id, OnGetQuery(), cancellationToken);
}

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

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -76,7 +89,7 @@ public virtual Task<MessageContract<TResponseContract>> Update(TUpdateRequestCon
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
[HttpDelete]
public virtual Task<MessageContract> HardDeleteById(TId id, CancellationToken cancellationToken = default)
{
return ContractLogic.HardDeleteById(id, cancellationToken);
Expand All @@ -93,6 +106,17 @@ public virtual Task<MessageContract<List<TResponseContract>>> GetAll(Cancellatio
return ContractLogic.GetAll(OnGetAllQuery(), cancellationToken);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet]
public virtual Task<MessageContract<List<TResponseContract>>> GetAllByUniqueIdentity(GetUniqueIdentityRequest request, CancellationToken cancellationToken = default)
{
return ContractLogic.GetAllByUniqueIdentity(request, OnGetAllQuery(), cancellationToken);
}

/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using EasyMicroservices.Cores.Interfaces;

namespace EasyMicroservices.Cores.Contracts.Contracts.Requests
{
/// <summary>
///
/// </summary>
public class GetUniqueIdentityRequest : IUniqueIdentitySchema
{
/// <summary>
///
/// </summary>
public string UniqueIdentity { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.0.0.1</Version>
<Version>0.0.0.2</Version>
<Description>core.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base</PackageTags>
<PackageProjectUrl>https://github.com/EasyMicroservices/Cores</PackageProjectUrl>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DocumentationFile>.\bin\$(Configuration)\$(TargetFramework)\EasyMicroservices.Cores.Contracts.xml</DocumentationFile>
<RootNamespace>EasyMicroservices.Cores</RootNamespace>
</PropertyGroup>

<ItemGroup>
<Folder Include="Contracts\Common\" />
<Folder Include="Contracts\Responses\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EasyMicroservices.Cores.Database.Interfaces
namespace EasyMicroservices.Cores.Interfaces
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace EasyMicroservices.Cores.Database.Interfaces
namespace EasyMicroservices.Cores.Interfaces
{
/// <summary>
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;

namespace EasyMicroservices.Cores.Database.Entities
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ServiceContracts;
using EasyMicroservices.Cores.Interfaces;
using ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -55,12 +56,29 @@ public interface IContractReadableLogic<TEntity, TContract, TId> : IReadableLogi
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<MessageContract<TContract>> GetById(TId id, 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>> GetByUniqueIdentity(IUniqueIdentitySchema request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = default, CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<MessageContract<List<TContract>>> GetAll(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<List<TContract>>> GetAllByUniqueIdentity(IUniqueIdentitySchema request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = default, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EasyMicroservices.Database.Interfaces;
using System;

namespace EasyMicroservices.Cores.Database.Interfaces
{
Expand All @@ -16,5 +15,13 @@ public interface IUniqueIdentityManager
/// <param name="entity"></param>
/// <returns>is need update database</returns>
bool UpdateUniqueIdentity<TEntity>(IContext context, TEntity entity);
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="context"></param>
/// <param name="uniqueIdentity"></param>
/// <returns></returns>
bool IsUniqueIdentityForThisTable<TEntity>(IContext context, string uniqueIdentity);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.Interfaces;
using ServiceContracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.Interfaces;
using ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -54,6 +56,16 @@ protected virtual void ValidateMappedResult<T>(ref T value)
throw new NullReferenceException("the result was null when we mapped it to contract! something went wrong!");
}

private IEasyReadableQueryableAsync<TEntity> UniqueIdentityQueryMaker<TEntity>(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, string uniqueIdentity)
where TEntity : class
{
IEasyReadableQueryableAsync<TEntity> queryable = easyReadableQueryable;
if (!_uniqueIdentityManager.IsUniqueIdentityForThisTable<TEntity>(easyReadableQueryable.Context, uniqueIdentity))
uniqueIdentity += "-";
queryable = queryable.ConvertToReadable(queryable.Where(x => (x as IUniqueIdentitySchema).UniqueIdentity.StartsWith(uniqueIdentity)));
return queryable;
}

#region Get one

/// <summary>
Expand Down Expand Up @@ -99,6 +111,26 @@ public async Task<MessageContract<TEntity>> GetBy<TEntity>(IEasyReadableQueryabl
return result;
}

/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="easyReadableQueryable"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<MessageContract<TEntity>> GetBy<TEntity>(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> query = default, CancellationToken cancellationToken = default)
where TEntity : class
{
IEasyReadableQueryableAsync<TEntity> queryable = easyReadableQueryable;
if (query != null)
queryable = query(queryable);
var result = await queryable.FirstOrDefaultAsync(cancellationToken);
if (result == null)
return (FailedReasonType.NotFound, $"Item by predicate not found!");
return result;
}

/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -144,6 +176,31 @@ protected async Task<MessageContract<TContract>> GetById<TEntity, TContract, TId
return result;
}

/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TContract"></typeparam>
/// <param name="easyReadableQueryable"></param>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<MessageContract<TContract>> GetByUniqueIdentity<TEntity, TContract>(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IUniqueIdentitySchema request, Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> query = null, CancellationToken cancellationToken = default)
where TEntity : class
where TContract : class
{
IEasyReadableQueryableAsync<TEntity> queryable = UniqueIdentityQueryMaker(easyReadableQueryable, request.UniqueIdentity);
var entityResult = await GetBy(queryable, query, cancellationToken);
if (!entityResult)
return entityResult.ToContract<TContract>();
var result = await _mapperProvider.MapAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
return result;
}


#endregion

#region Get list
Expand Down Expand Up @@ -186,6 +243,30 @@ protected async Task<MessageContract<List<TContract>>> GetAll<TEntity, TContract
return result;
}

/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TContract"></typeparam>
/// <param name="easyReadableQueryable"></param>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<MessageContract<List<TContract>>> GetAllByUniqueIdentity<TEntity, TContract>(IEasyReadableQueryableAsync<TEntity> easyReadableQueryable, IUniqueIdentitySchema request, Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> query = null, CancellationToken cancellationToken = default)
where TEntity : class
where TContract : class
{
IEasyReadableQueryableAsync<TEntity> queryable = UniqueIdentityQueryMaker(easyReadableQueryable, request.UniqueIdentity);
var entityResult = await GetAll(queryable, query, cancellationToken);
if (!entityResult)
return entityResult.ToContract<List<TContract>>();
var result = await _mapperProvider.MapToListAsync<TContract>(entityResult.Result);
ValidateMappedResult(ref result);
return result;
}

#endregion

#region Update
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.Interfaces;
using ServiceContracts;
Expand Down Expand Up @@ -182,5 +183,35 @@ public Task<MessageContract> HardDeleteById(TResponseContract contract, Cancella
{
throw new Exception("HardDeleteById is not supported in DatabaseMappedLogicBase, you can use IdSchemaDatabaseMappedLogicBase or override this HardDeleteById method");
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<MessageContract<TResponseContract>> GetByUniqueIdentity(IUniqueIdentitySchema request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = null, CancellationToken cancellationToken = default)
{
Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> func = null;
if (query != null)
func = (q) => _easyReadableQueryable.ConvertToReadable(query(_easyReadableQueryable));
return base.GetByUniqueIdentity<TEntity, TResponseContract>(_easyReadableQueryable, request, func, cancellationToken);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<MessageContract<List<TResponseContract>>> GetAllByUniqueIdentity(IUniqueIdentitySchema request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = null, CancellationToken cancellationToken = default)
{
Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> func = null;
if (query != null)
func = (q) => _easyReadableQueryable.ConvertToReadable(query(_easyReadableQueryable));
return base.GetAllByUniqueIdentity<TEntity, TResponseContract>(_easyReadableQueryable, request, func, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.Interfaces;
using ServiceContracts;
Expand Down Expand Up @@ -181,5 +182,36 @@ public Task<MessageContract> HardDeleteById(TId id, CancellationToken cancellati
{
return HardDeleteById<TEntity, TResponseContract, TId>(_easyWriteableQueryable, id, cancellationToken);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public Task<MessageContract<List<TResponseContract>>> GetAllByUniqueIdentity(IUniqueIdentitySchema request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = null, CancellationToken cancellationToken = default)
{
Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> func = null;
if (query != null)
func = (q) => _easyReadableQueryable.ConvertToReadable(query(_easyReadableQueryable));
return base.GetAllByUniqueIdentity<TEntity, TResponseContract>(_easyReadableQueryable, request, func, cancellationToken);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="query"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<MessageContract<TResponseContract>> GetByUniqueIdentity(IUniqueIdentitySchema request, Func<IQueryable<TEntity>, IQueryable<TEntity>> query = null, CancellationToken cancellationToken = default)
{
Func<IEasyReadableQueryableAsync<TEntity>, IEasyReadableQueryableAsync<TEntity>> func = null;
if (query != null)
func = (q) => _easyReadableQueryable.ConvertToReadable(query(_easyReadableQueryable));
return base.GetByUniqueIdentity<TEntity, TResponseContract>(_easyReadableQueryable, request, func, cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.Interfaces;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.Database.Interfaces;
using EasyMicroservices.Mapper.Interfaces;

Expand Down
Loading

0 comments on commit 72dbdb5

Please sign in to comment.