diff --git a/src/DistributionCenter.Infraestructure/DTOs/Concretes/Orders/CreateOrderDto.cs b/src/DistributionCenter.Infraestructure/DTOs/Concretes/Orders/CreateOrderDto.cs new file mode 100644 index 0000000..4dd0e9d --- /dev/null +++ b/src/DistributionCenter.Infraestructure/DTOs/Concretes/Orders/CreateOrderDto.cs @@ -0,0 +1,26 @@ +namespace DistributionCenter.Infraestructure.DTOs.Concretes.Orders; + +using Commons.Results; +using Domain.Entities.Concretes; +using Interfaces; +using Validators.Core.Concretes.Orders; + +public class CreateOrderDto : ICreateDto +{ + public required Guid ClientId { get; init; } + public required Guid OrderStatusId { get; init; } + + public Order ToEntity() + { + return new Order + { + ClientId = ClientId, + OrderStatusId = OrderStatusId, + }; + } + + public Result Validate() + { + return new CreateOrderValidator().Validate(this); + } +} diff --git a/src/DistributionCenter.Infraestructure/DTOs/Concretes/Orders/UpdateOrderDto.cs b/src/DistributionCenter.Infraestructure/DTOs/Concretes/Orders/UpdateOrderDto.cs new file mode 100644 index 0000000..a3b8552 --- /dev/null +++ b/src/DistributionCenter.Infraestructure/DTOs/Concretes/Orders/UpdateOrderDto.cs @@ -0,0 +1,27 @@ +namespace DistributionCenter.Infraestructure.DTOs.Concretes.Orders; + +using Commons.Results; +using Domain.Entities.Concretes; +using Interfaces; +using Validators.Core.Concretes.Orders; + +public class UpdateOrderDto: IUpdateDto +{ + public Guid? ClientId { get; init; } + public Guid? OrderStatusId { get; init; } + + public Order FromEntity(Order client) + { + ArgumentNullException.ThrowIfNull(client, nameof(client)); + + client.ClientId = ClientId ?? client.ClientId; + client.OrderStatusId = OrderStatusId ?? client.OrderStatusId; + + return client; + } + + public Result Validate() + { + return new UpdateOrderValidator().Validate(this); + } +} diff --git a/src/DistributionCenter.Infraestructure/Validators/Core/Concretes/Orders/CreateOrderValidator.cs b/src/DistributionCenter.Infraestructure/Validators/Core/Concretes/Orders/CreateOrderValidator.cs new file mode 100644 index 0000000..e2d6636 --- /dev/null +++ b/src/DistributionCenter.Infraestructure/Validators/Core/Concretes/Orders/CreateOrderValidator.cs @@ -0,0 +1,17 @@ +namespace DistributionCenter.Infraestructure.Validators.Core.Concretes.Orders; + +using Bases; +using DTOs.Concretes.Orders; +using Extensions; + +public class CreateOrderValidator : BaseFluentValidator +{ + public CreateOrderValidator() + { + _ = RuleFor(static x => x.ClientId) + .UuidNotNull("Client id is required"); + + _ = RuleFor(static x => x.OrderStatusId) + .UuidNotNull("Order status id is required"); + } +} diff --git a/src/DistributionCenter.Infraestructure/Validators/Core/Concretes/Orders/UpdateOrderValidator.cs b/src/DistributionCenter.Infraestructure/Validators/Core/Concretes/Orders/UpdateOrderValidator.cs new file mode 100644 index 0000000..f9b73da --- /dev/null +++ b/src/DistributionCenter.Infraestructure/Validators/Core/Concretes/Orders/UpdateOrderValidator.cs @@ -0,0 +1,17 @@ +namespace DistributionCenter.Infraestructure.Validators.Core.Concretes.Orders; + +using Bases; +using DTOs.Concretes.Orders; +using Extensions; + +public class UpdateOrderValidator : BaseFluentValidator +{ + public UpdateOrderValidator() + { + _ = RuleFor(static x => x.ClientId) + .UuidNotNull("Client id is required"); + + _ = RuleFor(static x => x.OrderStatusId) + .UuidNotNull("Order status id is required"); + } +} diff --git a/src/DistributionCenter.Infraestructure/Validators/Extensions/ValidationExtensions.Uuid.cs b/src/DistributionCenter.Infraestructure/Validators/Extensions/ValidationExtensions.Uuid.cs new file mode 100644 index 0000000..7a14019 --- /dev/null +++ b/src/DistributionCenter.Infraestructure/Validators/Extensions/ValidationExtensions.Uuid.cs @@ -0,0 +1,13 @@ +namespace DistributionCenter.Infraestructure.Validators.Extensions; + +using Components.Builders.Interfaces; + +public static partial class ValidationExtensionsUuid +{ + public static IValidationBuilder UuidNotNull(this IValidationBuilder builder, string message) + { + ArgumentNullException.ThrowIfNull(builder, nameof(builder)); + + return builder.When(static x => x.HasValue).AddRule(static x => x != Guid.Empty, message)!; + } +}