Add TagType CRUD
This commit is contained in:
		| @@ -0,0 +1,19 @@ | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.Application.UseCases.TagType.Ports; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using Microsoft.AspNetCore.Mvc; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Adapter | ||||
| { | ||||
|     public class TagTypePort : BasePresenter, ITagTypePort | ||||
|     { | ||||
|         public void Success(TagTypeAdapter output) | ||||
|         { | ||||
|             ViewModel = new OkObjectResult(output); | ||||
|         } | ||||
|         public void Success(List<TagTypeAdapter> output) | ||||
|         { | ||||
|             ViewModel = new OkObjectResult(output); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,16 @@ | ||||
| using Core.Blueprint.Mongo; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Input | ||||
| { | ||||
|     public class ChangeTagTypeStatusRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string Id { get; set; } | ||||
|         public StatusEnum Status { get; set; } | ||||
|  | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return Id != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,17 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Input | ||||
| { | ||||
|     public class CreateTagTypeRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string TenantId { get; set; } = null!; | ||||
|         public string TypeName { get; set; } = null!; | ||||
|         public int Level { get; set; } | ||||
|         public string ParentTypeId { get; set; } = null!; | ||||
|  | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return TypeName != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Input | ||||
| { | ||||
|     public class GetAllTagTypesByListRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string[] TagTypes { get; set; } | ||||
|  | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return TagTypes != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,12 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Input | ||||
| { | ||||
|     public class GetAllTagTypesRequest : ICommand | ||||
|     { | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return true; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,13 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Input | ||||
| { | ||||
|     public class GetTagTypeRequest : Notificator, ICommand | ||||
|     { | ||||
|         public string Id { get; set; } | ||||
|         public bool Validate() | ||||
|         { | ||||
|             return Id != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,22 @@ | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Input | ||||
| { | ||||
|     public class UpdateTagTypeRequest : Notificator, ICommand | ||||
|     { | ||||
|         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 bool Validate() | ||||
|         { | ||||
|             return Id != null; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Core.Adapters.Lib; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Ports | ||||
| { | ||||
|     public interface ITagTypePort : IBasePort, | ||||
|         ICommandSuccessPort<TagTypeAdapter>, | ||||
|         ICommandSuccessPort<List<TagTypeAdapter>>, | ||||
|         INoContentPort, IBusinessErrorPort, ITimeoutPort, IValidationErrorPort, | ||||
|         INotFoundPort, IForbiddenPort, IUnauthorizedPort, IInternalServerErrorPort, | ||||
|         IBadRequestPort | ||||
|     { | ||||
|     } | ||||
| } | ||||
							
								
								
									
										214
									
								
								Core.Inventory.Application/UseCases/TagType/TagTypeHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										214
									
								
								Core.Inventory.Application/UseCases/TagType/TagTypeHandler.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,214 @@ | ||||
| using Core.Adapters.Lib; | ||||
| using Core.Inventory.Application.UseCases.TagType.Input; | ||||
| using Core.Inventory.Application.UseCases.TagType.Ports; | ||||
| using Core.Inventory.External.Clients; | ||||
| using Core.Inventory.External.Clients.Requests; | ||||
| using FluentValidation; | ||||
| using Lib.Architecture.BuildingBlocks; | ||||
| using Lib.Architecture.BuildingBlocks.Helpers; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType | ||||
| { | ||||
|     public class TagTypeHandler : | ||||
|         IComponentHandler<ChangeTagTypeStatusRequest>, | ||||
|         IComponentHandler<GetAllTagTypesRequest>, | ||||
|         IComponentHandler<GetAllTagTypesByListRequest>, | ||||
|         IComponentHandler<UpdateTagTypeRequest>, | ||||
|         IComponentHandler<GetTagTypeRequest>, | ||||
|         IComponentHandler<CreateTagTypeRequest> | ||||
|     { | ||||
|         private readonly ITagTypePort _port; | ||||
|         private readonly IValidator<ChangeTagTypeStatusRequest> _changeTagTypeStatusValidator; | ||||
|         private readonly IValidator<CreateTagTypeRequest> _registerTagTypeValidator; | ||||
|         private readonly IValidator<UpdateTagTypeRequest> _updateTagTypeValidator; | ||||
|         private readonly IValidator<GetAllTagTypesByListRequest> _TagTypesByListValidator; | ||||
|         private readonly IInventoryServiceClient _inventoryServiceClient; | ||||
|  | ||||
|         public TagTypeHandler( | ||||
|             ITagTypePort port, | ||||
|             IValidator<ChangeTagTypeStatusRequest> changeTagTypeStatusValidator, | ||||
|             IValidator<CreateTagTypeRequest> registerTagTypeValidator, | ||||
|             IValidator<UpdateTagTypeRequest> updateTagTypeValidator, | ||||
|             IValidator<GetAllTagTypesByListRequest> TagTypesByListValidator, | ||||
|             IInventoryServiceClient inventoryDALService) | ||||
|         { | ||||
|             _port = port ?? throw new ArgumentNullException(nameof(port)); | ||||
|             _changeTagTypeStatusValidator = changeTagTypeStatusValidator ?? throw new ArgumentNullException(nameof(changeTagTypeStatusValidator)); | ||||
|             _registerTagTypeValidator = registerTagTypeValidator ?? throw new ArgumentNullException(nameof(registerTagTypeValidator)); | ||||
|             _updateTagTypeValidator = updateTagTypeValidator ?? throw new ArgumentNullException(nameof(updateTagTypeValidator)); | ||||
|             _inventoryServiceClient = inventoryDALService ?? throw new ArgumentNullException(nameof(inventoryDALService)); | ||||
|             _TagTypesByListValidator = TagTypesByListValidator ?? throw new ArgumentNullException(nameof(TagTypesByListValidator)); | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(GetTagTypeRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 var result = await _inventoryServiceClient.GetTagTypeByIdAsync(command.Id, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|                 if (result == null) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _port.Success(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(GetAllTagTypesRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 var _result = await _inventoryServiceClient.GetAllTagTypesAsync().ConfigureAwait(false); | ||||
|                 if (!_result.Any()) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|                 _port.Success(_result.ToList()); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(GetAllTagTypesByListRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 if (!command.IsValid(_TagTypesByListValidator)) | ||||
|                 { | ||||
|                     _port.ValidationErrors(command.Notifications); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var _result = await _inventoryServiceClient.GetAllTagTypesByListAsync(command.TagTypes, cancellationToken).ConfigureAwait(false); | ||||
|                 if (!_result.Any()) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|                 _port.Success(_result.ToList()); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(ChangeTagTypeStatusRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 if (!command.IsValid(_changeTagTypeStatusValidator)) | ||||
|                 { | ||||
|                     _port.ValidationErrors(command.Notifications); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var result = await _inventoryServiceClient.ChangeStatusTagTypeAsync(command.Id, command.Status, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|                 if (result == null) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _port.Success(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(CreateTagTypeRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 if (!command.IsValid(_registerTagTypeValidator)) | ||||
|                 { | ||||
|                     _port.ValidationErrors(command.Notifications); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var request = new TagTypeRequest | ||||
|                 { | ||||
|                     TenantId = command.TenantId, | ||||
|                     TypeName = command.TypeName, | ||||
|                     Level = command.Level, | ||||
|                     ParentTypeId = command.ParentTypeId, | ||||
|                 }; | ||||
|  | ||||
|                 var result = await _inventoryServiceClient.CreateTagTypeAsync(request, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|                 if (result == null) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _port.Success(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public async ValueTask ExecuteAsync(UpdateTagTypeRequest command, CancellationToken cancellationToken = default) | ||||
|         { | ||||
|             try | ||||
|             { | ||||
|                 ArgumentNullException.ThrowIfNull(command); | ||||
|  | ||||
|                 if (!command.IsValid(_updateTagTypeValidator)) | ||||
|                 { | ||||
|                     _port.ValidationErrors(command.Notifications); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 var request = new TagTypeAdapter | ||||
|                 { | ||||
|                     Id = command.Id, | ||||
|                     TenantId = command.TenantId, | ||||
|                     TypeName = command.TypeName, | ||||
|                     Level = command.Level, | ||||
|                     ParentTypeId = command.ParentTypeId | ||||
|                 }; | ||||
|  | ||||
|                 string id = command.Id; | ||||
|  | ||||
|                 var result = await _inventoryServiceClient.UpdateTagTypeAsync(request, id, cancellationToken).ConfigureAwait(false); | ||||
|  | ||||
|                 if (result == null) | ||||
|                 { | ||||
|                     _port.NoContentSuccess(); | ||||
|                     return; | ||||
|                 } | ||||
|  | ||||
|                 _port.Success(result); | ||||
|             } | ||||
|             catch (Exception ex) | ||||
|             { | ||||
|                 ApiResponseHelper.EvaluatePort(ex, _port); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Core.Inventory.Application.UseCases.TagType.Input; | ||||
| using FluentValidation; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Validator | ||||
| { | ||||
|     public class ChangeTagTypeStatusValidator : AbstractValidator<ChangeTagTypeStatusRequest> | ||||
|     { | ||||
|         public ChangeTagTypeStatusValidator() | ||||
|         { | ||||
|             RuleFor(i => i.Id).NotEmpty().NotNull().OverridePropertyName(x => x.Id).WithName("TagType ID").WithMessage("TagType ID is Obligatory."); | ||||
|             RuleFor(i => i.Status).NotNull().OverridePropertyName(x => x.Status).WithName("Status").WithMessage("Status is Obligatory."); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Core.Inventory.Application.UseCases.TagType.Input; | ||||
| using FluentValidation; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Validator | ||||
| { | ||||
|     public class CreateTagTypeValidator : AbstractValidator<CreateTagTypeRequest> | ||||
|     { | ||||
|         public CreateTagTypeValidator() | ||||
|         { | ||||
|             RuleFor(i => i.TypeName).NotEmpty().NotNull().OverridePropertyName(x => x.TypeName).WithName("TagType Name").WithMessage("TagType Name is Obligatory."); | ||||
|             RuleFor(i => i.Level).NotEmpty().NotNull().OverridePropertyName(x => x.Level).WithName("TagType Level").WithMessage("TagType Level is Obligatory."); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Core.Inventory.Application.UseCases.TagType.Input; | ||||
| using FluentValidation; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Validator | ||||
| { | ||||
|     public class GetAllTagTypesByListValidator : AbstractValidator<GetAllTagTypesByListRequest> | ||||
|     { | ||||
|         public GetAllTagTypesByListValidator() | ||||
|         { | ||||
|             RuleFor(i => i.TagTypes).NotEmpty().NotNull().OverridePropertyName(x => x.TagTypes).WithName("TagTypes").WithMessage("TagTypes are Obligatory."); | ||||
|         } | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,14 @@ | ||||
| using Core.Inventory.Application.UseCases.TagType.Input; | ||||
| using FluentValidation; | ||||
|  | ||||
| namespace Core.Inventory.Application.UseCases.TagType.Validator | ||||
| { | ||||
|     public class UpdateTagTypeValidator : AbstractValidator<UpdateTagTypeRequest> | ||||
|     { | ||||
|         public UpdateTagTypeValidator() | ||||
|         { | ||||
|             RuleFor(i => i.TypeName).NotEmpty().NotNull().OverridePropertyName(x => x.TypeName).WithName("TagType Name").WithMessage("TagType Name is Obligatory."); | ||||
|             RuleFor(i => i.Level).NotEmpty().NotNull().OverridePropertyName(x => x.Level).WithName("Level").WithMessage("Level is Obligatory."); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Oscar Morales
					Oscar Morales