46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
|
using CH.CQRS.Service.User;
|
|||
|
using CH.CQRS.Service.User.Options;
|
|||
|
using CH.Dal;
|
|||
|
using FluentValidation;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using OpenHarbor.CQRS.Abstractions;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace CH.CQRS.Command.User;
|
|||
|
|
|||
|
public class UpdateEmailCommand
|
|||
|
{
|
|||
|
public required string Email { get; set; }
|
|||
|
}
|
|||
|
|
|||
|
public class UpdateEmailCommandHandler(UserService userService) : ICommandHandler<UpdateEmailCommand>
|
|||
|
{
|
|||
|
public Task HandleAsync(UpdateEmailCommand command, CancellationToken cancellationToken = default)
|
|||
|
{
|
|||
|
return userService.UpdateEmailAsync(new UpdateEmailCommandOptions
|
|||
|
{
|
|||
|
Email = command.Email
|
|||
|
}, cancellationToken);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public class UpdateEmailCommandValidator : AbstractValidator<UpdateEmailCommand>
|
|||
|
{
|
|||
|
public UpdateEmailCommandValidator(CHDbContext dbContext)
|
|||
|
{
|
|||
|
RuleFor(command => command.Email)
|
|||
|
.NotEmpty()
|
|||
|
.EmailAddress()
|
|||
|
.MustAsync(async (email, CancellationToken) =>
|
|||
|
{
|
|||
|
var emailComparaison = email.Trim().ToLower();
|
|||
|
var emailInUse = await dbContext.Users.AnyAsync(user => user.Email.ToLower() == emailComparaison);
|
|||
|
return false == emailInUse;
|
|||
|
})
|
|||
|
.WithMessage("This email is already in use by another user.");
|
|||
|
}
|
|||
|
}
|