using Connected.Middleware; using Connected.Validation; namespace Logistics.Types.Packaging; internal sealed class InsertPackingValidator : MiddlewareComponent, IValidator { public InsertPackingValidator(IPackingService packingService) { PackingService = packingService; } public IPackingService PackingService { get; } public async Task Validate(InsertPackingArgs args) { if (await PackingService.Select(new SelectPackingArgs { Ean = args.Ean }) is not null) { throw ValidationExceptions.ValueExists(nameof(args.Ean), args.Ean); } } } internal sealed class UpdatePackingValidator : MiddlewareComponent, IValidator { public UpdatePackingValidator(IPackingService packingService) { PackingService = packingService; } public IPackingService PackingService { get; } public async Task Validate(UpdatePackingArgs args) { if (await PackingService.Select(new SelectPackingArgs { Ean = args.Ean }) is Packing entity && entity.Id != args.Id) { throw ValidationExceptions.ValueExists(nameof(args.Ean), args.Ean); } } }