27 lines
814 B
C#
27 lines
814 B
C#
using Hutopy.Application.Common.Interfaces;
|
|
|
|
namespace Hutopy.Application.TodoLists.Commands.CreateTodoList;
|
|
|
|
public class CreateTodoListCommandValidator : AbstractValidator<CreateTodoListCommand>
|
|
{
|
|
private readonly IApplicationDbContext _context;
|
|
|
|
public CreateTodoListCommandValidator(IApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
|
|
RuleFor(v => v.Title)
|
|
.NotEmpty()
|
|
.MaximumLength(200)
|
|
.MustAsync(BeUniqueTitle)
|
|
.WithMessage("'{PropertyName}' must be unique.")
|
|
.WithErrorCode("Unique");
|
|
}
|
|
|
|
public async Task<bool> BeUniqueTitle(string title, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.TodoLists
|
|
.AllAsync(l => l.Title != title, cancellationToken);
|
|
}
|
|
}
|