using System.Threading; using System.Threading.Tasks; using xCategoryService.Interfaces; using xCategoryService.Interfaces.Dtos; using xCategoryService.Models.Dtos; using xCategoryService.Models.Entities; using xCategoryService.Providers.Dtos; using xCommons.Configurations; using xCommons.Extensions; using xExceptions.Constants; using xIdentityModels.Models; using xModels.Dtos; using xPushService.Providers; namespace xCategoryService.Providers { public class XCategoryProvider : XBaseProvided, IXCategoryProvider { private readonly IXCategoryServiceProvider provider; private readonly XAppConfiguration appConfiguration; private readonly IXCategoryTitleResourceProvider titleResourceProvider; private readonly IXCategoryDescriptionResourceProvider descriptionResourceProvider; public XCategoryProvider( XAppConfiguration appConfiguration, IXCategoryServiceProvider provider, IXCategoryTitleResourceProvider titleResourceProvider, IXCategoryDescriptionResourceProvider descriptionResourceProvider ) : base( provider, permittedRoles: new string[] { } ) { // this.provider = provider; this.appConfiguration = appConfiguration; this.titleResourceProvider = titleResourceProvider; this.descriptionResourceProvider = descriptionResourceProvider; } public override bool IsOwned( XCategoryDto item, XUserClaimsInfoDto userInfo ) { return !item.IsNullOrDefault(); } public override bool IsOwned( XCategory item, XUserClaimsInfoDto userInfo ) { return !item.IsNullOrDefault(); } /// /// Load an Item by all of it's Related Translations ... /// /// /// /// public async Task GetResource( int id, CancellationToken cancellationToken = default ) { // var category = await Get( id: id, includeBuilder: null, cancellationToken: cancellationToken ); var titleResource = await titleResourceProvider.GetResource( identifier: id, cancellationToken: cancellationToken ); var descriptionResource = await titleResourceProvider.GetResource( identifier: id, cancellationToken: cancellationToken ); // var result = new XCategoryResourceDto { Item = category, Titles = titleResource, Descriptions = descriptionResource }; // return result; } /// /// Add Category Item for Specified Language ... /// /// /// /// /// /// /// public async Task Add( XCategoryDto item, string langauge = null, string connectionId = null, XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ) { // // Normalize ... langauge = !langauge.IsNullOrEmpty() ? langauge : appConfiguration.DefaultLanguage; // // Validate ... var isValid = !item.IsNullOrDefault(); if (!isValid) { XException.InvalidArgs.Throw(); } // var id = -1; try { // var title = item.Title; var description = item.Description; // // Add Item Pure for ID Generating ... item = await base.Add( item: item, userInfo: userInfo, connectionId: null, // Since here We dont want notification ... cancellationToken: cancellationToken ); isValid = !item.IsNullOrDefault(); if (!isValid) { XException.ActionFailed.Throw(); } // id = item.Id; var titleResource = await titleResourceProvider .AddOrUpdateLocale( identifier: id, locale: new XLocaleResourceDto { Value = title, Language = langauge }, connectionId: connectionId, cancellationToken: cancellationToken ); var descriptionResource = await descriptionResourceProvider .AddOrUpdateLocale( identifier: id, locale: new XLocaleResourceDto { Value = description, Language = langauge }, connectionId: connectionId, cancellationToken: cancellationToken ); // // Update Category Dto ... item.Title = titleResource.Resource; item.Description = descriptionResource.Resource; item = await base.Update( id: id, item: item, userInfo: userInfo, connectionId: connectionId, cancellationToken: cancellationToken ); } catch { XException.InvalidArgs.Throw(); } // // Validate Id ... isValid = id.IsValidIntId(); if (!isValid) { XException.ActionFailed.Throw(); } // // Retrieve Category Resourv=ce ... var result = await GetResource( id: id, cancellationToken: cancellationToken ); // return result; } public async Task Update( int id, XCategoryDto item, string langauge = null, string connectionId = null, XUserClaimsInfoDto userInfo = null, CancellationToken cancellationToken = default ) {} } }