Esempio n. 1
0
        public TemplateValidator(IBonusRepository repository, BonusQueries queries)
        {
            CascadeMode = CascadeMode.StopOnFirstFailure;

            _infoGetter  = template => repository.Templates.Single(t => t.Id == template.Id && t.Version == template.Version).Info;
            _brandGetter = template => template.Info == null?_infoGetter(template).Brand : repository.Brands.SingleOrDefault(b => b.Id == template.Info.BrandId);

            When(template => template.Id != Guid.Empty, () =>
            {
                Func <CreateUpdateTemplate, Template> templateGetter =
                    vm => queries.GetCurrentVersionTemplates().SingleOrDefault(t => t.Id == vm.Id);
                RuleFor(template => template)
                .Must(template => templateGetter(template) != null)
                .WithMessage(ValidatorMessages.TemplateDoesNotExist)
                .Must(template => templateGetter(template).Version == template.Version)
                .WithMessage(ValidatorMessages.TemplateVersionIsNotCurrent)
                .Must(template => queries.GetBonusesUsingTemplate(templateGetter(template)).Any(bonus => bonus.IsActive) == false)
                .WithMessage(ValidatorMessages.AllBonusesShouldBeInactive)
                .When(template => templateGetter(template).Status == TemplateStatus.Complete, ApplyConditionTo.CurrentValidator)
                .WithName("Template")
                .DependentRules(rules =>
                {
                    When(template => template.Availability != null, () => ValidateAvailability(repository));
                    When(template => template.Rules != null, ValidateRules);
                    When(template => template.Wagering != null, () => ValidateWagering(repository));
                });
            });

            When(template => template.Info != null, () => ValidateInfo(repository));
        }