Add Tag CRUD
This commit is contained in:
		
							
								
								
									
										239
									
								
								Core.Inventory.Service.API/Controllers/TagController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										239
									
								
								Core.Inventory.Service.API/Controllers/TagController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,239 @@ | ||||
| using Asp.Versioning; | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.Application.UseCases.Tag.Input; | ||||
| using Core.Inventory.Application.UseCases.Tag.Ports; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace Core.Inventory.Service.API.Controllers | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all services and business rules related to <see cref="TagController"/>. | ||||
|     /// </summary> | ||||
|     [ApiVersion("1.0")] | ||||
|     [Route("api/v{api-version:apiVersion}/[controller]")] | ||||
|     [Produces("application/json")] | ||||
|     [ApiController] | ||||
|     [AllowAnonymous] | ||||
|     public class TagController : ControllerBase | ||||
|     { | ||||
|         private readonly IComponentHandler<GetTagRequest> getTagHandler; | ||||
|         private readonly IComponentHandler<GetAllTagsRequest> getAllTagsHandler; | ||||
|         private readonly IComponentHandler<GetAllTagsByListRequest> getAllTagsByListHandler; | ||||
|         private readonly IComponentHandler<CreateTagRequest> createTagHandler; | ||||
|         private readonly IComponentHandler<UpdateTagRequest> updateTagHandler; | ||||
|         private readonly IComponentHandler<ChangeTagStatusRequest> changeTagStatusHandler; | ||||
|         private readonly IComponentHandler<AddParentTagToTagRequest> addParentTagToTagHandler; | ||||
|         private readonly IComponentHandler<RemoveParentTagFromTagRequest> removeParentTagFromTagUserHandler; | ||||
|         private readonly ITagPort port; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Handles all services and business rules related to <see cref="TagController"/>. | ||||
|         /// </summary> | ||||
|         public TagController( | ||||
|             IComponentHandler<GetTagRequest> getTagHandler, | ||||
|             IComponentHandler<GetAllTagsRequest> getAllTagsHandler, | ||||
|             IComponentHandler<GetAllTagsByListRequest> getAllTagsByListHandler, | ||||
|             IComponentHandler<CreateTagRequest> createTagHandler, | ||||
|             IComponentHandler<UpdateTagRequest> updateTagHandler, | ||||
|             IComponentHandler<ChangeTagStatusRequest> changeTagStatusHandler, | ||||
|             IComponentHandler<AddParentTagToTagRequest> addParentTagToTagHandler, | ||||
|             IComponentHandler<RemoveParentTagFromTagRequest> removeParentTagFromTagUserHandler, | ||||
|             ITagPort port | ||||
|             ) | ||||
|         { | ||||
|             this.createTagHandler = createTagHandler; | ||||
|             this.updateTagHandler = updateTagHandler; | ||||
|             this.changeTagStatusHandler = changeTagStatusHandler; | ||||
|             this.getAllTagsHandler = getAllTagsHandler; | ||||
|             this.getTagHandler = getTagHandler; | ||||
|             this.getAllTagsByListHandler = getAllTagsByListHandler; | ||||
|             this.addParentTagToTagHandler = addParentTagToTagHandler; | ||||
|             this.removeParentTagFromTagUserHandler = removeParentTagFromTagUserHandler; | ||||
|             this.port = port; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the Tags. | ||||
|         /// </summary> | ||||
|         [HttpGet("GetAll")] | ||||
|         [ProducesResponseType(StatusCodes.Status200OK)] | ||||
|         [ProducesResponseType(StatusCodes.Status204NoContent)] | ||||
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||||
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] | ||||
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] | ||||
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)] | ||||
|         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||||
|         public async Task<IActionResult> GetAllTagsAsync(CancellationToken cancellationToken) | ||||
|         { | ||||
|             await getAllTagsHandler.ExecuteAsync(new GetAllTagsRequest { }, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|  | ||||
|         /// <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="412">Precondition failed if the request does not meet expected conditions.</response> | ||||
|         /// <response code="422">Unprocessable entity if the request cannot be processed.</response> | ||||
|         /// <response code="500">Internal server error if an unexpected error occurs.</response> | ||||
|         [HttpPost] | ||||
|         [Route("GetTagList")] | ||||
|         [ProducesResponseType(typeof(IEnumerable<TagAdapter>), 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> GetAllTagsByListAsync([FromBody] GetAllTagsByListRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|  | ||||
|             if (request == null || request.Tags == null || !request.Tags.Any()) | ||||
|             { | ||||
|                 return BadRequest("Tag identifiers are required."); | ||||
|             } | ||||
|  | ||||
|             await getAllTagsByListHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets the Tag by identifier. | ||||
|         /// </summary> | ||||
|         [HttpPost] | ||||
|         [Route("GetById")] | ||||
|         [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> GetTagById([FromBody] GetTagRequest request, CancellationToken cancellationToken) | ||||
|         { | ||||
|  | ||||
|             if (request.Id == null || !request.Id.Any()) | ||||
|             { | ||||
|                 return BadRequest("Invalid Tag Id"); | ||||
|             } | ||||
|  | ||||
|             await getTagHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Tag. | ||||
|         /// </summary> | ||||
|         [HttpPost("Create")] | ||||
|         [ProducesResponseType(StatusCodes.Status200OK)] | ||||
|         [ProducesResponseType(StatusCodes.Status204NoContent)] | ||||
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||||
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] | ||||
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] | ||||
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)] | ||||
|         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||||
|         public async Task<IActionResult> CreateTagAsync([FromBody] CreateTagRequest newTag, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             await createTagHandler.ExecuteAsync(newTag, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a full Tag by identifier. | ||||
|         /// </summary> | ||||
|         [HttpPut("Update")] | ||||
|         [ProducesResponseType(StatusCodes.Status200OK)] | ||||
|         [ProducesResponseType(StatusCodes.Status204NoContent)] | ||||
|         [ProducesResponseType(StatusCodes.Status401Unauthorized)] | ||||
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status412PreconditionFailed)] | ||||
|         [ProducesResponseType(typeof(Notification), StatusCodes.Status422UnprocessableEntity)] | ||||
|         [ProducesResponseType(StatusCodes.Status500InternalServerError)] | ||||
|         [ProducesResponseType(StatusCodes.Status400BadRequest)] | ||||
|         public async Task<IActionResult> UpdateTagAsync([FromBody] UpdateTagRequest request, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             await updateTagHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|  | ||||
|         /// <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> ChangeTagStatusAsync([FromBody] ChangeTagStatusRequest request, | ||||
|                                                                      CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (string.IsNullOrEmpty(request.Id)) { return BadRequest("Invalid Tag identifier"); } | ||||
|  | ||||
|             await changeTagStatusHandler.ExecuteAsync(request, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|  | ||||
|         /// <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) | ||||
|         { | ||||
|             if (string.IsNullOrEmpty(request.TagId)) { return BadRequest("Invalid tag identifier"); } | ||||
|             if (string.IsNullOrEmpty(request.ParentTagId)) { return BadRequest("Invalid parentTag identifier"); } | ||||
|              | ||||
|             await addParentTagToTagHandler.ExecuteAsync(request, cancellationToken); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|  | ||||
|         /// <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] RemoveParentTagFromTagRequest request, | ||||
|                                                                    CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (string.IsNullOrEmpty(request.TagId)) { return BadRequest("Invalid tag identifier"); } | ||||
|             if (string.IsNullOrEmpty(request.ParentTagId)) { return BadRequest("Invalid parentTag identifier"); } | ||||
|              | ||||
|             await removeParentTagFromTagUserHandler.ExecuteAsync(request, cancellationToken); | ||||
|  | ||||
|             return port.ViewModel; | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales