Add Tag CRUD
This commit is contained in:
		
							
								
								
									
										190
									
								
								Core.Inventory.DAL.API/Controllers/TagController.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										190
									
								
								Core.Inventory.DAL.API/Controllers/TagController.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,190 @@ | ||||
| using Asp.Versioning; | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Blueprint.Logging; | ||||
| using Core.Blueprint.Mongo; | ||||
| using Core.Inventory.Domain.Contexts.Inventory.Request; | ||||
| using Core.Inventory.Provider.Contracts; | ||||
| using Microsoft.AspNetCore.Authorization; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace Core.Inventory.DAL.API.Controllers | ||||
| { | ||||
|     /// <summary> | ||||
|     /// Handles all requests for Tag authentication. | ||||
|     /// </summary> | ||||
|     [ApiVersion(MimeTypes.ApplicationVersion)] | ||||
|     [Route("api/v{api-version:apiVersion}/[controller]")] | ||||
|     [Produces(MimeTypes.ApplicationJson)] | ||||
|     [Consumes(MimeTypes.ApplicationJson)] | ||||
|     [ApiController] | ||||
|     [AllowAnonymous] | ||||
|     public class TagController(ITagProvider service) : ControllerBase | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Gets all the Tags. | ||||
|         /// </summary> | ||||
|         /// <returns>The <see cref="IEnumerable{TagAdapter}"/> found entities.</returns> | ||||
|         /// <response code="200">The Tags found.</response> | ||||
|         /// <response code="404">The Tags not found error.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpGet] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(IEnumerable<TagAdapter>), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetAllTagsAsync(CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.GetAllTags(cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets all the Tags by Tag identifiers. | ||||
|         /// </summary> | ||||
|         /// <param name="Tags">The list of Tag identifiers.</param> | ||||
|         /// <returns>The <see cref="IEnumerable{TagAdapter}"/> found entities.</returns> | ||||
|         /// <response code="200">The Tags found.</response> | ||||
|         /// <response code="404">The Tags not found error.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPost] | ||||
|         [Route("GetTagList")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(IEnumerable<TagAdapter>), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetAllTagsByList([FromBody] string[] tags, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (tags == null || !tags.Any()) | ||||
|             { | ||||
|                 return BadRequest("Tag identifiers are required."); | ||||
|             } | ||||
|  | ||||
|             var result = await service.GetAllTagsByList(tags, cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Gets the Tag by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Tag identifier.</param> | ||||
|         /// <returns>The <see cref="TagAdapter"/> found entity.</returns> | ||||
|         /// <response code="200">The Tag found.</response> | ||||
|         /// <response code="404">The Tag not found error.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpGet] | ||||
|         [Route("{id}")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> GetTagByIdAsync([FromRoute] string id, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.GetTagById(id, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             if (result == null) | ||||
|             { | ||||
|                 return NotFound("Entity not found"); | ||||
|             } | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Creates a new Tag. | ||||
|         /// </summary> | ||||
|         /// <param name="newTag">The Tag to be added.</param> | ||||
|         /// <returns>The <see cref="TagAdapter"/> created entity.</returns> | ||||
|         /// <response code="201">The Tag created.</response> | ||||
|         /// <response code="422">The Tag could not be created.</response> | ||||
|         /// <response code="500">The service internal e|ror.</response> | ||||
|         [HttpPost] | ||||
|         [ProducesResponseType(typeof(TagAdapter), StatusCodes.Status201Created)] | ||||
|         public async Task<IActionResult> CreateTagAsync([FromBody] TagRequest newTag, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.CreateTag(newTag, cancellationToken).ConfigureAwait(false); | ||||
|             return Created("CreatedWithIdAsync", result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Updates a full Tag by identifier. | ||||
|         /// </summary> | ||||
|         /// <param name="entity">The Tag to update.</param> | ||||
|         /// <param name="id">The Tag identifier.</param> | ||||
|         /// <returns>The <see cref="TagAdapter"/> updated entity.</returns> | ||||
|         /// <response code="200">The Tag updated.</response> | ||||
|         /// <response code="404">The Tag not found.</response> | ||||
|         /// <response code="422">The Tag could not be updated.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPut] | ||||
|         [Route("{id}")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> UpdateTagAsync([FromRoute] string id, TagAdapter entity, CancellationToken cancellationToken) | ||||
|         { | ||||
|             if (id != entity.Id?.ToString()) | ||||
|             { | ||||
|                 return BadRequest("Tag ID mismatch"); | ||||
|             } | ||||
|  | ||||
|             var result = await service.UpdateTag(entity, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Changes the status of the Tag. | ||||
|         /// </summary> | ||||
|         /// <param name="id">The Tag identifier.</param> | ||||
|         /// <param name="newStatus">The new status of the Tag.</param> | ||||
|         /// <returns>The <see cref="TagAdapter"/> updated entity.</returns> | ||||
|         /// <response code="200">The Tag updates.</response> | ||||
|         /// <response code="404">The Tag not found.</response> | ||||
|         /// <response code="422">The Tag could not be deleted.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPatch] | ||||
|         [Route("{id}/{newStatus}/ChangeStatus")] | ||||
|         [Consumes(MimeTypes.ApplicationJson)] | ||||
|         [Produces(MimeTypes.ApplicationJson)] | ||||
|         [ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> ChangeTagStatus([FromRoute] string id, [FromRoute] StatusEnum newStatus, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.ChangeTagStatus(id, newStatus, cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds a parentTag to the tag. | ||||
|         /// </summary> | ||||
|         /// <param name="tagId">The tag identifier.</param> | ||||
|         /// <param name="parentTagId">The parentTag identifier to add.</param> | ||||
|         /// <returns>The updated <see cref="TagAdapter"/> entity.</returns> | ||||
|         /// <response code="200">The tag with the updated parentTags.</response> | ||||
|         /// <response code="404">The tag or parentTag not found.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpPost] | ||||
|         [Route("{tagId}/ParentTags/{parentTagId}/Add")] | ||||
|         [ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> AddParentTagAsync([FromRoute] string tagId, [FromRoute] string parentTagId, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.AddParentTag(tagId, parentTagId, cancellationToken).ConfigureAwait(false); | ||||
|             return Ok(result); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Remove a parentTag to the tag. | ||||
|         /// </summary> | ||||
|         /// <param name="tagId">The tag identifier.</param> | ||||
|         /// <param name="parentTagId">The parentTag identifier to remove.</param> | ||||
|         /// <returns>The updated <see cref="TagAdapter"/> entity.</returns> | ||||
|         /// <response code="200">The tag with the updated parentTags.</response> | ||||
|         /// <response code="404">The tag or parentTag not found.</response> | ||||
|         /// <response code="500">The service internal error.</response> | ||||
|         [HttpDelete] | ||||
|         [Route("{tagId}/ParentTags/{parentTagId}/Remove")] | ||||
|         [ProducesResponseType(typeof(TagAdapter), StatusCodes.Status200OK)] | ||||
|         public async Task<IActionResult> RemoveParentTagAsync([FromRoute] string tagId, [FromRoute] string parentTagId, CancellationToken cancellationToken) | ||||
|         { | ||||
|             var result = await service.RemoveParentTag(tagId, parentTagId, cancellationToken).ConfigureAwait(false); ; | ||||
|             return Ok(result); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales