コード例 #1
0
        public Data.Bonus MapModelToBonus(CreateUpdateBonus model)
        {
            var bonus = Mapper.Map <Data.Bonus>(model);

            var template = _bonusQueries.GetCurrentVersionTemplates().Single(a => a.Id == model.TemplateId);

            bonus.Template = template;

            var timezoneId = template.Info.Brand.TimezoneId;

            bonus.ActiveFrom = bonus.DurationStart = model.ActiveFrom.ToBrandDateTimeOffset(timezoneId);
            bonus.ActiveTo   = bonus.DurationEnd = model.ActiveTo.ToBrandDateTimeOffset(timezoneId);

            if (model.DurationType == DurationType.Custom)
            {
                bonus.DurationStart = model.DurationStart.ToBrandDateTimeOffset(timezoneId);
                bonus.DurationEnd   = model.DurationEnd.ToBrandDateTimeOffset(timezoneId);
            }
            else if (model.DurationType == DurationType.StartDateBased)
            {
                var durationLength = new TimeSpan(model.DurationDays, model.DurationHours, model.DurationMinutes, 0);
                bonus.DurationEnd = bonus.ActiveFrom.Add(durationLength);
            }

            return(bonus);
        }
コード例 #2
0
        public void Add_template_saves_all_data_to_DB()
        {
            var template       = CreateFirstDepositTemplate();
            var templateFromDb = _bonusQueries.GetCurrentVersionTemplates().Single(t => t.Id == template.Id);

            Assert.NotNull(templateFromDb.Info);
            Assert.NotNull(templateFromDb.Availability);
            Assert.NotNull(templateFromDb.Rules);
            Assert.NotNull(templateFromDb.Wagering);
            Assert.NotNull(templateFromDb.Notification);
            Assert.AreEqual(TemplateStatus.Complete, templateFromDb.Status);
        }
コード例 #3
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));
        }
コード例 #4
0
        public TemplateDeletionValidator(BonusQueries queries)
        {
            CascadeMode = CascadeMode.StopOnFirstFailure;

            RuleFor(model => model)
            .Must(model => queries.GetCurrentVersionTemplates().Any(t => t.Id == model.TemplateId))
            .WithMessage(ValidatorMessages.TemplateDoesNotExist)
            .Must(model =>
                  queries.GetCurrentVersionBonuses().Any(bonus => bonus.Template.Id == model.TemplateId) == false)
            .WithMessage(ValidatorMessages.TemplateIsInUse)
            .WithName("Template");
        }
コード例 #5
0
        public BonusValidator(IBonusRepository repository, BonusQueries bonusQueries)
        {
            Func <Guid, Template> templateGetter = templateId => bonusQueries.GetCurrentVersionTemplates().SingleOrDefault(a => a.Id == templateId);

            When(bonus => bonus.Id != Guid.Empty, () =>
            {
                Func <CreateUpdateBonus, IQueryable <Data.Bonus> > bonuses = bonusVm => repository.Bonuses.Where(b => b.Id == bonusVm.Id);
                RuleFor(bonus => bonus)
                .Must(bonus => bonuses(bonus).Any())
                .WithMessage(ValidatorMessages.BonusDoesNotExist)
                .WithName("Bonus");

                RuleFor(bonus => bonus)
                .Must(bonus => bonus.Version == bonuses(bonus).Max(b => b.Version))
                .WithMessage(ValidatorMessages.BonusVersionIsNotCurrent)
                .WithName("Bonus")
                .When(bonus => bonuses(bonus).Any());
            });

            ValidateName(repository);

            RuleFor(bonus => bonus.Description)
            .NotNull()
            .WithMessage(ValidatorMessages.DescriptionIsRequired)
            .Length(1, 200)
            .WithMessage(ValidatorMessages.DescriptionLengthIsInvalid, 1, 200);

            RuleFor(bonus => bonus.DaysToClaim)
            .GreaterThanOrEqualTo(0)
            .WithMessage(ValidatorMessages.BonusDaysToClaimIsNegative);

            RuleFor(bonus => bonus.TemplateId)
            .Must(templateId => templateGetter(templateId) != null)
            .WithMessage(ValidatorMessages.BonusTemplateIsNotAssigned);

            When(b => templateGetter(b.TemplateId) != null, () =>
            {
                RuleFor(bonus => bonus.Code)
                .NotEmpty()
                .When(bonus => templateGetter(bonus.TemplateId).Info.Mode == IssuanceMode.AutomaticWithCode)
                .WithMessage(ValidatorMessages.BonusCodeIsNotSpecified);
                When(bonus => string.IsNullOrWhiteSpace(bonus.Code) == false, () => RuleFor(bonus => bonus.Code)
                     .Matches(@"^[a-zA-Z0-9]*$")
                     .WithMessage(ValidatorMessages.BonusCodeIsInvalid)
                     .Length(1, 20)
                     .WithMessage(ValidatorMessages.BonusCodeLengthIsInvalid, 1, 20)
                     .Must((bonus, code) =>
                {
                    if (bonus.Id == Guid.Empty)
                    {
                        return(bonusQueries.GetCurrentVersionBonuses().Any(b => b.Code == code) == false);
                    }

                    return(bonusQueries.GetCurrentVersionBonuses().Any(b => b.Code == code && b.Id != bonus.Id) == false);
                })
                     .WithMessage(ValidatorMessages.BonusCodeIsNotUnique));

                RuleFor(bonus => bonus.ActiveTo)
                .Must((bonus, activeTo) => activeTo > bonus.ActiveFrom)
                .WithMessage(ValidatorMessages.BonusActivityRangeIsInvalid)
                .Must((bonus, activeTo) =>
                {
                    var template   = templateGetter(bonus.TemplateId);
                    var timezoneId = template.Info.Brand.TimezoneId;
                    return(activeTo.ToBrandDateTimeOffset(timezoneId) >
                           DateTimeOffset.Now.ToBrandOffset(timezoneId));
                })
                .WithMessage(ValidatorMessages.BonusActiveToIsInvalid);

                ValidateDuration();
            });
        }