last ...
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
using System.Linq;
|
||||
using xCategoryService.Providers.Dtos;
|
||||
using xCommons.Extensions;
|
||||
|
||||
namespace xCategoryService.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions Method Related to XCategoryResourceDto ...
|
||||
/// </summary>
|
||||
public static class XCategoryResourceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Check a Resource is Valid or not ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValid(
|
||||
this XCategoryResourceDto source
|
||||
)
|
||||
{
|
||||
//
|
||||
return
|
||||
!source.IsNullOrDefault() &&
|
||||
!source.Item.IsNullOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check a Resource is a New Resource for Add or not ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNew(
|
||||
this XCategoryResourceDto source
|
||||
)
|
||||
{
|
||||
//
|
||||
return
|
||||
source.IsValid() &&
|
||||
!source.Item.Id.IsValidIntId();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extract Available Languages ...
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
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 =
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,40 @@ namespace xCategoryService.Interfaces
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Update a Category Item for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XCategoryResourceDto> Update(
|
||||
int id,
|
||||
XCategoryDto item,
|
||||
string language = null,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Referesh a Resource ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<XCategoryResourceDto> RefereshResource(
|
||||
XCategoryResourceDto resource,
|
||||
string connectionId = null,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Referesh a Resource ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="connectionId"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<XCategoryResourceDto> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user