233 lines
7.2 KiB
C#
233 lines
7.2 KiB
C#
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 xPushService.Providers;
|
|
|
|
namespace xCategoryService.Providers
|
|
{
|
|
public class XCategoryProvider : XBaseProvided<XCategory, XCategoryDto, int, XCategoryDtoHub>, 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load an Item by all of it's Related Translations ...
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XCategoryResourceDto> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add Category Item for Specified Language ...
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <param name="langauge"></param>
|
|
/// <param name="connectionId"></param>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XCategoryResourceDto> 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 titleResourceTitle = await titleResourceProvider.AddOrUpdateItemResourcer(
|
|
identifier: id,
|
|
language: langauge,
|
|
translation: title,
|
|
userInfo: userInfo,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
var descriptionResourceTitle = await descriptionResourceProvider.AddOrUpdateItemResourcer(
|
|
identifier: id,
|
|
language: langauge,
|
|
userInfo: userInfo,
|
|
translation: description,
|
|
connectionId: connectionId,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
|
|
//
|
|
// Update Category Dto ...
|
|
item.Title = titleResourceTitle;
|
|
item.Description = descriptionResourceTitle;
|
|
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<bool> RemoveResaource(
|
|
int id,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
// Validate ...
|
|
var isValid = id.IsValidIntId();
|
|
if (!isValid)
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
|
|
//
|
|
// Retrieve Resource ...
|
|
var resource = await GetResource(
|
|
id: id,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
isValid = !resource.IsNullOrDefault();
|
|
if (!isValid)
|
|
{
|
|
XException.NotFound.Throw();
|
|
}
|
|
|
|
//
|
|
await titleResourceProvider.RemoveResource();
|
|
}
|
|
}
|
|
} |