Complete Category and SubCategory Base Actions, and also ControllersBase and their Implemented Usages ...
This commit is contained in:
@@ -0,0 +1,470 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using xCategoryService.Interfaces;
|
||||
using xCategoryService.Models.Dtos;
|
||||
using xCategoryService.Models.Entities;
|
||||
using xCategoryService.Providers.Dtos;
|
||||
using xCommons.Configurations;
|
||||
using xCommons.Extensions;
|
||||
using xCommons.Providers;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
using xModels.Dtos;
|
||||
using xPushService.Controllers;
|
||||
|
||||
namespace xCategoryService.Controllers
|
||||
{
|
||||
public abstract class XSubCategoryProviderControllerBase : XBaseProvidedController<XSubCategory, XSubCategoryDto, int, XSubCategoryDtoHub>, IXSubCategoryProviderController
|
||||
{
|
||||
private readonly IXSubCategoryProvider provider;
|
||||
|
||||
protected XSubCategoryProviderControllerBase(
|
||||
ILogger<XSubCategoryProviderControllerBase> logger,
|
||||
XAppConfiguration appConfiguration,
|
||||
IXIdentityProvider identityProvider,
|
||||
XValidationProvider validationProvider,
|
||||
IXSubCategoryProvider provider,
|
||||
Func<IQueryable<XSubCategory>, IOrderedQueryable<XSubCategory>> defaultOrderBuilder = null,
|
||||
Func<IQueryable<XSubCategory>, IIncludableQueryable<XSubCategory, object>> defaultIncludeBuilder = null
|
||||
) : base(
|
||||
logger,
|
||||
appConfiguration,
|
||||
identityProvider,
|
||||
validationProvider,
|
||||
provider,
|
||||
defaultOrderBuilder,
|
||||
defaultIncludeBuilder
|
||||
)
|
||||
{
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
//
|
||||
#region Actions ...
|
||||
/// <summary>
|
||||
/// Check Has Specified Locale or not ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Resources/{id}/HasLocale")]
|
||||
public virtual async Task<ActionResult<bool>> HasLocale(
|
||||
[FromRoute] int id,
|
||||
[FromQuery] string language,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid ||
|
||||
!id.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
ValidationProvider.NotEmpty(language);
|
||||
|
||||
//
|
||||
var result = await provider.HasLocale(
|
||||
id: id,
|
||||
language: language,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load an Item by all of it's Related Translations ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Resources/{id}")]
|
||||
public virtual async Task<ActionResult<XSubCategoryResourceDto>> GetResource(
|
||||
[FromRoute] int id,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid ||
|
||||
!id.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await provider.GetResource(
|
||||
id: id,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add Category Item for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("Resources")]
|
||||
public virtual async Task<ActionResult<XSubCategoryResourceDto>> Add(
|
||||
[FromBody] XSubCategoryDto item,
|
||||
[FromQuery] string language = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(item)
|
||||
.AddNotEmpty(language)
|
||||
.AddNotEmpty(item.Title)
|
||||
.AddNotEmpty(item.Description)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
// Reading Requirements ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await provider.Add(
|
||||
item: item,
|
||||
language: language,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update a Category Item for Specified Language ...
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("Resources/{id}")]
|
||||
public virtual async Task<ActionResult<XSubCategoryResourceDto>> Update(
|
||||
[FromRoute] int id,
|
||||
[FromBody] XSubCategoryDto item,
|
||||
[FromQuery] string language = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid ||
|
||||
!id.IsValidIntId())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(item)
|
||||
.AddNotEmpty(language)
|
||||
.AddNotEmpty(item.Title)
|
||||
.AddNotEmpty(item.Description)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
// Reading Requirements ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await provider.Update(
|
||||
id: id,
|
||||
item: item,
|
||||
language: language,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Refresh a Resource ...
|
||||
/// </summary>
|
||||
/// <param name="resource"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("Resources/Refresh")]
|
||||
public virtual async Task<ActionResult<XSubCategoryResourceDto>> Refresh(
|
||||
[FromBody] XSubCategoryResourceDto resource,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(resource)
|
||||
.AddNotNull(resource.Item)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
// Reading Requirements ...
|
||||
var userInfo = await GetUserInfo();
|
||||
var connectionId = GetConnectionId();
|
||||
|
||||
//
|
||||
var result = await provider.Refresh(
|
||||
resource: resource,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get All Resources ...
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Resources/All")]
|
||||
public virtual async Task<ActionResult<IEnumerable<XSubCategoryResourceDto>>> GetAllResources(
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
var result = await provider.GetAllResources(
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find One Resource ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Resources/Find/One")]
|
||||
public virtual async Task<ActionResult<XSubCategoryResourceDto>> FindOneResource(
|
||||
[FromQuery] string query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotEmpty(query)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var result = await provider.FindOneResource(
|
||||
cancellationToken: cancellationToken,
|
||||
predicate: x => x.PropValuesContains(query)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find Many Resource ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Resources/Find/Many")]
|
||||
public virtual async Task<ActionResult<IEnumerable<XSubCategoryResourceDto>>> FindManyResource(
|
||||
[FromQuery] string query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotEmpty(query)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var result = await provider.FindManyResource(
|
||||
cancellationToken: cancellationToken,
|
||||
predicate: x => x.PropValuesContains(query),
|
||||
orderBuilder: x => x.OrderBy(y => y.Item.Id)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve Resources by Query Pattern ...
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("Resources/Query")]
|
||||
public virtual async Task<ActionResult<XQueryResult<XSubCategoryResourceDto>>> QueryResources(
|
||||
[FromQuery] XQuery query = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
try
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
await ValidationProvider
|
||||
.GroupValidationBuilder()
|
||||
.AddNotNull(query)
|
||||
.ValidateGroupAsync();
|
||||
|
||||
//
|
||||
var result = await provider.QueryResources(
|
||||
query: query,
|
||||
predicate: null,
|
||||
cancellationToken: cancellationToken,
|
||||
orderBuilder: x => x.OrderBy(y => y.Item.Id)
|
||||
);
|
||||
|
||||
//
|
||||
return Ok(result.ToDynamicObject());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//
|
||||
var result = GetExceptionActionResult(ex);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user