add Base Controller, Complete Documentation and remove unncessary files ...

This commit is contained in:
2026-05-27 16:08:41 +03:30
parent 661a17fb97
commit e7979f8a65
10 changed files with 423 additions and 0 deletions
View File
View File
View File
@@ -0,0 +1,313 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using xCommons.Configurations;
using xCommons.Extensions;
using xCommons.Providers;
using xExceptions.Constants;
using xIdentityService.Controllers;
using xIdentityService.Interfaces;
using xModels.Base;
using xTermsAndConditionsService.Interfaces;
namespace xTermsAndConditionsService.Controllers
{
/// <summary>
/// a Base Controller Implementation for Providing Base Actions of
/// Terms and Conditions Provided Actions ...
/// </summary>
[AllowAnonymous]
public abstract class XTermsAndComditionsControllerBase : XBaseIdentityApiV1Controller, IXTermsAndComditionsController
{
private readonly IXTermsAndComditionsProvider provider;
protected XTermsAndComditionsControllerBase(
ILogger<XTermsAndComditionsControllerBase> logger,
XAppConfiguration appConfiguration,
IXIdentityProvider identityProvider,
IXTermsAndComditionsProvider provider,
XValidationProvider validationProvider
) : base(
logger,
appConfiguration,
identityProvider,
validationProvider
)
{
this.provider = provider;
}
//
#region Actions ...
/// <summary>
/// Retrieved all Exists Languages Terms and Conditions ...
/// </summary>
/// <returns></returns>
[HttpGet("Terms/Languages")]
public virtual async Task<ActionResult<IEnumerable<string>>> TermsLanguages(
CancellationToken cancellationToken = default
)
{
//
try
{
//
var result = await provider.TermsLanguages(
cancellationToken: cancellationToken
);
//
return Ok(result.ToDynamicObject());
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Check Has Terms and Conditions based on Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <returns></returns>
[HttpGet("Terms/{language}/Has")]
public virtual async Task<ActionResult<bool>> HasTerms(
[FromRoute] string language = null,
CancellationToken cancellationToken = default
)
{
//
try
{
//
// Validate ...
if (!ModelState.IsValid)
{
XException.InvalidArgs.Throw();
}
await ValidationProvider
.GroupValidationBuilder()
.AddNotEmpty(language)
.ValidateGroupAsync();
//
var result = await provider.HasTerms(
language: language,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Retrieve Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language">if null, used Default Language ...</param>
/// <returns></returns>
[HttpGet("Terms/{language}")]
public virtual async Task<ActionResult<string>> GetTerms(
[FromRoute] string language = null,
CancellationToken cancellationToken = default
)
{
//
try
{
//
// Validate ...
if (!ModelState.IsValid)
{
XException.InvalidArgs.Throw();
}
await ValidationProvider
.GroupValidationBuilder()
.AddNotEmpty(language)
.ValidateGroupAsync();
//
var result = await provider.GetTerms(
language: language,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Add Terms and Conditions for Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <returns></returns>
[HttpPost("Terms/{language}")]
public virtual async Task<ActionResult<string>> AddTerms(
[FromRoute] string language,
[FromBody] XBaseValueContainer<string> terms,
CancellationToken cancellationToken = default
)
{
//
try
{
//
// Validate ...
if (!ModelState.IsValid)
{
XException.InvalidArgs.Throw();
}
await ValidationProvider
.GroupValidationBuilder()
.AddNotEmpty(language)
.AddNotNull(terms)
.AddNotEmpty(terms.Value)
.ValidateGroupAsync();
//
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var result = await provider.AddTerms(
language: language,
terms: terms.Value,
userInfo: userInfo,
connectionId: connectionId,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Remove Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <returns></returns>
[HttpDelete("Terms/{language}")]
public virtual async Task<ActionResult<bool>> RemoveTerms(
[FromRoute] string language,
CancellationToken cancellationToken = default
)
{
//
try
{
//
// Validate ...
if (!ModelState.IsValid)
{
XException.InvalidArgs.Throw();
}
await ValidationProvider
.GroupValidationBuilder()
.AddNotEmpty(language)
.ValidateGroupAsync();
//
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var result = await provider.RemoveTerms(
language: language,
userInfo: userInfo,
connectionId: connectionId,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
/// <summary>
/// Update Terms and Conditions of Specified Language ...
/// </summary>
/// <param name="language"></param>
/// <param name="terms"></param>
/// <returns></returns>
[HttpPut("Terms/{language}")]
public virtual async Task<ActionResult<bool>> UpdateTerms(
[FromRoute] string language,
[FromBody] XBaseValueContainer<string> terms,
CancellationToken cancellationToken = default
)
{
//
try
{
//
// Validate ...
if (!ModelState.IsValid)
{
XException.InvalidArgs.Throw();
}
await ValidationProvider
.GroupValidationBuilder()
.AddNotEmpty(language)
.AddNotNull(terms)
.AddNotEmpty(terms.Value)
.ValidateGroupAsync();
//
var userInfo = await GetUserInfo();
var connectionId = GetConnectionId();
//
var result = await provider.UpdateTerms(
language: language,
terms: terms.Value,
userInfo: userInfo,
connectionId: connectionId,
cancellationToken: cancellationToken
);
//
return Ok(result);
}
catch (Exception ex)
{
//
var result = GetExceptionActionResult(ex);
return result;
}
}
#endregion
}
}
View File
+9
View File
@@ -6,9 +6,12 @@ using Microsoft.Extensions.DependencyInjection;
using xCommons.Configurations;
using xCommons.Extensions;
using xExceptions.Constants;
using xPushService.DI;
using xPushService.Helpers;
using xStringService.Interfaces.Dtos;
using xTermsAndConditionsService.Configuration;
using xTermsAndConditionsService.Constants;
using xTermsAndConditionsService.Hubs;
using xTermsAndConditionsService.Interfaces;
using xTermsAndConditionsService.Providers;
@@ -123,6 +126,12 @@ namespace xTermsAndConditionsService.DI
var stringProvider = scope.ServiceProvider.GetService<IXStringServiceProvider>();
var serviceConfiguration = scope.ServiceProvider.GetService<XTermsAndConditionsServiceConfiguration>();
//
// Using XTerms Hub ...
var pushHelper = new XPushServiceHelper();
pushHelper.AddHub<XTermsHub>("termsHub");
app.UseXPushService(pushHelper);
//
// Validate Requirements Exists ...
var isValid =
View File
View File
View File
+101
View File
@@ -6,6 +6,107 @@ its depends on:
- **xStringService**
## Providers
in this module all requirements tools for manage Terms and Conditions Integrated and Provided.
- **IXTermsAndComditionsProvider**: an Interface for Describe Provided Actions for Terms and Conditions Management.
- **XTermsAndComditionsProvider**: an Implementation of Provided Actions for Terms and Conditions Management.
- **XTermsHub**: a Hub Implementation for Notifing Terms Changes to Connected Clients.
- **IXTermsAndComditionsController**: an Interface for Describe base Terms and Conditions Management Controller Actions.
- **XTermsAndComditionsControllerBase**: an Abstract Base Implementation of base Terms and Conditions Management Controller Actions.
## Implementation
you have to follow these steps for using this Module.
### Configure Service
all of Requirements for Terms and Conditions must be Configured using appSettings and provided by IConfiguration to Modules.
fo using Service you Have to Configure the Service in you appSettings.json file.
this configurations mapped to **XTermsAndConditionsServiceConfiguration** class.
```C#
/// <summary>
/// Terms and Conditions Configuration ...
/// </summary>
public class XTermsAndConditionsServiceConfiguration
{
/// <summary>
/// Terms And Conditions Resource Title in XString Table ...
/// </summary>
public string ResourceTitle { get; set; } = "terms_and_conditions";
/// <summary>
/// Seeding Mode for Terms and Conditions ...
/// </summary>
public XDbSeedingMode SeedingMode { get; set; } = XDbSeedingMode.AddIfNotExists;
/// <summary>
/// Default Terms and Conditions for Add to Databse on App Startup ...
/// </summary>
public List<XStringDto> TermsAndConditions { get; set; } = null;
}
```
a sample Configuration must like this:
```json
{
...
"TermsAndConditionsService": {
"ResourceTitle": "terms_and_conditions",
"SeedingMode": "AddIfNotExists",
"TermsAndConditions": [
{
"Language": "fa-IR",
"TranslatedValue": "شرایط و ضوابط ثبت نام در سامانه"
}
]
},
...
}
```
### Register Service
after preparation of all requirements, final steps is Register Service.
there are two main step:
- **DI Registration**: in this phase, all Provided Services Registered in DI.
- **Middleware Usage**: in this phase, if any Terms and Conditions Provided in Configuration, Seeded in Data Persist System using Provided Services.
```C#
public class Startup
{
//
public void ConfigureServices(IServiceCollection services)
{
...
//
// Register Terms and Conditions Service ...
services.AddXTermsConditionsService(
lifeTime: lifeTime,
configuration: configuration
);
...
}
//
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
//
// Using Terms and Conditions Service ...
app.UseXTermsAndConditionsService();
...
}
}
```
## Maintainer
Hadi Khazaee asl