Skip to content

Commit

Permalink
Support for Multiple language inside of the core querables
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Dec 16, 2023
1 parent 062ee94 commit 7d0415e
Show file tree
Hide file tree
Showing 29 changed files with 876 additions and 67 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.64</Version>
<Version>0.0.0.65</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
@@ -0,0 +1,87 @@
using EasyMicroservices.Cores.Contracts.Requests;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.ServiceContracts;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;


namespace EasyMicroservices.Cores.AspCoreApi.Interfaces;
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TFilterContract"></typeparam>
/// <typeparam name="TResponseContract"></typeparam>
/// <typeparam name="TIdRequestContract"></typeparam>
/// <typeparam name="TUniqueIdentityRequestContract"></typeparam>
/// <typeparam name="TId"></typeparam>
public interface IReadableQueryServiceController<TEntity, TFilterContract, TResponseContract, TId, TIdRequestContract, TUniqueIdentityRequestContract>
where TResponseContract : class
where TEntity : class
where TFilterContract : FilterRequestContract
{
/// <summary>
///
/// </summary>
protected IContractReadableLogic<TEntity, TResponseContract, TId> ContractLogic { get; set; }

/// <summary>
///
/// </summary>
protected IBaseUnitOfWork UnitOfWork { get; }

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<MessageContract<TResponseContract>> GetById(TIdRequestContract request, CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<MessageContract<TResponseContract>> GetByUniqueIdentity(TUniqueIdentityRequestContract request, CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <param name="filterRequest"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<ListMessageContract<TResponseContract>> Filter(TFilterContract filterRequest, CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<ListMessageContract<TResponseContract>> GetAll(CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<ListMessageContract<TResponseContract>> GetAllByUniqueIdentity(TUniqueIdentityRequestContract request, CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <returns></returns>
protected Func<IQueryable<TEntity>, IQueryable<TEntity>> OnGetQuery();

/// <summary>
///
/// </summary>
/// <returns></returns>
protected Func<IQueryable<TEntity>, IQueryable<TEntity>> OnGetAllQuery();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
using EasyMicroservices.Cores.AspCoreApi.Interfaces;
using EasyMicroservices.Cores.Contracts.Requests;
using EasyMicroservices.Cores.Contracts.Requests.Multilingual;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Interfaces;
using EasyMicroservices.ServiceContracts;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;


namespace EasyMicroservices.Cores.AspCoreApi;
/// <summary>
///
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TFilterContract"></typeparam>
/// <typeparam name="TResponseContract"></typeparam>
/// <typeparam name="TLanguageResponseContract"></typeparam>
/// <typeparam name="TId"></typeparam>
[ApiController]
[Route("api/[controller]/[action]")]
public class MultilingualReadableQueryServiceController<TEntity, TFilterContract, TResponseContract, TLanguageResponseContract, TId> :
ControllerBase, IReadableQueryServiceController<TEntity, TFilterContract, TResponseContract, TId, IdLanguageRequestContract<TId>, UniqueIdentityLanguageRequestContract>
where TResponseContract : class
where TLanguageResponseContract : class
where TEntity : class
where TFilterContract : FilterRequestContract
{
/// <summary>
///
/// </summary>
public virtual IContractReadableLogic<TEntity, TResponseContract, TId> ContractLogic { get; set; }

/// <summary>
///
/// </summary>
public virtual IBaseUnitOfWork UnitOfWork { get; }

/// <summary>
///
/// </summary>
/// <param name="contractReadable"></param>
public MultilingualReadableQueryServiceController(IContractReadableLogic<TEntity, TResponseContract, TId> contractReadable)
{
ContractLogic = contractReadable;
}

/// <summary>
///
/// </summary>
/// <param name="unitOfWork"></param>
/// <param name="contractReadable"></param>
public MultilingualReadableQueryServiceController(IBaseUnitOfWork unitOfWork, IContractReadableLogic<TEntity, TResponseContract, TId> contractReadable)
{
ContractLogic = contractReadable;
UnitOfWork = unitOfWork;
}
/// <summary>
///
///
/// </summary>
/// <param name="unitOfWork"></param>
public MultilingualReadableQueryServiceController(IBaseUnitOfWork unitOfWork)
{
ContractLogic = unitOfWork.GetReadableContractLogic<TEntity, TResponseContract, TId>();
}

async Task<MessageContract<T>> ResolveContentLanguage<T>(MessageContract<T> result, string languageShortName)
{
if (!result)
return result;
var contentResolver = UnitOfWork.GetContentResolver();
await contentResolver.ResolveContentLanguage(result.Result, languageShortName);
return result;
}

async Task<ListMessageContract<T>> ResolveContentLanguage<T>(ListMessageContract<T> result, string languageShortName)
{
if (!result)
return result;
var contentResolver = UnitOfWork.GetContentResolver();
await contentResolver.ResolveContentLanguage(result.Result, languageShortName);
return result;
}

async Task<MessageContract<TLanguageResponseContract>> ResolveContentAllLanguage<T>(MessageContract<T> result)
{
if (!result)
return result.ToContract<TLanguageResponseContract>();
var contentResolver = UnitOfWork.GetContentResolver();
var mapper = UnitOfWork.GetMapper();
var mapped = mapper.Map<TLanguageResponseContract>(result.Result);
await contentResolver.ResolveContentAllLanguage(mapped);
return mapped;
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public virtual async Task<MessageContract<TResponseContract>> GetById(IdLanguageRequestContract<TId> request, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetById(request, OnGetQuery(), cancellationToken);
return await ResolveContentLanguage(result, request.LanguageShortName);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageContract<TLanguageResponseContract>> GetByIdAllLanguage(IdRequestContract<TId> request, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetById(request, OnGetQuery(), cancellationToken);
return await ResolveContentAllLanguage(result);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public virtual async Task<MessageContract<TResponseContract>> GetByUniqueIdentity(UniqueIdentityLanguageRequestContract request, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetByUniqueIdentity(request, request.Type, OnGetQuery(), cancellationToken);
return await ResolveContentLanguage(result, request.LanguageShortName);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageContract<TLanguageResponseContract>> GetByUniqueIdentityAllLanguage(UniqueIdentityRequestContract request, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetByUniqueIdentity(request, request.Type, OnGetQuery(), cancellationToken);
return await ResolveContentAllLanguage(result);
}

/// <summary>
///
/// </summary>
/// <param name="filterRequest"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public virtual async Task<ListMessageContract<TResponseContract>> Filter(TFilterContract filterRequest, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.Filter(filterRequest, OnGetAllQuery(), cancellationToken);
return await ResolveContentLanguage(result, filterRequest.LanguageShortName);
}

/// <summary>
///
/// </summary>
/// <param name="filterRequest"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageContract<TLanguageResponseContract>> FilterAllLanguage(TFilterContract filterRequest, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.Filter(filterRequest, OnGetAllQuery(), cancellationToken);
return await ResolveContentAllLanguage(result);
}

/// <summary>
///
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet]
public virtual async Task<ListMessageContract<TResponseContract>> GetAll(CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetAll(OnGetAllQuery(), cancellationToken);
return await ResolveContentLanguage(result, default);
}

/// <summary>
///
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageContract<TLanguageResponseContract>> GetAllWithAllLanguage(CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetAll(OnGetAllQuery(), cancellationToken);
return await ResolveContentAllLanguage(result);
}

/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public virtual async Task<ListMessageContract<TResponseContract>> GetAllByUniqueIdentity(UniqueIdentityLanguageRequestContract request, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetAllByUniqueIdentity(request, request.Type, OnGetAllQuery(), cancellationToken);
return await ResolveContentLanguage(result, request.LanguageShortName);
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageContract<TLanguageResponseContract>> GetAllByUniqueIdentityAllLanguage(UniqueIdentityRequestContract request, CancellationToken cancellationToken = default)
{
var result = await ContractLogic.GetAllByUniqueIdentity(request, request.Type, OnGetAllQuery(), cancellationToken);
return await ResolveContentAllLanguage(result);
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual Func<IQueryable<TEntity>, IQueryable<TEntity>> OnGetQuery()
{
return null;
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public virtual Func<IQueryable<TEntity>, IQueryable<TEntity>> OnGetAllQuery()
{
return null;
}
}
Loading

0 comments on commit 7d0415e

Please sign in to comment.