using FluentValidation; using GreenTree.Maschinenbestellungen.Domain.Model; using GreenTree.Maschinenbestellungen.Services.Authorization; using GreenTree.Maschinenbestellungen.Web.Models.Rights.User; using Microsoft.AspNetCore.Identity; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; namespace GreenTree.Maschinenbestellungen.Web.Validators { public class UserValidator : AbstractValidator { #region DI fields // The global DbContext private readonly OrderDbContext _eRPDbContext; #endregion #region Ctor /// /// Initializes a new instance of the UserValidator class /// /// Global DbContext. public UserValidator(OrderDbContext eRPDbContext) { _eRPDbContext = eRPDbContext; RuleFor(x => x.Accountname) .NotEmpty() .Length(0, 16) .WithMessage("Der Kontoname darf maximal 16 Zeichen lang sein."); RuleFor(x => x.Accountname) .Must(a => !_eRPDbContext.Users.Any(u => u.Accountname == a)) .When(x => _eRPDbContext.Users .Any(u => u.Id != x.Id && u.Accountname == x.Accountname)) .WithMessage("Der Accountname wird bereits verwendet."); RuleFor(x => x.Forename) .NotEmpty() .WithMessage("Es muss ein gültiger Vorname eingegeben werden."); RuleFor(x => x.Lastname) .NotEmpty() .WithMessage("Es muss ein gültiger Nachname eingegeben werden."); RuleFor(x => x.MailAddress) .EmailAddress() .WithMessage("Es muss eine gültige E-Mail Adresse eingegeben werden."); RuleFor(x => x.Birthdate) .LessThan(DateTime.Now.Date.AddYears(-12)) .WithMessage("Das Geburtsdatum muss mindestens 12 Jahre in der Vergangenheit liegen."); } #endregion } }