fix referencing issues ...
fix seeding issues ... fix SubCategory Refresh and Add Support for handle Category Referesh and Referencing ...
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using xCategoryService.Models.Dtos;
|
||||
|
||||
namespace xCategoryService.Configurations
|
||||
{
|
||||
/// <summary>
|
||||
/// a Configuration Class for Providing Data to Configure XCategoryService ...
|
||||
/// </summary>
|
||||
public class XCategoryServiceConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// a List of Categories for Seeding ...
|
||||
/// </summary>
|
||||
public XCategoryResourceDto[] Categories { get; set; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// a List of SubCategories for Seeding ...
|
||||
/// </summary>
|
||||
public XSubCategoryResourceDto[] SubCategories { get; set; } = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace xCategoryService.Constants
|
||||
{
|
||||
public partial struct ConfigurationNodeNames
|
||||
{
|
||||
public const string CATEGORY_SERVICE_NODE = "CategoryServiceConfiguration";
|
||||
}
|
||||
}
|
||||
@@ -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 ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@@ -73,11 +73,11 @@ namespace xCategoryService.Providers
|
||||
|
||||
//
|
||||
// Retrieve Last Index ...
|
||||
var maxIndex = (await GetReferencingIndexes(
|
||||
var indexes = await GetReferencingIndexes(
|
||||
providedForId: providedForId,
|
||||
cancellationToken: cancellationToken
|
||||
))
|
||||
.Max();
|
||||
);
|
||||
var maxIndex = indexes.HasChild() ? indexes.Max() : -1;
|
||||
if (forIndex > maxIndex)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace xCategoryService.Providers
|
||||
{
|
||||
public class XCategoryServiceSeeder
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -143,17 +143,21 @@ namespace xCategoryService.Providers
|
||||
//
|
||||
// Reading Category Resource ...
|
||||
XCategoryResourceDto categoryResourceDto = null;
|
||||
var categoryDto = await categoryProvider.GetReference(
|
||||
providedForId: id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!categoryDto.IsNullOrDefault())
|
||||
try
|
||||
{
|
||||
categoryResourceDto = await categoryProvider.GetResource(
|
||||
id: categoryDto.Id,
|
||||
var categoryDto = await categoryProvider.GetReference(
|
||||
providedForId: id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
if (!categoryDto.IsNullOrDefault())
|
||||
{
|
||||
categoryResourceDto = await categoryProvider.GetResource(
|
||||
id: categoryDto.Id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
//
|
||||
var result = new XSubCategoryResourceDto
|
||||
@@ -439,7 +443,7 @@ namespace xCategoryService.Providers
|
||||
|
||||
//
|
||||
// Update Category Dto if not ...
|
||||
if (item.Item.Title.IsNullOrEmpty() ||
|
||||
if (item.Item.Title.IsNullOrEmpty() ||
|
||||
!item.Item.Title
|
||||
.Equals(
|
||||
titleResource.Resource,
|
||||
@@ -671,6 +675,17 @@ namespace xCategoryService.Providers
|
||||
isValid = true;
|
||||
}
|
||||
|
||||
//
|
||||
// Remove all Referencings ...
|
||||
if (isValid)
|
||||
{
|
||||
await categoryProvider.RemoveReferences(
|
||||
providedForId: id,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
if (isValid)
|
||||
{
|
||||
@@ -1123,6 +1138,42 @@ namespace xCategoryService.Providers
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Handle Category ...
|
||||
if (
|
||||
!isDelete &&
|
||||
(isNew || isExists) &&
|
||||
!resource.Category.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
// Refresh Category ...
|
||||
var category = await categoryProvider.Refresh(
|
||||
userInfo: userInfo,
|
||||
resource: resource.Category,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
// Handle SubCategory Referencings ...
|
||||
var references = await categoryProvider.GetAllReferences(
|
||||
providedForId: id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
var hasReference =
|
||||
references.HasChild() &&
|
||||
references.Any(r => r.Id == category.Item.Id);
|
||||
if (!hasReference)
|
||||
{
|
||||
await categoryProvider.AddReference(
|
||||
providedForId: id,
|
||||
model: category.Item,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Validate Action ...
|
||||
isValid = id.IsValidIntId();
|
||||
@@ -1162,6 +1213,7 @@ namespace xCategoryService.Providers
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get All Resources as Async Enumerable ...
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user