diff --git a/Extensions/XCategoryResourceExtensions.cs b/Extensions/XCategoryResourceExtensions.cs new file mode 100644 index 0000000..5f65e44 --- /dev/null +++ b/Extensions/XCategoryResourceExtensions.cs @@ -0,0 +1,79 @@ +using System.Linq; +using xCategoryService.Providers.Dtos; +using xCommons.Extensions; + +namespace xCategoryService.Extensions +{ + /// + /// Extensions Method Related to XCategoryResourceDto ... + /// + public static class XCategoryResourceExtensions + { + /// + /// Check a Resource is Valid or not ... + /// + /// + /// + public static bool IsValid( + this XCategoryResourceDto source + ) + { + // + return + !source.IsNullOrDefault() && + !source.Item.IsNullOrDefault(); + } + + /// + /// Check a Resource is a New Resource for Add or not ... + /// + /// + /// + public static bool IsNew( + this XCategoryResourceDto source + ) + { + // + return + source.IsValid() && + !source.Item.Id.IsValidIntId(); + } + + /// + /// Extract Available Languages ... + /// + /// + /// + public static string[] ExtractLanguages( + this XCategoryResourceDto source + ) + { + // + var result = new string[] { }; + + // + // Validate ... + if (!source.IsValid()) + { + return result; + } + + // + result = + source.Titles.Locales.Select(x => x.Language) + .Union(source.Descriptions.Locales.Select(x => x.Language)) + .Distinct() + .ToArray(); + + // + return result; + } + + public static bool ValidateResources( + this XCategoryResourceDto source + ) + { + var result = + } + } +} \ No newline at end of file diff --git a/Interfaces/IXCategoryProvider.cs b/Interfaces/IXCategoryProvider.cs index b27044c..6d61a12 100644 --- a/Interfaces/IXCategoryProvider.cs +++ b/Interfaces/IXCategoryProvider.cs @@ -36,6 +36,40 @@ namespace xCategoryService.Interfaces string connectionId = null, XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default - ); + ); + + /// + /// Update a Category Item for Specified Language ... + /// + /// + /// + /// + /// + /// + /// + /// + Task Update( + int id, + XCategoryDto item, + string language = null, + string connectionId = null, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); + + /// + /// Referesh a Resource ... + /// + /// + /// + /// + /// + /// + Task RefereshResource( + XCategoryResourceDto resource, + string connectionId = null, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ); } } \ No newline at end of file diff --git a/Providers/XCategoryProvider.cs b/Providers/XCategoryProvider.cs index 48afff3..c2d14e5 100644 --- a/Providers/XCategoryProvider.cs +++ b/Providers/XCategoryProvider.cs @@ -319,6 +319,32 @@ namespace xCategoryService.Providers language: language, cancellationToken: cancellationToken ); + if (!isValid) + { + XException.NotFound.Throw(); + } + + // + // Retrieve Category Dto for Validate Owining ... + var dto = await Get( + id: id, + includeBuilder: null, + cancellationToken: cancellationToken + ); + + // + // Check Permission ... + isValid = + !dto.IsNullOrDefault() && + !userInfo.IsNullOrDefault() && + IsOwned( + item: dto, + userInfo: userInfo + ); + if (!isValid) + { + XException.NotAllowed.Throw(); + } // // Retrieve Exists Item Resource ... @@ -328,8 +354,132 @@ namespace xCategoryService.Providers ); // - // Here we can Use Exists Title and Description for Access Resource Titles ... + var titleUpdated = false; + var descriptionUpdated = false; + // + // Update Title ... + resource.Titles.Locales + .ToList() + .ForEach(tl => + { + // + if (tl.Language.Equals(language, System.StringComparison.InvariantCultureIgnoreCase)) + { + // + titleUpdated = true; + tl.Value = item.Title; + } + }); + + // + // Update Description ... + resource.Descriptions.Locales + .ToList() + .ForEach(dl => + { + // + if (dl.Language.Equals(language, System.StringComparison.InvariantCultureIgnoreCase)) + { + // + descriptionUpdated = true; + dl.Value = item.Description; + } + }); + + // + // Validate Title and Description Updated ... + isValid = titleUpdated && descriptionUpdated; + if (!isValid) + { + XException.ActionFailed.Throw(); + } + + // + resource = await RefereshResource( + resource: resource, + userInfo: userInfo, + connectionId: connectionId, + cancellationToken: cancellationToken + ); + + // + return resource; + } + + /// + /// Referesh a Resource ... + /// + /// + /// + /// + /// + /// + public async Task RefereshResource( + XCategoryResourceDto resource, + string connectionId = null, + XUserClaimsInfoDto userInfo = null, + CancellationToken cancellationToken = default + ) + { + // + // Validate Args ... + var isValid = + !resource.IsNull() && + !resource.Item.IsNullOrDefault(); + if (!isValid) + { + XException.InvalidArgs.Throw(); + } + + // + var id = -1; + + // + // Check Item Exists ... + XCategoryDto dto = + !resource.Item.Id.IsValidIntId() + ? null + : await Get( + id: resource.Item.Id, + includeBuilder: null, + cancellationToken: cancellationToken + ); + var isExists = !dto.IsNullOrDefault(); + + // + // Do Referesh Based on Existance ... + var isNew = + !isExists && + !resource.Item.Id.IsValidIntId(); + if (isNew) + { + // + // Add ... + } + else if (isExists) + { + // + // Re State ... + id = resource.Item.Id; + } + + // + isValid = id.IsValidIntId(); + if (!isValid) + { + XException.ActionFailed.Throw(); + } + + // + // Get Result ... + var result = await GetResource( + id: id, + cancellationToken: cancellationToken + ); + + // + return result; } } } \ No newline at end of file