fix referencing issues ...
fix seeding issues ... fix SubCategory Refresh and Add Support for handle Category Referesh and Referencing ...
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SQLitePCL;
|
||||
using xCategoryService.Configurations;
|
||||
using xCategoryService.Helpers;
|
||||
using xCategoryService.Interfaces;
|
||||
using xCategoryService.Interfaces.Dtos;
|
||||
@@ -22,6 +27,71 @@ namespace xCategoryService.DI
|
||||
{
|
||||
public static class XDIHelperExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// Extract Service Configurations ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static XCategoryServiceConfiguration GetXCategoryServiceConfigurations(
|
||||
this IConfiguration source
|
||||
)
|
||||
{
|
||||
//
|
||||
XCategoryServiceConfiguration result = null;
|
||||
|
||||
//
|
||||
var section = source.GetSection(Constants.ConfigurationNodeNames.CATEGORY_SERVICE_NODE);
|
||||
var sectionJson = source.GetSectionAsJson(Constants.ConfigurationNodeNames.CATEGORY_SERVICE_NODE);
|
||||
if (!section.IsNull())
|
||||
{
|
||||
result = sectionJson.FromJSON<XCategoryServiceConfiguration>();
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Service Configuration ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXCategoryServiceConfiguration(
|
||||
this IServiceCollection source,
|
||||
IConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
// Extract Configuration Class ...
|
||||
var config = configuration.GetXCategoryServiceConfigurations();
|
||||
|
||||
//
|
||||
// Register Class as Service ...
|
||||
source.AddXCategoryServiceConfiguration(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Service Configuration ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="configuration"></param>
|
||||
public static void AddXCategoryServiceConfiguration(
|
||||
this IServiceCollection source,
|
||||
XCategoryServiceConfiguration configuration
|
||||
)
|
||||
{
|
||||
//
|
||||
// Normalize Configuration Class ...
|
||||
if (configuration.IsNull())
|
||||
{
|
||||
configuration = new XCategoryServiceConfiguration();
|
||||
}
|
||||
|
||||
//
|
||||
// Register ...
|
||||
source.AddSingleton(configuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register Service ...
|
||||
/// </summary>
|
||||
@@ -114,6 +184,109 @@ namespace xCategoryService.DI
|
||||
isDevelopmentEnvironment: isDevelopmentEnvironment
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Create an Scope for Services Acces ...
|
||||
// Handle Seeding Category/SubCategory Resources ...
|
||||
using (var scope = app.ApplicationServices.CreateScope())
|
||||
{
|
||||
//
|
||||
// Retrieve Configuratuions ...
|
||||
var categoryProvider = scope.ServiceProvider.GetService<IXCategoryProvider>();
|
||||
var subCategoryProvider = scope.ServiceProvider.GetService<IXSubCategoryProvider>();
|
||||
var configuration = scope.ServiceProvider.GetService<XCategoryServiceConfiguration>();
|
||||
if (!configuration.IsNullOrDefault() &&
|
||||
!categoryProvider.IsNullOrDefault() &&
|
||||
!subCategoryDescriptor.IsNullOrDefault()
|
||||
)
|
||||
{
|
||||
//
|
||||
// Logging ...
|
||||
Log("Start Seeding ...");
|
||||
|
||||
//
|
||||
// Count Data in Category ...
|
||||
var hasCategory = (categoryProvider.Count().GetAwaiter().GetResult()) > 0;
|
||||
var hasSubCategory = (subCategoryProvider.Count().GetAwaiter().GetResult()) > 0;
|
||||
|
||||
//
|
||||
// Seed Category Resources ...
|
||||
var canSeed =
|
||||
!hasCategory &&
|
||||
configuration.Categories.HasChild();
|
||||
if (canSeed)
|
||||
{
|
||||
//
|
||||
// Loop through Provided items ...
|
||||
foreach (var cat in configuration.Categories)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
Log($"Start Seeding Category: {cat.Titles.Locales.ToArray()[0].Value} ...");
|
||||
|
||||
//
|
||||
categoryProvider.Refresh(
|
||||
resource: cat,
|
||||
userInfo: null,
|
||||
connectionId: null,
|
||||
cancellationToken: default
|
||||
)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
//
|
||||
Log($"Seeding Category: {cat.Titles.Locales.ToArray()[0].Value} Finished Successfully ...");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($"Seeding Category: {cat.Titles.Locales.ToArray()[0].Value} Failed due: {ex.Message} ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Seed SubCategory Resources ...
|
||||
canSeed =
|
||||
!hasSubCategory &&
|
||||
configuration.SubCategories.HasChild();
|
||||
if (canSeed)
|
||||
{
|
||||
//
|
||||
// Loop through Provided items ...
|
||||
foreach (var subcat in configuration.SubCategories)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
Log($"Start Seeding SubCategory: {subcat.Titles.Locales.ToArray()[0].Value} ...");
|
||||
|
||||
//
|
||||
subCategoryProvider.Refresh(
|
||||
userInfo: null,
|
||||
resource: subcat,
|
||||
connectionId: null,
|
||||
cancellationToken: default
|
||||
)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
|
||||
//
|
||||
Log($"Seeding SubCategory: {subcat.Titles.Locales.ToArray()[0].Value} Finished Successfully ...");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log($"Seeding SubCategory: {subcat.Titles.Locales.ToArray()[0].Value} Failed due: {ex.Message} ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
Log("Finish Seeding ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user