Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Multiple language inside of the core querables #96

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/CSharp/EasyMicroservices.Cores.AspCore.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.Cores.Database.Managers;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore;
using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces;
using EasyMicroservices.Cores.Tests.DatabaseLogics.Database.Contexts;
using EasyMicroservices.Cores.Tests.DatabaseLogics.Database.Entities;
Expand All @@ -18,7 +19,7 @@ public static async Task Main(string[] args)
app.Services.Builder<MyTestContext>();
app.Services.AddScoped((serviceProvider) => new UnitOfWork(serviceProvider).GetLongContractLogic<UserEntity, UserEntity, UserEntity, UserEntity>());
app.Services.AddTransient(serviceProvider => new MyTestContext(serviceProvider.GetService<IEntityFrameworkCoreDatabaseBuilder>()));
app.Services.AddScoped<IEntityFrameworkCoreDatabaseBuilder>(serviceProvider => new DatabaseBuilder());
app.Services.AddScoped<IEntityFrameworkCoreDatabaseBuilder, DatabaseBuilder>();
app.Services.AddSingleton(service => new WhiteLabelManager(service));
app.Services.AddSingleton<IUniqueIdentityManager, DefaultUniqueIdentityManager>((provider) =>
{
Expand All @@ -31,11 +32,18 @@ public static async Task Main(string[] args)
}
}

public class DatabaseBuilder : IEntityFrameworkCoreDatabaseBuilder
public class DatabaseBuilder : EntityFrameworkCoreDatabaseBuilder
{
public void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
public DatabaseBuilder(IConfiguration configuration) : base(configuration)
{
optionsBuilder.UseInMemoryDatabase("Test DB");
}

public override void OnConfiguring(DbContextOptionsBuilder optionsBuilder, string name)
{
if (name == "InMemoryDatabase")
optionsBuilder.UseInMemoryDatabase("Test DB");
else
optionsBuilder.UseInMemoryDatabase("Test DB");
}
}
}
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 All @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.AuthenticationsMicroservice.Clients" Version="0.0.0.17" />
<PackageReference Include="EasyMicroservices.AuthenticationsMicroservice.Clients" Version="*" />
</ItemGroup>

<ItemGroup>
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
Loading