using Connected.Middleware; using Connected.Security.Identity; using Connected.Validation; namespace Common.Documents; public abstract class InsertDocumentValidator : MiddlewareComponent, IValidator where TDocumentArgs : InsertDocumentArgs { public InsertDocumentValidator(IUserService users) { Users = users; } protected IUserService Users { get; } protected TDocumentArgs Arguments { get; private set; } public async Task Validate(TDocumentArgs args) { Arguments = args; await ValidateAuthor(); } protected virtual async Task OnValidating() { await Task.CompletedTask; } private async Task ValidateAuthor() { if (Arguments.Author is null) return; if (await Users.Select(Arguments.Author) is null) throw new ArgumentException(nameof(Arguments.Author)); } } public abstract class UpdateDocumentValidator : MiddlewareComponent, IValidator where TDocumentArgs : UpdateDocumentArgs where TPrimaryKey : notnull { public UpdateDocumentValidator(IUserService users) { Users = users; } protected IUserService Users { get; } public async Task Validate(TDocumentArgs args) { await ValidateOwner(args); } private async Task ValidateOwner(TDocumentArgs args) { if (args.Owner is null) return; if (await Users.Select(args.Owner) is null) throw new ArgumentException(nameof(args.Owner)); } }