41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using FluentValidation;
|
|
using Svrnty.CQRS.Abstractions;
|
|
|
|
namespace Svrnty.CQRS.Grpc.Sample;
|
|
|
|
public record AddUserCommand
|
|
{
|
|
public string Name { get; set; } = string.Empty;
|
|
public string Email { get; set; } = string.Empty;
|
|
public int Age { get; set; }
|
|
}
|
|
|
|
public class AddUserCommandValidator : AbstractValidator<AddUserCommand>
|
|
{
|
|
public AddUserCommandValidator()
|
|
{
|
|
RuleFor(x => x.Email)
|
|
.NotEmpty()
|
|
.WithMessage("Email is required")
|
|
.EmailAddress()
|
|
.WithMessage("Email must be a valid email address");
|
|
|
|
RuleFor(x => x.Name)
|
|
.NotEmpty()
|
|
.WithMessage("Name is required");
|
|
|
|
RuleFor(x => x.Age)
|
|
.GreaterThan(0)
|
|
.WithMessage("Age must be greater than 0");
|
|
}
|
|
}
|
|
|
|
public class AddUserCommandHandler : ICommandHandler<AddUserCommand, int>
|
|
{
|
|
public Task<int> HandleAsync(AddUserCommand command, CancellationToken cancellationToken = default)
|
|
{
|
|
// Simulate adding a user and returning ID
|
|
return Task.FromResult(123);
|
|
}
|
|
}
|