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

Work with UnitOfWork #19

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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

<ItemGroup>
<Folder Include="Database\Documents\" />
<Folder Include="Migrations\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Configuration" Version="0.0.0.2" />
<PackageReference Include="EasyMicroservices.Cores.Relational.EntityFrameworkCore" Version="0.0.0.9" />
<PackageReference Include="EasyMicroservices.Cores.Relational.EntityFrameworkCore" Version="0.0.0.20" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Cores.Database" Version="0.0.0.21" />
<PackageReference Include="EasyMicroservices.Cores.Database" Version="0.0.0.32" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi;
using System;
using System.Threading.Tasks;

namespace EasyMicroservices.ContentsMicroservice
{
public class AppUnitOfWork : UnitOfWork
{
public AppUnitOfWork(IServiceProvider service) : base(service)
{
}


public override void Dispose()
{
base.Dispose();
}


public override ValueTask DisposeAsync()
{
return base.DisposeAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Mapper.CompileTimeMapper" Version="0.0.0.5" />
<PackageReference Include="EasyMicroservices.Mapper.SerializerMapper" Version="0.0.0.2" />
<PackageReference Include="EasyMicroservices.Serialization.Newtonsoft.Json" Version="0.0.0.3" />
<PackageReference Include="EasyMicroservices.Cores.AspEntityFrameworkCoreApi" Version="0.0.0.12" />
<PackageReference Include="EasyMicroservices.Mapper.CompileTimeMapper" Version="0.0.0.6" />
<PackageReference Include="EasyMicroservices.Mapper.SerializerMapper" Version="0.0.0.3" />
<PackageReference Include="EasyMicroservices.Serialization.Newtonsoft.Json" Version="0.0.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class DatabaseBuilder : IDatabaseBuilder

public void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseInMemoryDatabase("ContentDatabase");
//optionsBuilder.UseSqlServer(config.GetConnectionString("local"));
//optionsBuilder.UseInMemoryDatabase("ContentDatabase");
optionsBuilder.UseSqlServer(config.GetConnectionString("local"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public async Task Initialize(string microserviceName, string whiteLableRoute, pa

var uniqueIdentityManager = _dependencyManager.GetUniqueIdentityManager() as DefaultUniqueIdentityManager;


AppUnitOfWork.MicroserviceId = DependencyManager.MicroserviceId;
AppUnitOfWork.DefaultUniqueIdentity = DependencyManager.DefaultUniqueIdentity;

var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, HttpClient);
var microserviceContextTables = await microserviceContextTableClient.GetAllAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="EasyMicroservices.Tests" Version="0.0.0.7" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit" Version="2.5.1" />
<PackageReference Include="xunit.runner.console" Version="2.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
using EasyMicroservices.Cores.AspCoreApi;
using EasyMicroservices.Cores.Database.Interfaces;
using EasyMicroservices.ContentsMicroservice.Contracts.Common;
using EasyMicroservices.ContentsMicroservice.Contracts.Common;
using EasyMicroservices.ContentsMicroservice.Contracts.Requests;
using EasyMicroservices.ContentsMicroservice.Database.Entities;
using EasyMicroservices.ServiceContracts;
using EasyMicroservices.Cores.AspCoreApi;
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces;
using EasyMicroservices.Cores.Contracts.Requests;
using Microsoft.EntityFrameworkCore;
using EasyMicroservices.ServiceContracts;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EasyMicroservices.QuestionsMicroservice.WebApi.Controllers
{
public class ContentController : SimpleQueryServiceController<ContentEntity, CreateContentRequestContract, UpdateContentRequestContract, ContentContract, long>
{
private readonly IContractLogic<ContentEntity, CreateContentRequestContract, UpdateContentRequestContract, ContentContract, long> _contractlogic;
private readonly IContractLogic<LanguageEntity, CreateLanguageRequestContract, UpdateLanguageRequestContract, LanguageContract, long> _languagelogic;
private readonly IContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract, long> _categorylogic;

public ContentController(IContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract, long> categorylogic, IContractLogic<LanguageEntity, CreateLanguageRequestContract, UpdateLanguageRequestContract, LanguageContract, long> languagelogic, IContractLogic<ContentEntity, CreateContentRequestContract, UpdateContentRequestContract, ContentContract, long> contractLogic) : base(contractLogic)
readonly IUnitOfWork unitOfWork;
public ContentController(IUnitOfWork _unitOfWork) : base(null)
{
_contractlogic = contractLogic;
_languagelogic = languagelogic;
_categorylogic = categorylogic;
unitOfWork = _unitOfWork;
}
public override async Task<MessageContract<long>> Add(CreateContentRequestContract request, CancellationToken cancellationToken = default)
{
var checkLanguageId = await _languagelogic.GetById(new GetIdRequestContract<long>() { Id = request.LanguageId });
var checkCategoryId = await _categorylogic.GetById(new GetIdRequestContract<long>() { Id = request.CategoryId });
using var categorylogic = unitOfWork.GetLongContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract>();
using var languageLogic = unitOfWork.GetLongContractLogic<LanguageEntity, LanguageContract>();
var checkLanguageId = await languageLogic.GetById(new GetIdRequestContract<long>() { Id = request.LanguageId });
var checkCategoryId = await categorylogic.GetById(new GetIdRequestContract<long>() { Id = request.CategoryId });
if (checkLanguageId.IsSuccess && checkCategoryId.IsSuccess)
return await base.Add(request, cancellationToken);
return (EasyMicroservices.ServiceContracts.FailedReasonType.Incorrect, "Language or Categoryid is incorrect");
}
public override async Task<MessageContract<ContentContract>> Update(UpdateContentRequestContract request, CancellationToken cancellationToken = default)
{
var checkLanguageId = await _languagelogic.GetById(new GetIdRequestContract<long>() { Id = request.LanguageId });
var checkCategoryId = await _categorylogic.GetById(new GetIdRequestContract<long>() { Id = request.CategoryId });
using var categorylogic = unitOfWork.GetLongContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract>();
using var languageLogic = unitOfWork.GetLongContractLogic<LanguageEntity, LanguageContract>();
var checkLanguageId = await languageLogic.GetById(new GetIdRequestContract<long>() { Id = request.LanguageId });
var checkCategoryId = await categorylogic.GetById(new GetIdRequestContract<long>() { Id = request.CategoryId });
if (checkLanguageId.IsSuccess && checkCategoryId.IsSuccess)
return await base.Update(request, cancellationToken);
return (EasyMicroservices.ServiceContracts.FailedReasonType.Incorrect, "Language or Categoryid is incorrect");
Expand All @@ -43,7 +42,8 @@ public override async Task<MessageContract<ContentContract>> Update(UpdateConten
[HttpPost]
public async Task<MessageContract<ContentContract>> GetByLanguage(GetByLanguageRequestContract request)
{
var getCategoryResult = await _categorylogic.GetBy(x => x.Key == request.Key, query => query.Include(x => x.Contents).ThenInclude(x => x.Language));
using var categorylogic = unitOfWork.GetLongContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract>();
var getCategoryResult = await categorylogic.GetBy(x => x.Key == request.Key, query => query.Include(x => x.Contents).ThenInclude(x => x.Language));
if (!getCategoryResult)
return getCategoryResult.ToContract<ContentContract>();
var contentResult = getCategoryResult.Result.Contents.FirstOrDefault(x => x.Language.Name.Equals(request.Language, StringComparison.OrdinalIgnoreCase));
Expand All @@ -59,7 +59,8 @@ public async Task<MessageContract<ContentContract>> GetByLanguage(GetByLanguageR
[HttpPost]
public async Task<ListMessageContract<ContentContract>> GetAllByKey(GetAllByKeyRequestContract request)
{
var getCategoryResult = await _categorylogic.GetBy(x => x.Key == request.Key, query => query.Include(x => x.Contents).ThenInclude(x => x.Language));
using var categorylogic = unitOfWork.GetLongContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract>();
var getCategoryResult = await categorylogic.GetBy(x => x.Key == request.Key, query => query.Include(x => x.Contents).ThenInclude(x => x.Language));
if (!getCategoryResult)
return getCategoryResult.ToListContract<ContentContract>();

Expand All @@ -69,16 +70,21 @@ public async Task<ListMessageContract<ContentContract>> GetAllByKey(GetAllByKeyR
[HttpPost]
public async Task<MessageContract<CategoryContract>> AddContentWithKey(AddContentWithKeyRequestContract request)
{
var getCategoryResult = await _categorylogic.GetBy(x => x.Key == request.Key);
GC.Collect();
using var categorylogic = unitOfWork.GetLongContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract>();
using var contentlogic = unitOfWork.GetLongContractLogic<ContentEntity, CreateContentRequestContract, UpdateContentRequestContract, ContentContract>();
using var languageLogic = unitOfWork.GetLongContractLogic<LanguageEntity, LanguageContract>();

var getCategoryResult = await categorylogic.GetBy(x => x.Key == request.Key);
if (getCategoryResult.IsSuccess)
return (FailedReasonType.Incorrect, $"{getCategoryResult.Result.Key} category is already exists.");
return getCategoryResult.ToContract<CategoryContract>();

var languages = await _languagelogic.GetAll();
var languages = await languageLogic.GetAll();
var notFoundLanguages = request.LanguageData.Select(x => x.Language).Except(languages.Result.Select(o => o.Name));

if (!notFoundLanguages.Any())
{
var addCategoryResult = await _categorylogic.Add(new CreateCategoryRequestContract
var addCategoryResult = await categorylogic.Add(new CreateCategoryRequestContract
{
Key = request.Key,
});
Expand All @@ -90,80 +96,80 @@ public async Task<MessageContract<CategoryContract>> AddContentWithKey(AddConten
{
var languageId = languages.Result.FirstOrDefault(o => o.Name == item.Language)?.Id;
if (!languageId.HasValue)
return (FailedReasonType.Unknown, "An error has occured!");
return (FailedReasonType.NotFound, $"Language {item.Language} not found!");

var addContentResult = await _contractlogic.Add(new CreateContentRequestContract
var addContentResult = await contentlogic.Add(new CreateContentRequestContract
{
CategoryId = addCategoryResult.Result,
LanguageId = languageId.Value,
Data = item.Data
});
}

var addedCategoryResult = await _categorylogic.GetById(new GetIdRequestContract<long>
var addedCategoryResult = await categorylogic.GetById(new GetIdRequestContract<long>
{
Id = addCategoryResult.Result
});

return addedCategoryResult.Result;
}

return (FailedReasonType.Incorrect, $"This languages are not registered in the content server: {string.Join(", ", notFoundLanguages)}");
}

[HttpPost]
public async Task<MessageContract> UpdateContentWithKey(AddContentWithKeyRequestContract request)
{
var getCategoryResult = await _categorylogic.GetBy(x => x.Key == request.Key, query => query.Include(x => x.Contents).ThenInclude(x => x.Language));
if (getCategoryResult.IsSuccess)
GC.Collect();
using var categorylogic = unitOfWork.GetLongContractLogic<CategoryEntity, CreateCategoryRequestContract, UpdateCategoryRequestContract, CategoryContract>();
using var contentlogic = unitOfWork.GetLongContractLogic<ContentEntity, CreateContentRequestContract, UpdateContentRequestContract, ContentContract>();
using var languageLogic = unitOfWork.GetLongContractLogic<LanguageEntity, LanguageContract>();
var getCategoryResult = await categorylogic.GetBy(x => x.Key == request.Key, query => query.Include(x => x.Contents).ThenInclude(x => x.Language));
if (!getCategoryResult)
return getCategoryResult;
var contents = getCategoryResult.Result.Contents;
foreach (var content in contents)
{
var contents = getCategoryResult.Result.Contents;
foreach (var content in contents)
if (request.LanguageData.Any(o => o.Language == content.Language.Name))
{
if (request.LanguageData.Any(o => o.Language == content.Language.Name))
var response = await contentlogic.Update(new UpdateContentRequestContract
{
var response = await _contractlogic.Update(new UpdateContentRequestContract
{
Id = content.Id,
CategoryId = content.CategoryId,
LanguageId = content.LanguageId,
UniqueIdentity = content.UniqueIdentity,

Data = request.LanguageData.FirstOrDefault(o => o.Language == content.Language.Name).Data
});

if (!response.IsSuccess)
return (FailedReasonType.Unknown, "An error has occured");
}
Id = content.Id,
CategoryId = content.CategoryId,
LanguageId = content.LanguageId,
UniqueIdentity = content.UniqueIdentity,

Data = request.LanguageData.FirstOrDefault(o => o.Language == content.Language.Name).Data
});

if (!response.IsSuccess)
return response.ToContract();
}
}

foreach (var languageData in request.LanguageData)
foreach (var languageData in request.LanguageData)
{
if (!contents.Any(o => o.Language.Name == languageData.Language))
{
if (!contents.Any(o => o.Language.Name == languageData.Language))
{
var language = await _languagelogic.GetBy(o => o.Name == languageData.Language);
var language = await languageLogic.GetBy(o => o.Name == languageData.Language);

if (!language)
continue;
if (!language)
continue;

var response = await _contractlogic.Add(new CreateContentRequestContract
{
CategoryId = getCategoryResult.Result.Id,
LanguageId = language.Result.Id,
UniqueIdentity = language.Result.UniqueIdentity,
var response = await contentlogic.Add(new CreateContentRequestContract
{
CategoryId = getCategoryResult.Result.Id,
LanguageId = language.Result.Id,
UniqueIdentity = language.Result.UniqueIdentity,

Data = languageData.Data
});
Data = languageData.Data
});

if (!response.IsSuccess)
return (FailedReasonType.Unknown, "An error has occured");
}
if (!response)
return response.ToContract();
}

return true;
}

return (FailedReasonType.Incorrect, $"{getCategoryResult.Result.Key} category doesn't exists");
return true;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Cores.AspCoreApi" Version="0.0.0.21" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Loading
Loading