Files
xContentService/Controllers/XControllerProviderControllerBase.cs
2026-09-06 00:18:57 +03:30

113 lines
3.6 KiB
C#

using System;
using System.Linq;
using System.Threading;
using xCommons.Providers;
using xCommons.Extensions;
using xExceptions.Constants;
using System.Threading.Tasks;
using xCommons.Configurations;
using xTagService.Controllers;
using Microsoft.AspNetCore.Mvc;
using xContentService.Interfaces;
using xContentService.Models.Dtos;
using xIdentityService.Interfaces;
using Microsoft.Extensions.Logging;
using xContentService.Providers.Dtos;
using xContentService.Models.Entities;
using Microsoft.EntityFrameworkCore.Query;
using xContentService.Interfaces.Controllers;
namespace xContentService.Controllers
{
public abstract class XContentProviderControllerBase : XBaseProvidedHasTagControllerBase<XContent, XContentDto, Guid, XContentDtoHub>, IXContentProviderController
{
private readonly IXContentProvider provider;
protected XContentProviderControllerBase(
ILogger<XBaseProvidedHasTagControllerBase<XContent, XContentDto, Guid, XContentDtoHub>> logger,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
XValidationProvider validationProvider,
IXContentProvider provider,
IXContentTagProvided tagProvided,
Func<IQueryable<XContent>, IOrderedQueryable<XContent>> defaultOrderBuilder = null,
Func<IQueryable<XContent>, IIncludableQueryable<XContent, object>> defaultIncludeBuilder = null
) : base(
logger,
appConfiguration,
identityProvider,
validationProvider,
provider,
tagProvided,
defaultOrderBuilder,
defaultIncludeBuilder
)
{
this.provider = provider;
}
/// <summary>
/// Add an item ...
/// </summary>
/// <param name="item"></param>
/// <param name="language"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost("Add")]
public async Task<ActionResult<XContentDto>> Add(
[FromBody] XContentDto item,
[FromQuery] string language = null,
CancellationToken cancellationToken = default
)
{
//
try
{
//
// Validate ...
if (!ModelState.IsValid)
{
XException.InvalidArgs.Throw();
}
await ValidationProvider
.GroupValidationBuilder()
.AddNotNull(item)
.AddNotEmpty(item.Title)
.ValidateGroupAsync();
//
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
// Normalize Language ...
if (language.IsNullOrEmpty())
{
language = AppConfiguration.DefaultLanguage;
}
if (language.IsNullOrEmpty())
{
XException.InvalidData.Throw();
}
//
var result = await provider.Add(
item: item,
userInfo: userInfo,
language: language,
connectionId: connectionId,
cancellationToken: cancellationToken
);
//
return result;
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
}
}