Skip to content

Commit

Permalink
feat(infraestructure): adding order dtos and validators
Browse files Browse the repository at this point in the history
  • Loading branch information
MayerliSantander committed Sep 12, 2024
1 parent 17fef4f commit 49f360d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Order>
{
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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Order>
{
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace DistributionCenter.Infraestructure.Validators.Core.Concretes.Orders;

using Bases;
using DTOs.Concretes.Orders;
using Extensions;

public class CreateOrderValidator : BaseFluentValidator<CreateOrderDto>
{
public CreateOrderValidator()
{
_ = RuleFor<Guid?>(static x => x.ClientId)
.UuidNotNull("Client id is required");

_ = RuleFor<Guid?>(static x => x.OrderStatusId)
.UuidNotNull("Order status id is required");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace DistributionCenter.Infraestructure.Validators.Core.Concretes.Orders;

using Bases;
using DTOs.Concretes.Orders;
using Extensions;

public class UpdateOrderValidator : BaseFluentValidator<UpdateOrderDto>
{
public UpdateOrderValidator()
{
_ = RuleFor(static x => x.ClientId)
.UuidNotNull("Client id is required");

_ = RuleFor(static x => x.OrderStatusId)
.UuidNotNull("Order status id is required");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DistributionCenter.Infraestructure.Validators.Extensions;

using Components.Builders.Interfaces;

public static partial class ValidationExtensionsUuid
{
public static IValidationBuilder<Guid?> UuidNotNull(this IValidationBuilder<Guid?> builder, string message)
{
ArgumentNullException.ThrowIfNull(builder, nameof(builder));

return builder.When(static x => x.HasValue).AddRule(static x => x != Guid.Empty, message)!;
}
}

0 comments on commit 49f360d

Please sign in to comment.