diff --git a/Core.Inventory.BFF.API/Controllers/TagTypeController.cs b/Core.Inventory.BFF.API/Controllers/TagTypeController.cs new file mode 100644 index 0000000..eb12c18 --- /dev/null +++ b/Core.Inventory.BFF.API/Controllers/TagTypeController.cs @@ -0,0 +1,203 @@ +using Asp.Versioning; +using Core.Adapters.Lib; +using Core.Inventory.External.Clients.Inventory; +using Core.Inventory.External.Clients.Inventory.Requests.TagType; +using Lib.Architecture.BuildingBlocks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using System.Text.Json; + +namespace Core.Inventory.BFF.API.Controllers +{ + /// + /// Handles all requests for TagType authentication. + /// + [ApiVersion("1.0")] + [Route("api/v{version:apiVersion}/[controller]")] + [Consumes("application/json")] + [Produces("application/json")] + [ApiController] + [AllowAnonymous] + public class TagTypeController(IInventoryServiceClient inventoryServiceClient, ILogger logger) : BaseController(logger) + { + /// + /// Gets all the TagTypes. + /// + [HttpGet("GetAll")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task GetAllTagTypesService(CancellationToken cancellationToken) + { + try + { + logger.LogInformation($"{nameof(GetAllTagTypesService)} - Request received - Payload: "); + + return await Handle(() => inventoryServiceClient.GetAllTagTypesService(new GetAllTagTypesRequest { }, cancellationToken)).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.LogError($"{nameof(GetAllTagTypesService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload"); + throw; + } + } + + /// + /// Gets all the TagTypes by TagType identifiers. + /// + /// The request containing the list of TagType identifiers. + /// Cancellation token for the asynchronous operation. + /// The representing the result of the service call. + /// The TagTypes found. + /// No content if no TagTypes are found. + /// Bad request if the TagType identifiers are missing or invalid. + /// Unauthorized if the user is not authenticated. + /// Internal server error if an unexpected error occurs. + [HttpPost("GetAllByList")] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task GetAllTagTypesByListAsync([FromBody] GetAllTagTypesByListRequest request, CancellationToken cancellationToken) + { + try + { + logger.LogInformation($"{nameof(GetAllTagTypesByListAsync)} - Request received - Payload: {request}"); + + if (request == null || request.TagTypes == null || !request.TagTypes.Any()) + { + return BadRequest("TagType identifiers are required."); + } + + return await Handle(() => inventoryServiceClient.GetAllTagTypesByListService(request, cancellationToken)).ConfigureAwait(false); + + } + catch (Exception ex) + { + logger.LogError(ex, $"{nameof(GetAllTagTypesByListAsync)} - An error occurred - {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload: {request}"); + return StatusCode(StatusCodes.Status500InternalServerError, "Internal server error"); + } + } + + + /// + /// Creates a new TagType. + /// + [HttpPost("Create")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task CreateTagTypeService(CreateTagTypeRequest newTagType, CancellationToken cancellationToken) + { + try + { + logger.LogInformation($"{nameof(CreateTagTypeService)} - Request received - Payload: {JsonSerializer.Serialize(newTagType)}"); + + if (newTagType == null) return BadRequest("Invalid TagType object"); + + if (string.IsNullOrEmpty(newTagType.TypeName)) return BadRequest("Invalid TagType name"); + + return await Handle(() => inventoryServiceClient.CreateTagTypeService(newTagType, cancellationToken)).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.LogError($"{nameof(CreateTagTypeService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newTagType)}"); + throw; + } + } + + /// + /// Gets the TagType by identifier. + /// + [HttpPost("GetById")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task GetTagTypeByIdService(GetTagTypeRequest request, CancellationToken cancellationToken) + { + try + { + logger.LogInformation($"{nameof(GetTagTypeByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); + + if (string.IsNullOrEmpty(request.Id)) return BadRequest("Invalid TagType identifier"); + + return await Handle(() => inventoryServiceClient.GetTagTypeByIdService(request, cancellationToken)).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.LogError($"{nameof(GetTagTypeByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); + throw; + } + } + + /// + /// Updates a full TagType by identifier. + /// + [HttpPut("Update")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task UpdateTagTypeService(UpdateTagTypeRequest newTagType, CancellationToken cancellationToken) + { + try + { + logger.LogInformation($"{nameof(UpdateTagTypeService)} - Request received - Payload: {JsonSerializer.Serialize(newTagType)}"); + + if (newTagType == null) return BadRequest("Invalid TagType object"); + + if (string.IsNullOrEmpty(newTagType.TypeName)) return BadRequest("Invalid TagType name"); + + return await Handle(() => inventoryServiceClient.UpdateTagTypeService(newTagType, cancellationToken)).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.LogError($"{nameof(UpdateTagTypeService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newTagType)}"); + throw; + } + } + + /// + /// Changes the status of the TagType. + /// + [HttpPatch] + [Route("ChangeStatus")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] + [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task ChangeTagTypeStatusService([FromBody] ChangeTagTypeStatusRequest request, CancellationToken cancellationToken) + { + try + { + logger.LogInformation($"{nameof(ChangeTagTypeStatusService)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); + + if (string.IsNullOrEmpty(request.Id)) { return BadRequest("Invalid TagType identifier"); } + + return await Handle(() => inventoryServiceClient.ChangeTagTypeStatusService(request, cancellationToken)).ConfigureAwait(false); + } + catch (Exception ex) + { + logger.LogError($"{nameof(ChangeTagTypeStatusService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); + throw; + } + } + + + } +} diff --git a/Core.Inventory.External/Clients/Inventory/IInventoryServiceClient.cs b/Core.Inventory.External/Clients/Inventory/IInventoryServiceClient.cs index 50529cc..b48023f 100644 --- a/Core.Inventory.External/Clients/Inventory/IInventoryServiceClient.cs +++ b/Core.Inventory.External/Clients/Inventory/IInventoryServiceClient.cs @@ -1,5 +1,6 @@ using Core.Adapters.Lib; using Core.Inventory.External.Clients.Inventory.Requests.Base; +using Core.Inventory.External.Clients.Inventory.Requests.TagType; using Core.Inventory.External.Clients.Inventory.Requests.Variant; using Refit; @@ -50,5 +51,27 @@ namespace Core.Inventory.External.Clients.Inventory Task>> GetAllVariantsAsync([Header("TrackingId")] string trackingId, CancellationToken cancellationToken = default); #endregion + + #region TagType + + [Post("/api/v1/TagType/Create")] + Task> CreateTagTypeService([Header("TrackingId")][Body] CreateTagTypeRequest request, CancellationToken cancellationToken = default); + + [Post("/api/v1/TagType/GetById")] + Task> GetTagTypeByIdService([Header("TrackingId")][Body] GetTagTypeRequest request, CancellationToken cancellationToken = default); + + [Get("/api/v1/TagType/GetAll")] + Task>> GetAllTagTypesService([Header("TrackingId")][Body] GetAllTagTypesRequest request, CancellationToken cancellationToken = default); + + [Post("/api/v1/TagType/GetTagTypeList")] + Task>> GetAllTagTypesByListService([Header("TrackingId")][Body] GetAllTagTypesByListRequest request, CancellationToken cancellationToken = default); + + [Put("/api/v1/TagType/Update")] + Task> UpdateTagTypeService([Header("TrackingId")][Body] UpdateTagTypeRequest request, CancellationToken cancellationToken = default); + + [Patch("/api/v1/TagType/ChangeStatus")] + Task> ChangeTagTypeStatusService([Header("TrackingId")][Body] ChangeTagTypeStatusRequest request, CancellationToken cancellationToken = default); + + #endregion } } diff --git a/Core.Inventory.External/Clients/Inventory/Requests/TagType/ChangeTagTypeStatusRequest.cs b/Core.Inventory.External/Clients/Inventory/Requests/TagType/ChangeTagTypeStatusRequest.cs new file mode 100644 index 0000000..6bf0ce9 --- /dev/null +++ b/Core.Inventory.External/Clients/Inventory/Requests/TagType/ChangeTagTypeStatusRequest.cs @@ -0,0 +1,10 @@ +using Core.Blueprint.Mongo; + +namespace Core.Inventory.External.Clients.Inventory.Requests.TagType +{ + public class ChangeTagTypeStatusRequest + { + public string Id { get; set; } + public StatusEnum Status { get; set; } + } +} diff --git a/Core.Inventory.External/Clients/Inventory/Requests/TagType/CreateTagTypeRequest.cs b/Core.Inventory.External/Clients/Inventory/Requests/TagType/CreateTagTypeRequest.cs new file mode 100644 index 0000000..2260a9f --- /dev/null +++ b/Core.Inventory.External/Clients/Inventory/Requests/TagType/CreateTagTypeRequest.cs @@ -0,0 +1,10 @@ +namespace Core.Inventory.External.Clients.Inventory.Requests.TagType +{ + public class CreateTagTypeRequest + { + public string TenantId { get; set; } = null!; + public string TypeName { get; set; } = null!; + public int Level { get; set; } + public string ParentTypeId { get; set; } = null!; + } +} diff --git a/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetAllTagTypesByListRequest.cs b/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetAllTagTypesByListRequest.cs new file mode 100644 index 0000000..68534fe --- /dev/null +++ b/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetAllTagTypesByListRequest.cs @@ -0,0 +1,7 @@ +namespace Core.Inventory.External.Clients.Inventory.Requests.TagType +{ + public class GetAllTagTypesByListRequest + { + public string[] TagTypes { get; set; } + } +} diff --git a/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetAllTagTypesRequest.cs b/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetAllTagTypesRequest.cs new file mode 100644 index 0000000..9f40c34 --- /dev/null +++ b/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetAllTagTypesRequest.cs @@ -0,0 +1,6 @@ +namespace Core.Inventory.External.Clients.Inventory.Requests.TagType +{ + public class GetAllTagTypesRequest + { + } +} diff --git a/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetTagTypeRequest.cs b/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetTagTypeRequest.cs new file mode 100644 index 0000000..f172711 --- /dev/null +++ b/Core.Inventory.External/Clients/Inventory/Requests/TagType/GetTagTypeRequest.cs @@ -0,0 +1,7 @@ +namespace Core.Inventory.External.Clients.Inventory.Requests.TagType +{ + public class GetTagTypeRequest + { + public string Id { get; set; } + } +} diff --git a/Core.Inventory.External/Clients/Inventory/Requests/TagType/UpdateTagTypeRequest.cs b/Core.Inventory.External/Clients/Inventory/Requests/TagType/UpdateTagTypeRequest.cs new file mode 100644 index 0000000..8ba3a56 --- /dev/null +++ b/Core.Inventory.External/Clients/Inventory/Requests/TagType/UpdateTagTypeRequest.cs @@ -0,0 +1,14 @@ +using Core.Blueprint.Mongo; + +namespace Core.Inventory.External.Clients.Inventory.Requests.TagType +{ + public class UpdateTagTypeRequest + { + public string Id { get; set; } = null!; + public string TenantId { get; set; } = null!; + public string TypeName { get; set; } = null!; + public int Level { get; set; } + public string ParentTypeId { get; set; } = null!; + public StatusEnum Status { get; set; } + } +} diff --git a/Core.Inventory.External/Core.Inventory.External.csproj b/Core.Inventory.External/Core.Inventory.External.csproj index c7d4c05..fdaf460 100644 --- a/Core.Inventory.External/Core.Inventory.External.csproj +++ b/Core.Inventory.External/Core.Inventory.External.csproj @@ -7,7 +7,7 @@ - +