Add Tag CRUD #2
							
								
								
									
										261
									
								
								Core.Inventory.BFF.API/Controllers/TagController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										261
									
								
								Core.Inventory.BFF.API/Controllers/TagController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,261 @@ | ||||
| using Asp.Versioning; | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.External.Clients.Inventory; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Tag; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
| using System.Text.Json; | ||||
|  | ||||
| namespace Core.Inventory.BFF.API.Controllers | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all requests for Tag authentication. | ||||
|     /// </summary> | ||||
|     [ApiVersion("1.0")] | ||||
|     [Route("api/v{version:apiVersion}/[controller]")] | ||||
|     [Consumes("application/json")] | ||||
|     [Produces("application/json")] | ||||
|     [ApiController] | ||||
|     [AllowAnonymous] | ||||
|     public class TagController(IInventoryServiceClient inventoryServiceClient, ILogger<TagController> logger) : BaseController(logger) | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Gets all the Tags. | ||||
|         /// </summary> | ||||
|         [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<IActionResult> GetAllTagsService(CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetAllTagsService)} - Request received - Payload: "); | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.GetAllTagsService(new GetAllTagsRequest { }, cancellationToken)).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError($"{nameof(GetAllTagsService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the Tags by Tag identifiers. | ||||
|         /// </summary> | ||||
|         /// <param name="request">The request containing the list of Tag identifiers.</param> | ||||
|         /// <param name="cancellationToken">Cancellation token for the asynchronous operation.</param> | ||||
|         /// <returns>The <see cref="IActionResult"/> representing the result of the service call.</returns> | ||||
|         /// <response code="200">The Tags found.</response> | ||||
|         /// <response code="204">No content if no Tags are found.</response> | ||||
|         /// <response code="400">Bad request if the Tag identifiers are missing or invalid.</response> | ||||
|         /// <response code="401">Unauthorized if the user is not authenticated.</response> | ||||
|         /// <response code="500">Internal server error if an unexpected error occurs.</response> | ||||
|         [HttpPost("GetAllByList")] | ||||
|         [ProducesResponseType(typeof(IEnumerable<TagAdapter>), StatusCodes.Status200OK)] | ||||
|         [ProducesResponseType(StatusCodes.Status204NoContent)] | ||||
|         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||||
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||||
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)] | ||||
|         public async Task<IActionResult> GetAllTagsByListAsync([FromBody] GetAllTagsByListRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetAllTagsByListAsync)} - Request received - Payload: {request}"); | ||||
|  | ||||
|                 if (request == null || request.Tags == null || !request.Tags.Any()) | ||||
|                 { | ||||
|                     return BadRequest("Tag identifiers are required."); | ||||
|                 } | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.GetAllTagsByListService(request, cancellationToken)).ConfigureAwait(false); | ||||
|  | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError(ex, $"{nameof(GetAllTagsByListAsync)} - An error occurred - {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload: {request}"); | ||||
|                 return StatusCode(StatusCodes.Status500InternalServerError, "Internal server error"); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Tag. | ||||
|         /// </summary> | ||||
|         [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<IActionResult> CreateTagService(CreateTagRequest newTag, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(CreateTagService)} - Request received - Payload: {JsonSerializer.Serialize(newTag)}"); | ||||
|  | ||||
|                 if (newTag == null) return BadRequest("Invalid Tag object"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newTag.TagName)) return BadRequest("Invalid Tag name"); | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.CreateTagService(newTag, cancellationToken)).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError($"{nameof(CreateTagService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newTag)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets the Tag by identifier. | ||||
|         /// </summary> | ||||
|         [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<IActionResult> GetTagByIdService(GetTagRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(GetTagByIdService)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(request.Id)) return BadRequest("Invalid Tag identifier"); | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.GetTagByIdService(request, cancellationToken)).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError($"{nameof(GetTagByIdService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a full Tag by identifier. | ||||
|         /// </summary> | ||||
|         [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<IActionResult> UpdateTagService(UpdateTagRequest newTag, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(UpdateTagService)} - Request received - Payload: {JsonSerializer.Serialize(newTag)}"); | ||||
|  | ||||
|                 if (newTag == null) return BadRequest("Invalid Tag object"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(newTag.TagName)) return BadRequest("Invalid Tag name"); | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.UpdateTagService(newTag, cancellationToken)).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError($"{nameof(UpdateTagService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(newTag)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the Tag. | ||||
|         /// </summary> | ||||
|         [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<IActionResult> ChangeTagStatusService([FromBody] ChangeTagStatusRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(ChangeTagStatusService)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(request.Id)) { return BadRequest("Invalid Tag identifier"); } | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.ChangeTagStatusService(request, cancellationToken)).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError($"{nameof(ChangeTagStatusService)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds a parentTag to the tag. | ||||
|         /// </summary> | ||||
|         [HttpPost] | ||||
|         [Route("AddParentTag")] | ||||
|         [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<IActionResult> AddParentTagAsync([FromBody] AddParentTagToTagRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(AddParentTagAsync)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(request.TagId)) { return BadRequest("Invalid tag identifier"); } | ||||
|                 if (string.IsNullOrEmpty(request.ParentTagId)) { return BadRequest("Invalid parentTag identifier"); } | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.AddParentTagAsync(request, cancellationToken)).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError($"{nameof(AddParentTagAsync)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Remove a parentTag to the tag. | ||||
|         /// </summary> | ||||
|         [HttpDelete] | ||||
|         [Route("RemoveParentTag")] | ||||
|         [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<IActionResult> RemoveParentTagAsync([FromBody] RemoveParentTagFromTag request, CancellationToken cancellationToken) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 logger.LogInformation($"{nameof(RemoveParentTagAsync)} - Request received - Payload: {JsonSerializer.Serialize(request)}"); | ||||
|  | ||||
|                 if (string.IsNullOrEmpty(request.TagId)) { return BadRequest("Invalid tag identifier"); } | ||||
|                 if (string.IsNullOrEmpty(request.ParentTagId)) { return BadRequest("Invalid parentTag identifier"); } | ||||
|  | ||||
|                 return await Handle(() => inventoryServiceClient.RemoveParentTagAsync(request, cancellationToken)).ConfigureAwait(false); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 logger.LogError($"{nameof(RemoveParentTagAsync)} - An Error Occurred- {ex.Message} - {ex.InnerException} - {ex.StackTrace} - with payload {JsonSerializer.Serialize(request)}"); | ||||
|                 throw; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,5 +1,6 @@ | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Base; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Tag; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.TagType; | ||||
| using Core.Inventory.External.Clients.Inventory.Requests.Variant; | ||||
| using Refit; | ||||
| @@ -73,5 +74,33 @@ namespace Core.Inventory.External.Clients.Inventory | ||||
|         Task<ApiResponse<TagTypeAdapter>> ChangeTagTypeStatusService([Header("TrackingId")][Body] ChangeTagTypeStatusRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         #endregion | ||||
|  | ||||
|         #region Tag | ||||
|  | ||||
|         [Post("/api/v1/Tag/Create")] | ||||
|         Task<ApiResponse<TagAdapter>> CreateTagService([Header("TrackingId")][Body] CreateTagRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/Tag/GetById")] | ||||
|         Task<ApiResponse<TagAdapter>> GetTagByIdService([Header("TrackingId")][Body] GetTagRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Get("/api/v1/Tag/GetAll")] | ||||
|         Task<ApiResponse<IEnumerable<TagAdapter>>> GetAllTagsService([Header("TrackingId")][Body] GetAllTagsRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/Tag/GetTagList")] | ||||
|         Task<ApiResponse<IEnumerable<TagAdapter>>> GetAllTagsByListService([Header("TrackingId")][Body] GetAllTagsByListRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Put("/api/v1/Tag/Update")] | ||||
|         Task<ApiResponse<TagAdapter>> UpdateTagService([Header("TrackingId")][Body] UpdateTagRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Patch("/api/v1/Tag/ChangeStatus")] | ||||
|         Task<ApiResponse<TagAdapter>> ChangeTagStatusService([Header("TrackingId")][Body] ChangeTagStatusRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Post("/api/v1/Tag/AddParentTag")] | ||||
|         Task<ApiResponse<TagAdapter>> AddParentTagAsync([Header("TrackingId")][Body] AddParentTagToTagRequest request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         [Delete("/api/v1/Tag/RemoveParentTag")] | ||||
|         Task<ApiResponse<TagAdapter>> RemoveParentTagAsync([Header("TrackingId")][Body] RemoveParentTagFromTag request, CancellationToken cancellationToken = default); | ||||
|  | ||||
|         #endregion | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,8 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class AddParentTagToTagRequest | ||||
|     { | ||||
|         public string TagId { get; set; } | ||||
|         public string ParentTagId { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,10 @@ | ||||
| using Core.Blueprint.Mongo; | ||||
|  | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class ChangeTagStatusRequest | ||||
|     { | ||||
|         public string Id { get; set; } | ||||
|         public StatusEnum Status { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,13 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class CreateTagRequest | ||||
|     { | ||||
|         public string TenantId { get; set; } = null!; | ||||
|         public string TagName { get; set; } = null!; | ||||
|         public string TypeId { get; set; } = null!; | ||||
|         public string[] ParentTagId { get; set; } = null!; | ||||
|         public string Slug { get; set; } = null!; | ||||
|         public int DisplayOrder { get; set; } | ||||
|         public string Icon { get; set; } = null!; | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class GetAllTagsByListRequest | ||||
|     { | ||||
|         public string[] Tags { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,6 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class GetAllTagsRequest | ||||
|     { | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,7 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class GetTagRequest | ||||
|     { | ||||
|         public string Id { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,8 @@ | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class RemoveParentTagFromTag | ||||
|     { | ||||
|         public string TagId { get; set; } | ||||
|         public string ParentTagId { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,17 @@ | ||||
| using Core.Blueprint.Mongo; | ||||
|  | ||||
| namespace Core.Inventory.External.Clients.Inventory.Requests.Tag | ||||
| { | ||||
|     public class UpdateTagRequest | ||||
|     { | ||||
|         public string Id { get; set; } = null!; | ||||
|         public string TenantId { get; set; } = null!; | ||||
|         public string TagName { get; set; } = null!; | ||||
|         public string TypeId { get; set; } = null!; | ||||
|         public string[] ParentTagId { get; set; } = null!; | ||||
|         public string Slug { get; set; } = null!; | ||||
|         public int DisplayOrder { get; set; } | ||||
|         public string Icon { get; set; } = null!; | ||||
|         public StatusEnum Status { get; set; } | ||||
|     } | ||||
| } | ||||
| @@ -7,7 +7,7 @@ | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <PackageReference Include="Adapters.Lib" Version="1.0.9" /> | ||||
|     <PackageReference Include="Adapters.Lib" Version="1.0.10" /> | ||||
|     <PackageReference Include="BuildingBlocks.Library" Version="1.0.0" /> | ||||
|     <PackageReference Include="Refit" Version="8.0.0" /> | ||||
|   </ItemGroup> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user