diff --git a/Core.Thalos.DAL.API/Controllers/ModuleController.cs b/Core.Thalos.DAL.API/Controllers/ModuleController.cs index e2b829d..b9e9678 100644 --- a/Core.Thalos.DAL.API/Controllers/ModuleController.cs +++ b/Core.Thalos.DAL.API/Controllers/ModuleController.cs @@ -9,14 +9,13 @@ using Core.Thalos.BuildingBlocks; using Core.Thalos.Provider.Contracts; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Graph; using ModuleRequest = Core.Thalos.Domain.Contexts.Onboarding.Request.ModuleRequest; using StatusEnum = Core.Blueprint.Mongo.StatusEnum; namespace LSA.Core.Thalos.API.Controllers { /// - /// Handles all requests for module authentication. + /// Handles all requests for module management. /// [ApiVersion(MimeTypes.ApplicationVersion)] [Route("api/v{api-version:apiVersion}/[controller]")] @@ -27,71 +26,47 @@ namespace LSA.Core.Thalos.API.Controllers public class ModuleController(IModuleProvider service) : ControllerBase { /// - /// Gets all the modules. + /// Gets all modules. /// - /// The found entities. - /// The roles found. - /// The roles not found error. - /// The service internal error. [HttpGet] - [Consumes(MimeTypes.ApplicationJson)] - [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [Permission("ModuleManagement.Read, RoleManagement.Read")] public async Task GetAllModulesAsync(CancellationToken cancellationToken) { - var result = await service.GetAllModules(cancellationToken).ConfigureAwait(false); + var result = await service.GetAllModules(cancellationToken); return Ok(result); } /// /// Gets all the modules by module identifiers. /// - /// The list of module identifiers. - /// The found entities. - /// The modules found. - /// The modules not found error. - /// The service internal error. [HttpPost] [Route(Routes.GetModuleList)] - [Consumes(MimeTypes.ApplicationJson)] - [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [Permission("ModuleManagement.Read")] public async Task GetAllModulesByList([FromBody] string[] modules, CancellationToken cancellationToken) { if (modules == null || !modules.Any()) - { return BadRequest("Module identifiers are required."); - } - var result = await service.GetAllModulesByList(modules, cancellationToken).ConfigureAwait(false); + var result = await service.GetAllModulesByList(modules, cancellationToken); return Ok(result); } - /// - /// Gets the module by identifier. + /// Gets the module by mongo identifier. /// - /// The module identifier. - /// The found entity. - /// The module found. - /// The module not found error. - /// The service internal error. [HttpGet] [Route(Routes.Id)] - [Consumes(MimeTypes.ApplicationJson)] - [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] [Permission("ModuleManagement.Read")] - public async Task GetModuleByIdAsync([FromRoute] string id, CancellationToken cancellationToken) + public async Task GetModuleByIdAsync([FromRoute] string _id, CancellationToken cancellationToken) { - var result = await service.GetModuleById(id, cancellationToken).ConfigureAwait(false); + var result = await service.GetModuleById(_id, cancellationToken); if (result == null) - { return NotFound("Entity not found"); - } return Ok(result); } @@ -99,67 +74,67 @@ namespace LSA.Core.Thalos.API.Controllers /// /// Creates a new module. /// - /// The module to be added. - /// The created entity. - /// The module created. - /// The module could not be created. - /// The service internal e|ror. [HttpPost] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] [Permission("ModuleManagement.Write")] public async Task CreateModuleAsync([FromBody] ModuleRequest newModule, CancellationToken cancellationToken) { - var result = await service.CreateModule(newModule, cancellationToken).ConfigureAwait(false); + var result = await service.CreateModule(newModule, cancellationToken); return Created("CreatedWithIdAsync", result); } /// - /// Updates a full module by identifier. + /// Updates a full module by mongo identifier. /// - /// The module to update. - /// The module identifier. - /// The updated entity. - /// The module updated. - /// The module not found. - /// The module could not be updated. - /// The service internal error. [HttpPut] [Route(Routes.Id)] - [Consumes(MimeTypes.ApplicationJson)] - [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] [Permission("ModuleManagement.Write")] - public async Task UpdateModuleAsync([FromRoute] string id, ModuleAdapter entity, CancellationToken cancellationToken) + public async Task UpdateModuleAsync([FromRoute] string _id, [FromBody] ModuleAdapter entity, CancellationToken cancellationToken) { - if (id != entity.Id?.ToString()) - { + if (_id != entity._Id) return BadRequest("Module ID mismatch"); - } - - var result = await service.UpdateModule(entity, cancellationToken).ConfigureAwait(false); + var result = await service.UpdateModule(entity, cancellationToken); return Ok(result); } /// /// Changes the status of the module. /// - /// The module identifier. - /// The new status of the module. - /// The updated entity. - /// The module updates. - /// The module not found. - /// The module could not be deleted. - /// The service internal error. [HttpPatch] [Route(Routes.ChangeStatus)] - [Consumes(MimeTypes.ApplicationJson)] - [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] [Permission("ModuleManagement.Write")] - public async Task ChangeModuleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) + public async Task ChangeModuleStatus([FromRoute] string _id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) { - var result = await service.ChangeModuleStatus(id, newStatus, cancellationToken).ConfigureAwait(false); + var result = await service.ChangeModuleStatus(_id, newStatus, cancellationToken); + return Ok(result); + } + + /// + /// Deletes a module by mongo identifier. + /// + /// The module Mongo mongo identifier. + /// Cancellation token for async operation. + /// The deleted if found. + /// The module was successfully deleted. + /// The module was not found. + /// Internal server error. + [HttpDelete] + [Route(Routes.Id)] + [ProducesResponseType(typeof(ModuleAdapter), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [Permission("ModuleManagement.Write")] + public async Task DeleteModuleAsync([FromRoute] string _id, CancellationToken cancellationToken) + { + var result = await service.DeleteModule(_id, cancellationToken); + if (result == null) + return NotFound("Entity not found"); + return Ok(result); } } diff --git a/Core.Thalos.DAL.API/Controllers/PermissionController.cs b/Core.Thalos.DAL.API/Controllers/PermissionController.cs index 5962382..ff3372b 100644 --- a/Core.Thalos.DAL.API/Controllers/PermissionController.cs +++ b/Core.Thalos.DAL.API/Controllers/PermissionController.cs @@ -27,11 +27,11 @@ namespace LSA.Core.Thalos.API.Controllers public class PermissionController(IPermissionProvider service) : ControllerBase { /// - /// Gets all the permissions. + /// Gets all permissions. /// + /// A token to cancel the asynchronous operation. /// The found entities. - /// The roles found. - /// The roles not found error. + /// The permissions found. /// The service internal error. [HttpGet] [Consumes(MimeTypes.ApplicationJson)] @@ -48,9 +48,10 @@ namespace LSA.Core.Thalos.API.Controllers /// Gets all the permissions by permission identifiers. /// /// The list of permission identifiers. + /// A token to cancel the asynchronous operation. /// The found entities. /// The permissions found. - /// The permissions not found error. + /// Bad request if list is null or empty. /// The service internal error. [HttpPost] [Route(Routes.GetPermissionList)] @@ -70,12 +71,13 @@ namespace LSA.Core.Thalos.API.Controllers } /// - /// Gets the permission by identifier. + /// Gets the permission by mongo identifier. /// - /// The permission identifier. + /// The permission mongo identifier. + /// A token to cancel the asynchronous operation. /// The found entity. /// The permission found. - /// The permission not found error. + /// The permission not found. /// The service internal error. [HttpGet] [Route(Routes.Id)] @@ -83,9 +85,9 @@ namespace LSA.Core.Thalos.API.Controllers [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] [Permission("PermissionManagement.Read")] - public async Task GetPermissionByIdAsync([FromRoute] string id, CancellationToken cancellationToken) + public async Task GetPermissionByIdAsync([FromRoute] string _id, CancellationToken cancellationToken) { - var result = await service.GetPermissionById(id, cancellationToken).ConfigureAwait(false); + var result = await service.GetPermissionById(_id, cancellationToken).ConfigureAwait(false); if (result == null) { @@ -99,10 +101,11 @@ namespace LSA.Core.Thalos.API.Controllers /// Creates a new permission. /// /// The permission to be added. + /// A token to cancel the asynchronous operation. /// The created entity. /// The permission created. /// The permission could not be created. - /// The service internal e|ror. + /// Internal server error. [HttpPost] [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status201Created)] [Permission("PermissionManagement.Write")] @@ -113,24 +116,25 @@ namespace LSA.Core.Thalos.API.Controllers } /// - /// Updates a full permission by identifier. + /// Updates a full permission by mongo identifier. /// + /// The permission mongo identifier. /// The permission to update. - /// The permission identifier. + /// A token to cancel the asynchronous operation. /// The updated entity. /// The permission updated. - /// The permission not found. + /// Bad request if ID mismatch. /// The permission could not be updated. - /// The service internal error. + /// Internal server error. [HttpPut] [Route(Routes.Id)] [Consumes(MimeTypes.ApplicationJson)] [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] [Permission("PermissionManagement.Write")] - public async Task UpdatePermissionAsync([FromRoute] string id, PermissionAdapter entity, CancellationToken cancellationToken) + public async Task UpdatePermissionAsync([FromRoute] string _id, [FromBody] PermissionAdapter entity, CancellationToken cancellationToken) { - if (id != entity.Id?.ToString()) + if (_id != entity._Id) { return BadRequest("Permission ID mismatch"); } @@ -143,22 +147,44 @@ namespace LSA.Core.Thalos.API.Controllers /// /// Changes the status of the permission. /// - /// The permission identifier. + /// The permission mongo identifier. /// The new status of the permission. + /// A token to cancel the asynchronous operation. /// The updated entity. - /// The permission updates. - /// The permission not found. - /// The permission could not be deleted. - /// The service internal error. + /// The permission status was updated. + /// Internal server error. [HttpPatch] [Route(Routes.ChangeStatus)] [Consumes(MimeTypes.ApplicationJson)] [Produces(MimeTypes.ApplicationJson)] [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] [Permission("PermissionManagement.Write")] - public async Task ChangePermissionStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) + public async Task ChangePermissionStatus([FromRoute] string _id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) { - var result = await service.ChangePermissionStatus(id, newStatus, cancellationToken).ConfigureAwait(false); + var result = await service.ChangePermissionStatus(_id, newStatus, cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Deletes a permission by its mongo identifier. + /// + /// The permission mongo identifier. + /// A token to cancel the asynchronous operation. + /// The deleted entity. + /// The permission was successfully deleted. + /// The permission was not found. + /// Internal server error occurred. + [HttpDelete] + [Route(Routes.Id)] + [ProducesResponseType(typeof(PermissionAdapter), StatusCodes.Status200OK)] + [Permission("PermissionManagement.Write")] + public async Task DeletePermissionAsync([FromRoute] string _id, CancellationToken cancellationToken) + { + var result = await service.DeletePermission(_id, cancellationToken).ConfigureAwait(false); + + if (result is null) + return NotFound("Permission not found."); + return Ok(result); } } diff --git a/Core.Thalos.DAL.API/Controllers/RoleController.cs b/Core.Thalos.DAL.API/Controllers/RoleController.cs index dee539a..2955977 100644 --- a/Core.Thalos.DAL.API/Controllers/RoleController.cs +++ b/Core.Thalos.DAL.API/Controllers/RoleController.cs @@ -3,6 +3,7 @@ // AgileWebs // // *********************************************************************** + using Asp.Versioning; using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; @@ -25,11 +26,11 @@ namespace LSA.Core.Thalos.API.Controllers public class RoleController(IRoleProvider service) : ControllerBase { /// - /// Gets all the roles. + /// Gets all roles. /// - /// The rol found entities. + /// A token to cancel the asynchronous operation. + /// The found entities. /// The roles found. - /// The roles not found error. /// The service internal error. [HttpGet] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] @@ -41,20 +42,21 @@ namespace LSA.Core.Thalos.API.Controllers } /// - /// Gets the role by identifier. + /// Gets the role by mongo identifier. /// - /// The role identifier. + /// The role mongo identifier. + /// A token to cancel the asynchronous operation. /// The found entity. /// The role found. - /// The role not found error. + /// The role not found. /// The service internal error. [HttpGet] [Route(Routes.Id)] [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] [Permission("RoleManagement.Read")] - public async Task GetRoleByIdAsync([FromRoute] string id, CancellationToken cancellationToken) + public async Task GetRoleByIdAsync([FromRoute] string _id, CancellationToken cancellationToken) { - var result = await service.GetRoleById(id, cancellationToken).ConfigureAwait(false); + var result = await service.GetRoleById(_id, cancellationToken).ConfigureAwait(false); if (result == null) { @@ -68,6 +70,7 @@ namespace LSA.Core.Thalos.API.Controllers /// Creates a new role. /// /// The role to be added. + /// A token to cancel the asynchronous operation. /// The created entity. /// The role created. /// The role could not be created. @@ -82,60 +85,58 @@ namespace LSA.Core.Thalos.API.Controllers } /// - /// Updates a full role by identifier. + /// Updates a full role by mongo identifier. /// + /// The role mongo identifier. /// The role to update. - /// The role identifier. + /// A token to cancel the asynchronous operation. /// The updated entity. /// The role updated. - /// The role not found. + /// Bad request if role ID mismatches. /// The role could not be updated. /// The service internal error. [HttpPut] [Route(Routes.Id)] [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] [Permission("RoleManagement.Write")] - public async Task UpdateRoleAsync([FromRoute] string id, [FromBody] RoleAdapter entity, CancellationToken cancellationToken) + public async Task UpdateRoleAsync([FromRoute] string _id, [FromBody] RoleAdapter entity, CancellationToken cancellationToken) { - if (id != entity.Id?.ToString()) + if (_id != entity._Id) { return BadRequest("Role ID mismatch"); } var result = await service.UpdateRole(entity, cancellationToken).ConfigureAwait(false); - return Ok(result); } /// /// Changes the status of the role. /// - /// The role identifier. + /// The role mongo identifier. /// The new status of the role. + /// A token to cancel the asynchronous operation. /// The updated entity. - /// The role updates. - /// The role not found. - /// The role could not be deleted. + /// The role status updated. /// The service internal error. [HttpPatch] [Route(Routes.ChangeStatus)] [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] [Permission("RoleManagement.Write")] - public async Task ChangeRoleStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) + public async Task ChangeRoleStatus([FromRoute] string _id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) { - var result = await service.ChangeRoleStatus(id, newStatus, cancellationToken).ConfigureAwait(false); + var result = await service.ChangeRoleStatus(_id, newStatus, cancellationToken).ConfigureAwait(false); return Ok(result); } /// /// Adds an application to the role's list of applications. /// - /// The identifier of the role to which the application will be added. + /// The mongo identifier of the role to which the application will be added. /// The application enum value to add. - /// A representing the asynchronous operation, with the updated role object. - /// The role updates. - /// The role not found. - /// The role could not be deleted. + /// A token to cancel the asynchronous operation. + /// The updated object. + /// The application was added to the role. /// The service internal error. [HttpPost(Routes.AddApplication)] [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] @@ -149,12 +150,11 @@ namespace LSA.Core.Thalos.API.Controllers /// /// Removes an application from the role's list of applications. /// - /// The identifier of the role from which the application will be removed. + /// The mongo identifier of the role from which the application will be removed. /// The application enum value to remove. - /// A representing the asynchronous operation, with the updated role object. - /// The role updates. - /// The role not found. - /// The role could not be deleted. + /// A token to cancel the asynchronous operation. + /// The updated object. + /// The application was removed from the role. /// The service internal error. [HttpDelete(Routes.RemoveApplication)] [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] @@ -164,5 +164,28 @@ namespace LSA.Core.Thalos.API.Controllers var result = await service.RemoveApplicationFromRole(roleId, application, cancellationToken).ConfigureAwait(false); return Ok(result); } + + /// + /// Deletes a role by its mongo identifier. + /// + /// The role mongo identifier. + /// A token to cancel the asynchronous operation. + /// The deleted entity. + /// The role was deleted successfully. + /// The role was not found. + /// The service internal error. + [HttpDelete] + [Route(Routes.Id)] + [ProducesResponseType(typeof(RoleAdapter), StatusCodes.Status200OK)] + [Permission("RoleManagement.Write")] + public async Task DeleteRoleAsync([FromRoute] string _id, CancellationToken cancellationToken) + { + var result = await service.DeleteRole(_id, cancellationToken).ConfigureAwait(false); + + if (result is null) + return NotFound("Role not found."); + + return Ok(result); + } } } diff --git a/Core.Thalos.DAL.API/Controllers/TenantController.cs b/Core.Thalos.DAL.API/Controllers/TenantController.cs new file mode 100644 index 0000000..27c6be9 --- /dev/null +++ b/Core.Thalos.DAL.API/Controllers/TenantController.cs @@ -0,0 +1,162 @@ +// *********************************************************************** +// +// AgileWebs +// +// *********************************************************************** + +using Asp.Versioning; +using Core.Thalos.BuildingBlocks; +using Core.Thalos.Provider.Contracts; +using Microsoft.AspNetCore.Mvc; +using StatusEnum = Core.Blueprint.Mongo.StatusEnum; +using TenantRequest = Core.Thalos.Domain.Contexts.Onboarding.Request.TenantRequest; + +namespace LSA.Core.Thalos.API.Controllers +{ + /// + /// Handles all requests for Tenant authentication. + /// + [ApiVersion(MimeTypes.ApplicationVersion)] + [Route("api/v{api-version:apiVersion}/[controller]")] + [Produces(MimeTypes.ApplicationJson)] + [Consumes(MimeTypes.ApplicationJson)] + [ApiController] + // [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] + public class TenantController(ITenantProvider service) : ControllerBase + { + /// + /// Gets all Tenants. + /// + /// A token to cancel the asynchronous operation. + /// The found entities. + /// The tenants found. + /// The service internal error. + [HttpGet] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + // [Permission("TenantManagement.Read, RoleManagement.Read")] + public async Task GetAllTenantsAsync(CancellationToken cancellationToken) + { + var result = await service.GetAllTenants(cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Gets the Tenant by mongo identifier. + /// + /// The Tenant mongo identifier. + /// A token to cancel the asynchronous operation. + /// The found entity. + /// The tenant found. + /// The tenant not found error. + /// The service internal error. + [HttpGet] + [Route(Routes.Id)] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)] + // [Permission("TenantManagement.Read")] + public async Task GetTenantByIdAsync([FromRoute] string _id, CancellationToken cancellationToken) + { + var result = await service.GetTenantById(_id, cancellationToken).ConfigureAwait(false); + + if (result == null) + { + return NotFound("Entity not found"); + } + + return Ok(result); + } + + /// + /// Creates a new Tenant. + /// + /// The Tenant to be added. + /// A token to cancel the asynchronous operation. + /// The created entity. + /// The tenant was created. + /// The tenant could not be created. + /// The service internal error. + [HttpPost] + [ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status201Created)] + // [Permission("TenantManagement.Write")] + public async Task CreateTenantAsync([FromBody] TenantRequest newTenant, CancellationToken cancellationToken) + { + var result = await service.CreateTenant(newTenant, cancellationToken).ConfigureAwait(false); + return Created("CreatedWithIdAsync", result); + } + + /// + /// Updates a full Tenant by mongo identifier. + /// + /// The Tenant mongo identifier. + /// The Tenant to update. + /// A token to cancel the asynchronous operation. + /// The updated entity. + /// The tenant was updated. + /// Tenant ID mismatch. + /// The tenant could not be updated. + /// The service internal error. + [HttpPut] + [Route(Routes.Id)] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)] + // [Permission("TenantManagement.Write")] + public async Task UpdateTenantAsync([FromRoute] string _id, [FromBody] TenantAdapter entity, CancellationToken cancellationToken) + { + if (_id != entity._Id) + { + return BadRequest("Tenant ID mismatch"); + } + + var result = await service.UpdateTenant(entity, cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Changes the status of the Tenant. + /// + /// The Tenant mongo identifier. + /// The new status of the Tenant. + /// A token to cancel the asynchronous operation. + /// The updated entity. + /// The tenant status was updated. + /// The service internal error. + [HttpPatch] + [Route(Routes.ChangeStatus)] + [Consumes(MimeTypes.ApplicationJson)] + [Produces(MimeTypes.ApplicationJson)] + [ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)] + // [Permission("TenantManagement.Write")] + public async Task ChangeTenantStatus([FromRoute] string _id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) + { + var result = await service.ChangeTenantStatus(_id, newStatus, cancellationToken).ConfigureAwait(false); + return Ok(result); + } + + /// + /// Deletes a Tenant by mongo identifier. + /// + /// The Tenant mongo identifier. + /// A token to cancel the asynchronous operation. + /// The deleted entity if found. + /// The tenant was deleted. + /// The tenant was not found. + /// The service internal error. + [HttpDelete] + [Route(Routes.Id)] + [ProducesResponseType(typeof(TenantAdapter), StatusCodes.Status200OK)] + // [Permission("TenantManagement.Write")] + public async Task DeleteTenantAsync([FromRoute] string _id, CancellationToken cancellationToken) + { + var result = await service.DeleteTenant(_id, cancellationToken).ConfigureAwait(false); + + if (result is null) + return NotFound("Tenant not found."); + + return Ok(result); + } + } +} diff --git a/Core.Thalos.DAL.API/Controllers/UserController.cs b/Core.Thalos.DAL.API/Controllers/UserController.cs index 0eaaee7..d4d0a11 100644 --- a/Core.Thalos.DAL.API/Controllers/UserController.cs +++ b/Core.Thalos.DAL.API/Controllers/UserController.cs @@ -3,6 +3,7 @@ // AgileWebs // // *********************************************************************** + using Asp.Versioning; using Core.Thalos.BuildingBlocks; using Core.Thalos.Provider.Contracts; @@ -25,12 +26,10 @@ namespace LSA.Core.Thalos.API.Controllers public class UserController(IUserProvider service) : ControllerBase { /// - /// Gets all the users. - /// + /// mongo identifier all the users. + /// + /// A token to cancel the asynchronous operation. /// The found entity. - /// The users found. - /// The users not found error. - /// The service internal error. [HttpGet] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] @@ -42,38 +41,28 @@ namespace LSA.Core.Thalos.API.Controllers } /// - /// Gets the user by identifier. + /// mongo identifier the user by identifier. /// - /// The user identifier. + /// The user Mongo identifier. + /// A token to cancel the asynchronous operation. /// The found entity. - /// The user found. - /// The user not found error. - /// The service internal error. [HttpGet] [Route(Routes.Id)] [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] [Permission("UserManagement.Read")] - public async Task GetUserById([FromRoute] string id, CancellationToken cancellationToken) + public async Task GetUserById([FromRoute] string _id, CancellationToken cancellationToken) { - var result = await service.GetUserById(id, cancellationToken).ConfigureAwait(false); - - if (result == null) - { - return NotFound("Entity not found"); - } - - return Ok(result); + var result = await service.GetUserById(_id, cancellationToken).ConfigureAwait(false); + return result == null ? NotFound("Entity not found") : Ok(result); } /// - /// Gets the user by email. + /// mongo identifier the user by email. /// /// The user's email. + /// A token to cancel the asynchronous operation. /// The found entity. - /// The user found. - /// The user not found error. - /// The service internal error. [HttpGet] [Route(Routes.Email)] [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)] @@ -81,23 +70,15 @@ namespace LSA.Core.Thalos.API.Controllers public async Task GetUserByEmail([FromRoute] string email, CancellationToken cancellationToken) { var result = await service.GetUserByEmail(email, cancellationToken).ConfigureAwait(false); - - if (result == null) - { - return NotFound("User not found"); - } - - return Ok(result); + return result == null ? NotFound("User not found") : Ok(result); } /// /// Validates if a user exists on the database. /// /// The user's email. - /// The found entity. - /// The user found. - /// The user not found error. - /// The service internal error. + /// A token to cancel the asynchronous operation. + /// The indicating existence. [HttpGet] [Route("{email}/ValidateExistence")] [ProducesResponseType(typeof(UserExistenceAdapter), StatusCodes.Status200OK)] @@ -105,24 +86,15 @@ namespace LSA.Core.Thalos.API.Controllers public async Task ValidateUserExistence([FromRoute] string email, CancellationToken cancellationToken) { var result = await service.ValidateUserExistence(email, cancellationToken).ConfigureAwait(false); - - if (result == null) - { - return NotFound("User not found"); - } - - return Ok(result); + return result == null ? NotFound("User not found") : Ok(result); } /// /// Creates a new user. /// /// The user to be added. - /// Sends an invitation in case of third party access. + /// A token to cancel the asynchronous operation. /// The created entity. - /// The user created. - /// The user could not be created. - /// The service internal error. [HttpPost(Routes.Register)] [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status201Created)] [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] @@ -136,100 +108,78 @@ namespace LSA.Core.Thalos.API.Controllers /// /// Updates a full user by identifier. /// + /// The user Mongo identifier. /// The user to update. - /// The user identifier. + /// A token to cancel the asynchronous operation. /// The updated entity. - /// The user updated. - /// The user not found. - /// The user could not be updated. - /// The service internal error. [HttpPut] [Route(Routes.Id)] [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] [Permission("UserManagement.Write")] - public async Task UpdateUserAsync([FromRoute] string id, [FromBody] UserAdapter entity, CancellationToken cancellationToken) + public async Task UpdateUserAsync([FromRoute] string _id, [FromBody] UserAdapter entity, CancellationToken cancellationToken) { - if (id != entity.Id?.ToString()) - { + if (_id != entity._Id) return BadRequest("User ID mismatch"); - } var result = await service.UpdateUser(entity, cancellationToken).ConfigureAwait(false); - return Ok(result); } /// /// Logs in the user. /// - /// The User's email. - /// A representing - /// the asynchronous execution of the service. - /// The User found. - /// The User not found. - /// The service internal error. + /// The user's email. + /// A token to cancel the asynchronous operation. + /// The found entity. [HttpPatch(Routes.LogIn)] [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = $"{Schemes.DefaultScheme}, {Schemes.GoogleScheme}")] public async Task LoginUserAsync([FromRoute] string email, CancellationToken cancellationToken) { var result = await service.LogInUser(email, cancellationToken).ConfigureAwait(false); - - if (result is null) - return new NotFoundObjectResult($"The user with email: '{email}' was not found"); - - return Ok(result); + return result == null ? NotFound($"The user with email: '{email}' was not found") : Ok(result); } /// /// Logs out the user. /// - /// The User's email. - /// A representing - /// the asynchronous execution of the service. - /// The User updated. - /// The service internal error. + /// The user's email. + /// A token to cancel the asynchronous operation. + /// The updated entity. [HttpPatch(Routes.LogOut)] [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = $"{Schemes.DefaultScheme}, {Schemes.GoogleScheme}")] public async Task LogOutUserSessionAsync([FromRoute] string email, CancellationToken cancellationToken) { var result = await service.LogOutUserSession(email, cancellationToken).ConfigureAwait(false); - return Ok(result); - } /// /// Changes the status of the user. /// - /// The user identifier. + /// The user Mongo identifier. /// The new status of the user. + /// A token to cancel the asynchronous operation. /// The updated entity. - /// The user updates. - /// The user not found. - /// The user could not be deleted. - /// The service internal error. [HttpPatch] [Route(Routes.ChangeStatus)] [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)] [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] [Permission("UserManagement.Write")] - public async Task ChangeUserStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) + public async Task ChangeUserStatus([FromRoute] string _id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) { - var result = await service.ChangeUserStatus(id, newStatus, cancellationToken).ConfigureAwait(false); + var result = await service.ChangeUserStatus(_id, newStatus, cancellationToken).ConfigureAwait(false); return Ok(result); } /// - /// Gets a token for the user, including roles, permissions, and modules. + /// mongo identifier a token for the user, including roles, permissions, and modules. /// /// The user's email. - /// The token adapter with user details, role, permissions, and modules. - /// The token adapter with user details. - /// The user not found. - /// The service internal error. + /// A token to cancel the asynchronous operation. + /// The with user details. [HttpGet] [Route("{email}/GetTokenAdapter")] [ProducesResponseType(typeof(TokenAdapter), StatusCodes.Status200OK)] @@ -237,10 +187,26 @@ namespace LSA.Core.Thalos.API.Controllers public async Task GetTokenAdapter([FromRoute] string email, CancellationToken cancellationToken) { var tokenAdapter = await service.GetToken(email, cancellationToken).ConfigureAwait(false); + return tokenAdapter == null ? NotFound($"User with email: {email} not found") : Ok(tokenAdapter); + } - if (tokenAdapter == null) return NotFound($"User with email: {email} not found"); - - return Ok(tokenAdapter); + /// + /// Deletes a user by identifier. + /// + /// The user Mongo identifier. + /// A token to cancel the asynchronous operation. + /// The deleted entity. + /// The user deleted. + /// The user not found. + [HttpDelete] + [Route(Routes.Id)] + [ProducesResponseType(typeof(UserAdapter), StatusCodes.Status200OK)] + [Authorize(AuthenticationSchemes = Schemes.DefaultScheme)] + [Permission("UserManagement.Write")] + public async Task DeleteUserAsync([FromRoute] string _id, CancellationToken cancellationToken) + { + var result = await service.DeleteUser(_id, cancellationToken).ConfigureAwait(false); + return result == null ? NotFound("User not found") : Ok(result); } } } diff --git a/Core.Thalos.Domain/Contexts/Onboarding/Request/TenantRequest.cs b/Core.Thalos.Domain/Contexts/Onboarding/Request/TenantRequest.cs new file mode 100644 index 0000000..72619ae --- /dev/null +++ b/Core.Thalos.Domain/Contexts/Onboarding/Request/TenantRequest.cs @@ -0,0 +1,96 @@ +// *********************************************************************** +// +// AgileWebs +// +// *********************************************************************** +using MongoDB.Bson.Serialization.Attributes; + +namespace Core.Thalos.Domain.Contexts.Onboarding.Request +{ + /// + /// Represents a tenant creation request with business and contact details. + /// + public class TenantRequest + { + /// + /// The legal or commercial name of the tenant. + /// + [BsonElement("name")] + public string Name { get; set; } = null!; + + /// + /// The tax identification number of the tenant (e.g., RFC, VAT). + /// + [BsonElement("taxIdentifier")] + public string TaxIdentifier { get; set; } = null!; + + /// + /// The primary address line (street, number, etc.). + /// + [BsonElement("addressLine1")] + public string AddressLine1 { get; set; } = null!; + + /// + /// An optional second address line (apartment, suite, etc.). + /// + [BsonElement("addressLine2")] + [BsonIgnoreIfNull] + public string? AddressLine2 { get; set; } + + /// + /// The city where the tenant is located. + /// + [BsonElement("city")] + public string City { get; set; } = null!; + + /// + /// The state, province, or region of the tenant. + /// + [BsonElement("state")] + public string State { get; set; } = null!; + + /// + /// The country of the tenant. + /// + [BsonElement("country")] + public string Country { get; set; } = null!; + + /// + /// The postal or ZIP code of the tenant’s location. + /// + [BsonElement("postalCode")] + public string PostalCode { get; set; } = null!; + + /// + /// The main email address for contacting the tenant. + /// + [BsonElement("contactEmail")] + public string ContactEmail { get; set; } = null!; + + /// + /// The main phone number for contacting the tenant. + /// + [BsonElement("contactPhone")] + public string ContactPhone { get; set; } = null!; + + /// + /// The tenant’s website URL, if available. + /// + [BsonElement("website")] + [BsonIgnoreIfNull] + public string? Website { get; set; } + + /// + /// The database connection string for the tenant, if applicable. + /// + [BsonElement("connectionString")] + [BsonIgnoreIfNull] + public string? ConnectionString { get; set; } + + /// + /// Indicates whether the tenant uses an isolated database. + /// + [BsonElement("isolated")] + public bool Isolated { get; set; } + } +} diff --git a/Core.Thalos.Domain/Core.Thalos.Domain.csproj b/Core.Thalos.Domain/Core.Thalos.Domain.csproj index d09f8c0..710df0c 100644 --- a/Core.Thalos.Domain/Core.Thalos.Domain.csproj +++ b/Core.Thalos.Domain/Core.Thalos.Domain.csproj @@ -8,7 +8,7 @@ - + diff --git a/Core.Thalos.Provider/Contracts/IModuleProvider.cs b/Core.Thalos.Provider/Contracts/IModuleProvider.cs index 9e63dd5..d8275cd 100644 --- a/Core.Thalos.Provider/Contracts/IModuleProvider.cs +++ b/Core.Thalos.Provider/Contracts/IModuleProvider.cs @@ -3,61 +3,85 @@ // AgileWebs // // *********************************************************************** + using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; namespace Core.Thalos.Provider.Contracts { + /// + /// Interface for Module-related service operations. + /// public interface IModuleProvider { /// /// Creates a new Module. /// - /// The Module to be created. - /// A representing - /// the asynchronous execution of the service. + /// The Module to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask CreateModule(ModuleRequest newModule, CancellationToken cancellationToken); /// - /// Gets an Module by identifier. + /// Gets a Module by its identifier. /// - /// The Module identifier. - /// A representing - /// the asynchronous execution of the service. + /// The Module Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask GetModuleById(string _id, CancellationToken cancellationToken); /// - /// Gets all the roles. + /// Gets all Modules. /// - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask> GetAllModules(CancellationToken cancellationToken); /// - /// Gets all the permissions by permissions identifier list. + /// Gets all Modules by a list of identifiers. /// - /// The list of permissions identifiers. - /// A representing - /// the asynchronous execution of the service. + /// The list of Module identifiers. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask> GetAllModulesByList(string[] modules, CancellationToken cancellationToken); /// - /// Changes the status of the permission. + /// Changes the status of a Module. /// - /// The permission identifier. - /// The new status of the permission. - /// The updated entity. - /// A representing - /// the asynchronous execution of the service. - ValueTask ChangeModuleStatus(string id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); + /// The Module Mongo identifier. + /// The new status of the Module. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask ChangeModuleStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); /// - /// Updates a Module by id. + /// Updates a Module by its identifier. /// /// The Module to be updated. - /// The Module identifier. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken); + + /// + /// Deletes a Module by its identifier. + /// + /// The Module Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask DeleteModule(string _id, CancellationToken cancellationToken); } } diff --git a/Core.Thalos.Provider/Contracts/IPermissionProvider.cs b/Core.Thalos.Provider/Contracts/IPermissionProvider.cs index 3824b61..bfcafe6 100644 --- a/Core.Thalos.Provider/Contracts/IPermissionProvider.cs +++ b/Core.Thalos.Provider/Contracts/IPermissionProvider.cs @@ -3,61 +3,85 @@ // AgileWebs // // *********************************************************************** + using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; namespace Core.Thalos.Provider.Contracts { + /// + /// Interface for Permission-related service operations. + /// public interface IPermissionProvider { /// /// Creates a new Permission. /// - /// The Permission to be created. - /// A representing - /// the asynchronous execution of the service. + /// The Permission to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask CreatePermission(PermissionRequest newPermission, CancellationToken cancellationToken); /// - /// Gets an Permission by identifier. + /// Gets a Permission by its identifier. /// - /// The Permission identifier. - /// A representing - /// the asynchronous execution of the service. + /// The Permission Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask GetPermissionById(string _id, CancellationToken cancellationToken); /// - /// Gets all the roles. + /// Gets all Permissions. /// - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask> GetAllPermissions(CancellationToken cancellationToken); /// - /// Gets all the permissions by permissions identifier list. + /// Gets all Permissions by a list of identifiers. /// - /// The list of permissions identifiers. - /// A representing - /// the asynchronous execution of the service. + /// The list of Permission identifiers. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask> GetAllPermissionsByList(string[] permissions, CancellationToken cancellationToken); /// - /// Changes the status of the permission. + /// Changes the status of a Permission. /// - /// The permission identifier. - /// The new status of the permission. - /// The updated entity. - /// A representing - /// the asynchronous execution of the service. - ValueTask ChangePermissionStatus(string id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); + /// The Permission Mongo identifier. + /// The new status of the Permission. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask ChangePermissionStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); /// - /// Updates a Permission by id. + /// Updates a Permission. /// /// The Permission to be updated. - /// The Permission identifier. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken); + + /// + /// Deletes a Permission by its identifier. + /// + /// The Permission Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask DeletePermission(string _id, CancellationToken cancellationToken); } } diff --git a/Core.Thalos.Provider/Contracts/IRoleProvider.cs b/Core.Thalos.Provider/Contracts/IRoleProvider.cs index a8c8676..52f5966 100644 --- a/Core.Thalos.Provider/Contracts/IRoleProvider.cs +++ b/Core.Thalos.Provider/Contracts/IRoleProvider.cs @@ -3,69 +3,97 @@ // AgileWebs // // *********************************************************************** + using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; namespace Core.Thalos.Provider.Contracts { + /// + /// Interface for Role-related service operations. + /// public interface IRoleProvider { /// /// Creates a new Role. /// - /// The Role to be created. - /// A representing - /// the asynchronous execution of the service. + /// The Role to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask CreateRole(RoleRequest newRole, CancellationToken cancellationToken); /// - /// Gets an Role by identifier. + /// Gets a Role by its identifier. /// - /// The Role identifier. - /// A representing - /// the asynchronous execution of the service. + /// The Role Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask GetRoleById(string _id, CancellationToken cancellationToken); /// - /// Gets all the roles. + /// Gets all Roles. /// - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask> GetAllRoles(CancellationToken cancellationToken); /// - /// Changes the status of the role. + /// Changes the status of a Role. /// - /// The role identifier. - /// The new status of the role. - /// The updated entity. - /// A representing - /// the asynchronous execution of the service. - ValueTask ChangeRoleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); + /// The Role Mongo identifier. + /// The new status of the Role. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask ChangeRoleStatus(string _id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); /// - /// Updates a Role by id. + /// Updates a Role. /// /// The Role to be updated. - /// The Role identifier. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask UpdateRole(RoleAdapter entity, CancellationToken cancellationToken); /// - /// Adds an application to the role's list of applications. + /// Adds an application to the Role's list of applications. /// - /// The identifier of the role to which the application will be added. + /// The identifier of the Role to which the application will be added. /// The application enum value to add. - /// A representing the asynchronous operation, with the updated role object. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous operation with the updated Role. + /// ValueTask AddApplicationToRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken); /// - /// Removes an application from the role's list of applications. + /// Removes an application from the Role's list of applications. /// - /// The identifier of the role from which the application will be removed. + /// The identifier of the Role from which the application will be removed. /// The application enum value to remove. - /// A representing the asynchronous operation, with the updated role object. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous operation with the updated Role. + /// ValueTask RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken); + + /// + /// Deletes a Role by its identifier. + /// + /// The Role Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask DeleteRole(string _id, CancellationToken cancellationToken); } } diff --git a/Core.Thalos.Provider/Contracts/ITenantProvider.cs b/Core.Thalos.Provider/Contracts/ITenantProvider.cs new file mode 100644 index 0000000..85892e8 --- /dev/null +++ b/Core.Thalos.Provider/Contracts/ITenantProvider.cs @@ -0,0 +1,77 @@ +// *********************************************************************** +// +// AgileWebs +// +// *********************************************************************** + +using Core.Thalos.BuildingBlocks; +using Core.Thalos.Domain.Contexts.Onboarding.Request; + +namespace Core.Thalos.Provider.Contracts +{ + /// + /// Interface for Tenant-related service operations. + /// + public interface ITenantProvider + { + /// + /// Creates a new Tenant. + /// + /// The Tenant to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask CreateTenant(TenantRequest newTenant, CancellationToken cancellationToken); + + /// + /// Gets a Tenant by its identifier. + /// + /// The Tenant Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask GetTenantById(string _id, CancellationToken cancellationToken); + + /// + /// Gets all Tenants. + /// + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask> GetAllTenants(CancellationToken cancellationToken); + + /// + /// Changes the status of a Tenant. + /// + /// The Tenant Mongo identifier. + /// The new status of the Tenant. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask ChangeTenantStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); + + /// + /// Updates a Tenant. + /// + /// The Tenant to be updated. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask UpdateTenant(TenantAdapter entity, CancellationToken cancellationToken); + + /// + /// Deletes a Tenant by its identifier. + /// + /// The Tenant Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask DeleteTenant(string _id, CancellationToken cancellationToken); + } +} diff --git a/Core.Thalos.Provider/Contracts/IUserProvider.cs b/Core.Thalos.Provider/Contracts/IUserProvider.cs index 2af9487..68777f0 100644 --- a/Core.Thalos.Provider/Contracts/IUserProvider.cs +++ b/Core.Thalos.Provider/Contracts/IUserProvider.cs @@ -3,100 +3,125 @@ // AgileWebs // // *********************************************************************** + using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; namespace Core.Thalos.Provider.Contracts { + /// + /// Interface for User-related service operations. + /// public interface IUserProvider { /// /// Creates a new User. /// - /// The User to be created. - /// A representing - /// the asynchronous execution of the service. + /// The User to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask CreateUser(UserRequest newUser, CancellationToken cancellationToken); /// - /// Gets an User by identifier. + /// Gets a User by Mongo identifier. /// - /// The User identifier. - /// A representing - /// the asynchronous execution of the service. + /// The User Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask GetUserById(string _id, CancellationToken cancellationToken); - /// - /// Gets all the users. + /// Gets all Users. /// - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask> GetAllUsers(CancellationToken cancellationToken); /// - /// Gets an User by email. + /// Gets a User by email. /// - /// The User email. - /// A representing - /// the asynchronous execution of the service. - ValueTask GetUserByEmail(string? email, CancellationToken cancellationToken); + /// The User's email. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask GetUserByEmail(string email, CancellationToken cancellationToken); /// - /// Validates if a users exists by email. + /// Validates if a User exists by email. /// - /// The User email. - /// A representing - /// the asynchronous execution of the service. - ValueTask ValidateUserExistence(string? email, CancellationToken cancellationToken); + /// The User's email. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask ValidateUserExistence(string email, CancellationToken cancellationToken); /// - /// Changes the status of the user. + /// Changes the status of a User. /// - /// The user identifier. - /// The new status of the user. - /// A representing - /// the asynchronous execution of the service. - ValueTask ChangeUserStatus(string id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); + /// The User Mongo identifier. + /// The new status of the User. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask ChangeUserStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken); /// - /// Updates a User by id. + /// Updates a User. /// /// The User to be updated. - /// The User identifier. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask UpdateUser(UserAdapter entity, CancellationToken cancellationToken); /// - /// Logs in the user. + /// Logs in the User. /// /// The User's email. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask LogInUser(string email, CancellationToken cancellationToken); /// - /// Logs out the user's session. + /// Logs out the User's session. /// /// The User's email. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask LogOutUserSession(string email, CancellationToken cancellationToken); /// - /// Gets the token adapter for a user. + /// Gets the TokenAdapter for a User. /// - /// The user's email. - /// A representing the asynchronous execution of the service. + /// The User's email. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// ValueTask GetToken(string email, CancellationToken cancellationToken); /// - /// Delete an User by identifier. + /// Deletes a User by Mongo identifier. /// - /// The User identifier. - /// A representing - /// the asynchronous execution of the service. - ValueTask DeleteUser(string _id, CancellationToken cancellationToken); + /// The User Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + ValueTask DeleteUser(string _id, CancellationToken cancellationToken); } } diff --git a/Core.Thalos.Provider/Providers/Onboarding/ModuleProvider.cs b/Core.Thalos.Provider/Providers/Onboarding/ModuleProvider.cs index e24135c..838abf6 100644 --- a/Core.Thalos.Provider/Providers/Onboarding/ModuleProvider.cs +++ b/Core.Thalos.Provider/Providers/Onboarding/ModuleProvider.cs @@ -3,6 +3,7 @@ // AgileWebs // // *********************************************************************** + using Core.Blueprint.Mongo; using Core.Blueprint.Redis; using Core.Blueprint.Redis.Helpers; @@ -10,8 +11,8 @@ using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; using Core.Thalos.Provider.Contracts; using Mapster; -using Microsoft.Extensions.Options; using MongoDB.Driver; +using StatusEnum = Core.Blueprint.Mongo.StatusEnum; namespace Core.Thalos.Provider.Providers.Onboarding { @@ -21,59 +22,63 @@ namespace Core.Thalos.Provider.Providers.Onboarding public class ModuleProvider : IModuleProvider { private readonly CollectionRepository repository; - private readonly CacheSettings cacheSettings; + private readonly ICacheSettings cacheSettings; private readonly IRedisCacheProvider cacheProvider; - public ModuleProvider(CollectionRepository repository, + public ModuleProvider( + CollectionRepository repository, IRedisCacheProvider cacheProvider, - IOptions cacheSettings) + ICacheSettings cacheSettings) { this.repository = repository; this.repository.CollectionInitialization(); - this.cacheSettings = cacheSettings.Value; this.cacheProvider = cacheProvider; + this.cacheSettings = cacheSettings; } /// /// Creates a new Module. /// - /// The Module to be created. - /// A representing - /// the asynchronous execution of the service. + /// The Module to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask CreateModule(ModuleRequest newModule, CancellationToken cancellationToken) { var moduleCollection = newModule.Adapt(); - await repository.InsertOneAsync(moduleCollection); - return moduleCollection; } /// - /// Gets an Module by identifier. + /// Gets a Module by identifier. /// - /// The Module identifier. - /// A representing - /// the asynchronous execution of the service.0 + /// The Module Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask GetModuleById(string _id, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModuleById", _id); var cachedData = await cacheProvider.GetAsync(cacheKey); - if (cachedData is not null) { return cachedData; } + if (cachedData is not null) return cachedData; var module = await repository.FindByIdAsync(_id); - - await cacheProvider.SetAsync(cacheKey, module); + await cacheProvider.SetAsync(cacheKey, module, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return module; } /// - /// Gets all the modules. + /// Gets all the Modules. /// - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask> GetAllModules(CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetModules"); @@ -82,22 +87,22 @@ namespace Core.Thalos.Provider.Providers.Onboarding if (cachedData.Any()) return cachedData; var modules = await repository.AsQueryable(); - await cacheProvider.SetAsync(cacheKey, modules); return modules; } /// - /// Gets all the modules by modules identifier list. + /// Gets all the Modules by a list of identifiers. /// - /// The list of modules identifiers. - /// A representing - /// the asynchronous execution of the service. + /// The list of Module identifiers. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask> GetAllModulesByList(string[] modules, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllModulesByList", modules); - var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; if (cachedData.Any()) return cachedData; @@ -105,49 +110,61 @@ namespace Core.Thalos.Provider.Providers.Onboarding var builder = Builders.Filter; var filters = new List>(); - if (modules != null || !modules.Any()) + if (modules is { Length: > 0 }) { filters.Add(builder.In(x => x._Id, modules)); } var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty; - var modulesList = await repository.FilterByMongoFilterAsync(finalFilter); - await cacheProvider.SetAsync(cacheKey, modulesList); - + await cacheProvider.SetAsync(cacheKey, modulesList, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return modulesList; } - /// - /// Changes the status of the module. + /// Changes the status of the Module. /// - /// The module identifier. - /// The new status of the module. - /// A representing - /// the asynchronous execution of the service. - public async ValueTask ChangeModuleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) + /// The Module Mongo identifier. + /// The new status of the Module. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask ChangeModuleStatus(string _id, StatusEnum newStatus, CancellationToken cancellationToken) { - var entity = await repository.FindByIdAsync(id); + var entity = await repository.FindByIdAsync(_id); entity.Status = newStatus; - await repository.ReplaceOneAsync(entity); - return entity; } /// - /// Updates a Module by id. + /// Updates a Module. /// /// The Module to be updated. - /// The Module identifier. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask UpdateModule(ModuleAdapter entity, CancellationToken cancellationToken) { await repository.ReplaceOneAsync(entity); + return entity; + } + /// + /// Deletes a Module by identifier. + /// + /// The Module Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous deletion result. + /// The deleted Module entity if found; otherwise, null. + /// + public async ValueTask DeleteModule(string _id, CancellationToken cancellationToken) + { + var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id); return entity; } } diff --git a/Core.Thalos.Provider/Providers/Onboarding/PermissionProvider.cs b/Core.Thalos.Provider/Providers/Onboarding/PermissionProvider.cs index 5abc218..5cb2955 100644 --- a/Core.Thalos.Provider/Providers/Onboarding/PermissionProvider.cs +++ b/Core.Thalos.Provider/Providers/Onboarding/PermissionProvider.cs @@ -3,6 +3,7 @@ // AgileWebs // // *********************************************************************** + using Core.Blueprint.Mongo; using Core.Blueprint.Redis; using Core.Blueprint.Redis.Helpers; @@ -10,7 +11,6 @@ using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; using Core.Thalos.Provider.Contracts; using Mapster; -using Microsoft.Extensions.Options; using MongoDB.Driver; namespace Core.Thalos.Provider.Providers.Onboarding @@ -21,60 +21,63 @@ namespace Core.Thalos.Provider.Providers.Onboarding public class PermissionProvider : IPermissionProvider { private readonly CollectionRepository repository; - private readonly CacheSettings cacheSettings; + private readonly ICacheSettings cacheSettings; private readonly IRedisCacheProvider cacheProvider; - public PermissionProvider(CollectionRepository repository, + public PermissionProvider( + CollectionRepository repository, IRedisCacheProvider cacheProvider, - IOptions cacheSettings - ) + ICacheSettings cacheSettings) { this.repository = repository; this.repository.CollectionInitialization(); - this.cacheSettings = cacheSettings.Value; this.cacheProvider = cacheProvider; + this.cacheSettings = cacheSettings; } /// /// Creates a new Permission. /// - /// The Permission to be created. - /// A representing - /// the asynchronous execution of the service. + /// The Permission to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask CreatePermission(PermissionRequest newPermission, CancellationToken cancellationToken) { var permissionCollection = newPermission.Adapt(); - await repository.InsertOneAsync(permissionCollection); - return permissionCollection; } /// - /// Gets an Permission by identifier. + /// Gets a Permission by identifier. /// - /// The Permission identifier. - /// A representing - /// the asynchronous execution of the service.0 + /// The Permission Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask GetPermissionById(string _id, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetPermissionById", _id); var cachedData = await cacheProvider.GetAsync(cacheKey); - //if (cachedData is not null) { return cachedData; } + if (cachedData is not null) return cachedData; var permission = await repository.FindByIdAsync(_id); - await cacheProvider.SetAsync(cacheKey, permission); return permission; } /// - /// Gets all the permissions. + /// Gets all Permissions. /// - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask> GetAllPermissions(CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissions"); @@ -83,22 +86,22 @@ namespace Core.Thalos.Provider.Providers.Onboarding if (cachedData.Any()) return cachedData; var permissions = await repository.AsQueryable(); - - await cacheProvider.SetAsync(cacheKey, permissions); + await cacheProvider.SetAsync(cacheKey, permissions, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return permissions; } /// - /// Gets all the permissions by permissions identifier list. + /// Gets all Permissions by a list of identifiers. /// - /// The list of permissions identifiers. - /// A representing - /// the asynchronous execution of the service. + /// The list of Permission identifiers. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask> GetAllPermissionsByList(string[] permissions, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllPermissionsByList", permissions); - var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; if (cachedData.Any()) return cachedData; @@ -106,49 +109,61 @@ namespace Core.Thalos.Provider.Providers.Onboarding var builder = Builders.Filter; var filters = new List>(); - if (permissions != null || !permissions.Any()) + if (permissions is { Length: > 0 }) { filters.Add(builder.In(x => x._Id, permissions)); } var finalFilter = filters.Any() ? builder.And(filters) : builder.Empty; - var permissionsList = await repository.FilterByMongoFilterAsync(finalFilter); - await cacheProvider.SetAsync(cacheKey, permissionsList); - + await cacheProvider.SetAsync(cacheKey, permissionsList, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return permissionsList; } - /// - /// Changes the status of the permission. + /// Changes the status of a Permission. /// - /// The permission identifier. - /// The new status of the permission. - /// A representing - /// the asynchronous execution of the service. - public async ValueTask ChangePermissionStatus(string id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) + /// The Permission Mongo identifier. + /// The new status of the Permission. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask ChangePermissionStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) { - var entity = await repository.FindByIdAsync(id); + var entity = await repository.FindByIdAsync(_id); entity.Status = newStatus; - await repository.ReplaceOneAsync(entity); - return entity; } /// - /// Updates a Permission by id. + /// Updates a Permission. /// /// The Permission to be updated. - /// The Permission identifier. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask UpdatePermission(PermissionAdapter entity, CancellationToken cancellationToken) { await repository.ReplaceOneAsync(entity); + return entity; + } + /// + /// Deletes a Permission by identifier. + /// + /// The Permission Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous deletion result. + /// The deleted Permission entity if found; otherwise, null. + /// + public async ValueTask DeletePermission(string _id, CancellationToken cancellationToken) + { + var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id); return entity; } } diff --git a/Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs b/Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs index 02eafb8..fb24faf 100644 --- a/Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs +++ b/Core.Thalos.Provider/Providers/Onboarding/RoleProvider.cs @@ -3,6 +3,7 @@ // AgileWebs // // *********************************************************************** + using Core.Blueprint.Mongo; using Core.Blueprint.Redis; using Core.Blueprint.Redis.Helpers; @@ -10,7 +11,6 @@ using Core.Thalos.BuildingBlocks; using Core.Thalos.Domain.Contexts.Onboarding.Request; using Core.Thalos.Provider.Contracts; using Mapster; -using Microsoft.Extensions.Options; using MongoDB.Driver; namespace Core.Thalos.Provider.Providers.Onboarding @@ -21,60 +21,63 @@ namespace Core.Thalos.Provider.Providers.Onboarding public class RoleProvider : IRoleProvider { private readonly CollectionRepository repository; - private readonly CacheSettings cacheSettings; + private readonly ICacheSettings cacheSettings; private readonly IRedisCacheProvider cacheProvider; - public RoleProvider(CollectionRepository repository, + public RoleProvider( + CollectionRepository repository, IRedisCacheProvider cacheProvider, - IOptions cacheSettings - ) + ICacheSettings cacheSettings) { this.repository = repository; this.repository.CollectionInitialization(); - this.cacheSettings = cacheSettings.Value; this.cacheProvider = cacheProvider; + this.cacheSettings = cacheSettings; } /// /// Creates a new Role. /// - /// The Role to be created. - /// A representing - /// the asynchronous execution of the service. + /// The Role to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask CreateRole(RoleRequest newRole, CancellationToken cancellationToken) { var roleCollection = newRole.Adapt(); - await repository.InsertOneAsync(roleCollection); - return roleCollection; } /// - /// Gets an Role by identifier. + /// Gets a Role by its identifier. /// - /// The Role identifier. - /// A representing - /// the asynchronous execution of the service. + /// The Role Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask GetRoleById(string _id, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetRoleById", _id); var cachedData = await cacheProvider.GetAsync(cacheKey); - if (cachedData is not null) { return cachedData; } + if (cachedData is not null) return cachedData; var role = await repository.FindByIdAsync(_id); - - await cacheProvider.SetAsync(cacheKey, role); + await cacheProvider.SetAsync(cacheKey, role, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return role; } /// - /// Gets all the roles. + /// Gets all Roles. /// - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask> GetAllRoles(CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllRoles"); @@ -83,84 +86,101 @@ namespace Core.Thalos.Provider.Providers.Onboarding if (cachedData.Any()) return cachedData; var roles = await repository.AsQueryable(); - - await cacheProvider.SetAsync(cacheKey, roles); + await cacheProvider.SetAsync(cacheKey, roles, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return roles; } /// - /// Changes the status of the role. + /// Changes the status of a Role. /// - /// The role identifier. - /// The new status of the role. - /// A representing - /// the asynchronous execution of the service. - public async ValueTask ChangeRoleStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) + /// The Role Mongo identifier. + /// The new status of the Role. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask ChangeRoleStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) { - var entity = await repository.FindByIdAsync(id); + var entity = await repository.FindByIdAsync(_id); entity.Status = newStatus; await repository.ReplaceOneAsync(entity); - return entity; } /// - /// Updates a Role by id. + /// Updates a Role. /// /// The Role to be updated. - /// The Role identifier. - /// A representing - /// the asynchronous execution of the service. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// public async ValueTask UpdateRole(RoleAdapter entity, CancellationToken cancellationToken) { await repository.ReplaceOneAsync(entity); - return entity; } /// - /// Adds an application to the role's list of applications. + /// Adds an application to the Role's list of applications. /// - /// The identifier of the role to which the application will be added. + /// The identifier of the Role to which the application will be added. /// The application enum value to add. - /// A representing the asynchronous operation, with the updated role object. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous operation, with the updated Role object. + /// public async ValueTask AddApplicationToRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken) { var role = await repository.FindOneAsync( - u => u._Id == roleId && - u.Status == Core.Blueprint.Mongo.StatusEnum.Active); + u => u._Id == roleId && u.Status == Blueprint.Mongo.StatusEnum.Active); - var updatedApplications = role.Applications.Append(application).Distinct().ToArray(); + var updatedApplications = role.Applications?.Append(application).Distinct().ToArray(); role.Applications = updatedApplications; + await repository.ReplaceOneAsync(role); + return role; + } + + /// + /// Removes an application from the Role's list of applications. + /// + /// The identifier of the Role from which the application will be removed. + /// The application enum value to remove. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous operation, with the updated Role object. + /// + public async ValueTask RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken) + { + var role = await repository.FindOneAsync( + u => u._Id == roleId && u.Status == Blueprint.Mongo.StatusEnum.Active); + + var updatedApplications = role.Applications? + .Where(c => c != application) + .ToArray(); + + role.Applications = updatedApplications; await repository.ReplaceOneAsync(role); return role; } /// - /// Removes an application from the role's list of applications. + /// Deletes a Role by identifier. /// - /// The identifier of the role from which the application will be removed. - /// The application enum value to remove. - /// A representing the asynchronous operation, with the updated role object. - public async ValueTask RemoveApplicationFromRole(string roleId, ApplicationsEnum application, CancellationToken cancellationToken) + /// The Role Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous deletion result. + /// The deleted Role entity if found; otherwise, null. + /// + public async ValueTask DeleteRole(string _id, CancellationToken cancellationToken) { - var role = await repository.FindOneAsync( - u => u._Id == roleId && - u.Status == Core.Blueprint.Mongo.StatusEnum.Active); - - var updatedApplications = role.Applications - ?.Where(c => c != application) - .ToArray(); - - role.Applications = updatedApplications; - - await repository.ReplaceOneAsync(role); - - return role; + var entity = await repository.DeleteOneAsync(doc => doc._Id == _id); + return entity; } } } diff --git a/Core.Thalos.Provider/Providers/Onboarding/TenantProvider.cs b/Core.Thalos.Provider/Providers/Onboarding/TenantProvider.cs new file mode 100644 index 0000000..005cf33 --- /dev/null +++ b/Core.Thalos.Provider/Providers/Onboarding/TenantProvider.cs @@ -0,0 +1,140 @@ +// *********************************************************************** +// +// AgileWebs +// +// *********************************************************************** + +using Core.Blueprint.Mongo; +using Core.Blueprint.Redis; +using Core.Blueprint.Redis.Helpers; +using Core.Thalos.BuildingBlocks; +using Core.Thalos.Domain.Contexts.Onboarding.Request; +using Core.Thalos.Provider.Contracts; +using Mapster; + +namespace Core.Thalos.Provider.Providers.Onboarding +{ + /// + /// Handles all services and business rules related to . + /// + public class TenantProvider : ITenantProvider + { + private readonly CollectionRepository repository; + private readonly ICacheSettings cacheSettings; + private readonly IRedisCacheProvider cacheProvider; + + public TenantProvider( + CollectionRepository repository, + IRedisCacheProvider cacheProvider, + ICacheSettings cacheSettings) + { + this.repository = repository; + this.repository.CollectionInitialization(); + this.cacheSettings = cacheSettings; + this.cacheProvider = cacheProvider; + } + + /// + /// Creates a new Tenant. + /// + /// The Tenant to be created. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask CreateTenant(TenantRequest newTenant, CancellationToken cancellationToken) + { + var tenant = newTenant.Adapt(); + await repository.InsertOneAsync(tenant); + return tenant; + } + + /// + /// Gets a Tenant by identifier. + /// + /// The Tenant Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask GetTenantById(string _id, CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTenantById", _id); + var cachedData = await cacheProvider.GetAsync(cacheKey); + + if (cachedData is not null) return cachedData; + + var tenant = await repository.FindByIdAsync(_id); + await cacheProvider.SetAsync(cacheKey, tenant, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); + + return tenant; + } + + /// + /// Gets all Tenants. + /// + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask> GetAllTenants(CancellationToken cancellationToken) + { + var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetTenants"); + var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; + + if (cachedData.Any()) return cachedData; + + var tenants = await repository.AsQueryable(); + await cacheProvider.SetAsync(cacheKey, tenants, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); + + return tenants; + } + + /// + /// Changes the status of a Tenant. + /// + /// The Tenant Mongo identifier. + /// The new status of the Tenant. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask ChangeTenantStatus(string _id, Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) + { + var entity = await repository.FindByIdAsync(_id); + entity.Status = newStatus; + + await repository.ReplaceOneAsync(entity); + return entity; + } + + /// + /// Updates a Tenant. + /// + /// The Tenant to be updated. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous execution of the service. + /// + public async ValueTask UpdateTenant(TenantAdapter entity, CancellationToken cancellationToken) + { + await repository.ReplaceOneAsync(entity); + return entity; + } + + /// + /// Deletes a Tenant by identifier. + /// + /// The Tenant Mongo identifier. + /// A token to cancel the asynchronous operation. + /// + /// A representing the asynchronous deletion result. + /// The deleted Tenant entity if found; otherwise, null. + /// + public async ValueTask DeleteTenant(string _id, CancellationToken cancellationToken) + { + var entity = await repository.DeleteOneAsync(doc => doc._Id == _id); + return entity; + } + } +} diff --git a/Core.Thalos.Provider/Providers/Onboarding/UserProvider.cs b/Core.Thalos.Provider/Providers/Onboarding/UserProvider.cs index 9d9888b..3f28857 100644 --- a/Core.Thalos.Provider/Providers/Onboarding/UserProvider.cs +++ b/Core.Thalos.Provider/Providers/Onboarding/UserProvider.cs @@ -10,7 +10,6 @@ using Core.Blueprint.Redis.Helpers; using Core.Thalos.BuildingBlocks; using Core.Thalos.Provider.Contracts; using Mapster; -using Microsoft.Extensions.Options; using MongoDB.Bson; using MongoDB.Bson.Serialization; using MongoDB.Driver; @@ -24,17 +23,17 @@ namespace Core.Thalos.Provider.Providers.Onboarding public class UserProvider : IUserProvider { private readonly CollectionRepository repository; - private readonly CacheSettings cacheSettings; + private readonly ICacheSettings cacheSettings; private readonly IRedisCacheProvider cacheProvider; public UserProvider(CollectionRepository repository, IRedisCacheProvider cacheProvider, - IOptions cacheSettings + ICacheSettings cacheSettings ) { this.repository = repository; this.repository.CollectionInitialization(); - this.cacheSettings = cacheSettings.Value; + this.cacheSettings = cacheSettings; this.cacheProvider = cacheProvider; } @@ -56,7 +55,7 @@ namespace Core.Thalos.Provider.Providers.Onboarding /// /// Gets an User by identifier. /// - /// The User identifier. + /// The User mongo identifier. /// A representing /// the asynchronous execution of the service. public async ValueTask GetUserById(string _id, CancellationToken cancellationToken) @@ -64,11 +63,11 @@ namespace Core.Thalos.Provider.Providers.Onboarding var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetUserById", _id); var cachedData = await cacheProvider.GetAsync(cacheKey); - //if (cachedData is not null) { return cachedData; } + if (cachedData is not null) { return cachedData; } var user = await repository.FindByIdAsync(_id); - await cacheProvider.SetAsync(cacheKey, user); + await cacheProvider.SetAsync(cacheKey, user, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return user; } @@ -83,11 +82,11 @@ namespace Core.Thalos.Provider.Providers.Onboarding var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllUsers"); var cachedData = await cacheProvider.GetAsync>(cacheKey) ?? []; - if (cachedData.Any()) return cachedData; + //if (cachedData.Any()) return cachedData; var users = await repository.AsQueryable(); - await cacheProvider.SetAsync(cacheKey, users); + await cacheProvider.SetAsync(cacheKey, users, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes)); return users; } @@ -98,7 +97,7 @@ namespace Core.Thalos.Provider.Providers.Onboarding /// The User email. /// A representing /// the asynchronous execution of the service. - public async ValueTask GetUserByEmail(string? email, CancellationToken cancellationToken) + public async ValueTask GetUserByEmail(string email, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetUserByEmail", email); var cachedData = await cacheProvider.GetAsync(cacheKey); @@ -120,7 +119,7 @@ namespace Core.Thalos.Provider.Providers.Onboarding /// The User email. /// A representing /// the asynchronous execution of the service. - public async ValueTask ValidateUserExistence(string? email, CancellationToken cancellationToken) + public async ValueTask ValidateUserExistence(string email, CancellationToken cancellationToken) { var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetUserByEmail", email); var cachedData = await cacheProvider.GetAsync(cacheKey); @@ -143,13 +142,13 @@ namespace Core.Thalos.Provider.Providers.Onboarding /// /// Changes the status of the user. /// - /// The user identifier. + /// The user mongo identifier. /// The new status of the user. /// A representing /// the asynchronous execution of the service. - public async ValueTask ChangeUserStatus(string id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) + public async ValueTask ChangeUserStatus(string _id, Core.Blueprint.Mongo.StatusEnum newStatus, CancellationToken cancellationToken) { - var entity = await repository.FindByIdAsync(id); + var entity = await repository.FindByIdAsync(_id); entity.Status = newStatus; await repository.ReplaceOneAsync(entity); @@ -158,10 +157,10 @@ namespace Core.Thalos.Provider.Providers.Onboarding } /// - /// Updates a User by id. + /// Updates a User by _id. /// /// The User to be updated. - /// The User identifier. + /// The User mongo identifier. /// A representing /// the asynchronous execution of the service. public async ValueTask UpdateUser(UserAdapter entity, CancellationToken cancellationToken) @@ -174,7 +173,7 @@ namespace Core.Thalos.Provider.Providers.Onboarding /// /// Logs in the user. /// - /// The User identifier. + /// The User mongo identifier. /// A representing /// the asynchronous execution of the service. public async ValueTask LogInUser(string email, CancellationToken cancellationToken) @@ -318,19 +317,19 @@ namespace Core.Thalos.Provider.Providers.Onboarding User = new UserAdapter { Id = result["_id"]?.ToString() ?? "", - Guid = result.Contains("guid") && !result["guid"].IsBsonNull ? result["guid"].AsString : "", - Email = result.Contains("email") && !result["email"].IsBsonNull ? result["email"].AsString : "", - Name = result.Contains("name") && !result["name"].IsBsonNull ? result["name"].AsString : "", - MiddleName = result.Contains("middleName") && !result["middleName"].IsBsonNull ? result["middleName"].AsString : "", - LastName = result.Contains("lastName") && !result["lastName"].IsBsonNull ? result["lastName"].AsString : "", - DisplayName = result.Contains("displayName") && !result["displayName"].IsBsonNull ? result["displayName"].AsString : "", - RoleId = result.Contains("roleId") && !result["roleId"].IsBsonNull ? result["roleId"].ToString() : "", + Guid = result.Contains("guid") && !result["guid"].IsBsonNull ? result["guid"].AsString : string.Empty, + Email = result.Contains("email") && !result["email"].IsBsonNull ? result["email"].AsString : string.Empty, + Name = result.Contains("name") && !result["name"].IsBsonNull ? result["name"].AsString : string.Empty, + MiddleName = result.Contains("middleName") && !result["middleName"].IsBsonNull ? result["middleName"].AsString : string.Empty, + LastName = result.Contains("lastName") && !result["lastName"].IsBsonNull ? result["lastName"].AsString : string.Empty, + DisplayName = result.Contains("displayName") && !result["displayName"].IsBsonNull ? result["displayName"].AsString : string.Empty, + RoleId = result.Contains("roleId") && !result["roleId"].IsBsonNull ? result["roleId"].ToString() : string.Empty, LastLogIn = result.Contains("lastLogIn") && !result["lastLogIn"].IsBsonNull ? result["lastLogIn"].ToUniversalTime() : DateTime.MinValue, LastLogOut = result.Contains("lastLogOut") && !result["lastLogOut"].IsBsonNull ? result["lastLogOut"].ToUniversalTime() : DateTime.MinValue, CreatedAt = result.Contains("createdAt") && !result["createdAt"].IsBsonNull ? result["createdAt"].ToUniversalTime() : DateTime.MinValue, - CreatedBy = result.Contains("createdBy") && !result["createdBy"].IsBsonNull ? result["createdBy"].AsString : "", + CreatedBy = result.Contains("createdBy") && !result["createdBy"].IsBsonNull ? result["createdBy"].AsString : string.Empty, UpdatedAt = result.Contains("updatedAt") && !result["updatedAt"].IsBsonNull ? result["updatedAt"].ToUniversalTime() : DateTime.MinValue, - UpdatedBy = result.Contains("updatedBy") && !result["updatedBy"].IsBsonNull ? result["updatedBy"].AsString : "", + UpdatedBy = result.Contains("updatedBy") && !result["updatedBy"].IsBsonNull ? result["updatedBy"].AsString : string.Empty, Status = result.Contains("status") && !result["status"].IsBsonNull ? (Core.Blueprint.Mongo.StatusEnum)Enum.Parse(typeof(Core.Blueprint.Mongo.StatusEnum), result["status"].AsString) : Core.Blueprint.Mongo.StatusEnum.Inactive @@ -339,13 +338,13 @@ namespace Core.Thalos.Provider.Providers.Onboarding { Id = result.Contains("role") && result["role"].IsBsonDocument && result["role"].AsBsonDocument.Contains("_id") ? result["role"]["_id"]?.ToString() ?? "" - : "", + : string.Empty, Name = result.Contains("role") && result["role"].IsBsonDocument && result["role"].AsBsonDocument.Contains("name") ? result["role"]["name"]?.AsString ?? "" - : "", + : string.Empty, Description = result.Contains("role") && result["role"].IsBsonDocument && result["role"].AsBsonDocument.Contains("description") ? result["role"]["description"]?.AsString ?? "" - : "", + : string.Empty, Applications = result.Contains("role") && result["role"].IsBsonDocument && result["role"].AsBsonDocument.Contains("applications") && result["role"]["applications"].IsBsonArray @@ -389,12 +388,12 @@ namespace Core.Thalos.Provider.Providers.Onboarding result["role"].AsBsonDocument.Contains("createdBy") && !result["role"]["createdBy"].IsBsonNull ? result["role"]["createdBy"].AsString - : "", + : string.Empty, UpdatedBy = result.Contains("role") && result["role"].IsBsonDocument && result["role"].AsBsonDocument.Contains("updatedBy") && !result["role"]["updatedBy"].IsBsonNull ? result["role"]["updatedBy"].AsString - : "" + : string.Empty }, Permissions = result.Contains("permissions") && result["permissions"].IsBsonArray ? result["permissions"].AsBsonArray @@ -416,12 +415,12 @@ namespace Core.Thalos.Provider.Providers.Onboarding } /// - /// Deletes an User by id. + /// Deletes an User by _id. /// - /// The User identifier. + /// The User mongo identifier. /// A representing /// the asynchronous execution of the service. - public async ValueTask DeleteUser(string _id, CancellationToken cancellationToken) + public async ValueTask DeleteUser(string _id, CancellationToken cancellationToken) { var entity = await repository.DeleteOneAsync(doc => doc.Id == _id); diff --git a/Core.Thalos.Provider/ServiceCollectionExtensions.cs b/Core.Thalos.Provider/ServiceCollectionExtensions.cs index b1af15d..e7baf7b 100644 --- a/Core.Thalos.Provider/ServiceCollectionExtensions.cs +++ b/Core.Thalos.Provider/ServiceCollectionExtensions.cs @@ -24,6 +24,9 @@ namespace Core.Thalos.Provider services.AddScoped(); services.AddScoped>(); + services.AddScoped(); + services.AddScoped>(); + return services; } }