示例#1
0
        public void When_CampaignToDateIsInThePast_Expect_AnErrorForCampaignFromDateThrown()
        {
            var campaign = new CampaignCreateModel
            {
                ToDate = DateTime.UtcNow.AddDays(-1)
            };

            var result = _campaignCreateValidator.ShouldHaveValidationErrorFor(c => c.ToDate, campaign);

            result.WithErrorMessage(Phrases.CampaignToDateValidation);
        }
示例#2
0
        public void When_NoCreateConditionPassed_Expect_AnErrorForConditionRequiredThrown()
        {
            var campaign = new CampaignCreateModel
            {
                FromDate   = DateTime.UtcNow.AddMonths(1),
                Conditions = new List <ConditionCreateModel>()
            };

            var result = _campaignCreateValidator.ShouldHaveValidationErrorFor(c => c.Conditions, campaign);

            result.WithErrorMessage(Phrases.CampaignConditionNotNull);
        }
        public async Task <ActionResult <CampaignModel> > PostCampaign(CampaignCreateModel model)
        {
            var result = Check(model.Company_Id == COMPANY_ID, Forbidden).OkNull() ?? Check(Operation.Create);

            if (result.Fail())
            {
                return(result);
            }

            var entity = GetEntity(model);

            DB_TABLE.Add(entity);
            await DB.SaveChangesAsync();

            Log(DB_TABLE.GetName(), Operation.Create, entity.Id, GetModel(entity));

            return(CreatedAtAction(nameof(GetCampaign), new { id = entity.Id }, GetModel(entity)));
        }
示例#4
0
        public void When_EarnRuleContentsAreNull_Expect_AnErrorForMissingContentThrown()
        {
            var earnRule = new CampaignCreateModel()
            {
                Conditions = null,
                Contents   = null
            };

            var contentResult =
                _campaignCreateValidator.ShouldHaveValidationErrorFor(c => c.Contents, earnRule);

            contentResult.WithErrorMessage(Phrases.RuleContentTypeNotNull);

            var conditionResult =
                _campaignCreateValidator.ShouldHaveValidationErrorFor(c => c.Conditions, earnRule);

            conditionResult.WithErrorMessage(Phrases.CampaignConditionNotNull);
        }
        public async Task <CampaignCreateResponseModel> CreateCampaignAsync([FromBody] CampaignCreateModel model)
        {
            try
            {
                var campaign   = _mapper.Map <CampaignDetails>(model);
                var campaignId = await _campaignService.InsertAsync(campaign);

                return(new CampaignCreateResponseModel
                {
                    CampaignId = campaignId, ErrorCode = CampaignServiceErrorCodes.None
                });
            }
            catch (EntityNotValidException e)
            {
                _log.Info(Phrases.EntityNotValid, model);

                return(new CampaignCreateResponseModel
                {
                    ErrorCode = CampaignServiceErrorCodes.EntityNotValid, ErrorMessage = e.Message
                });
            }
        }
示例#6
0
        public void When_TwoCreateConditionsOfSameTypePassed_Expect_AnErrorForConditionOfSameTypeThrown()
        {
            const string type     = "SignUp";
            var          campaign = new CampaignCreateModel
            {
                FromDate   = DateTime.UtcNow.AddMonths(1),
                Conditions = new List <ConditionCreateModel>
                {
                    new ConditionCreateModel
                    {
                        Type = type
                    },
                    new ConditionCreateModel
                    {
                        Type = type
                    }
                }
            };

            var result = _campaignCreateValidator.ShouldHaveValidationErrorFor(c => c.Conditions, campaign);

            result.WithErrorMessage(Phrases.CampaignConditionUnique);
        }
示例#7
0
        public async Task <IActionResult> CreateCampaignAsync([FromBody] CampaignCreateModel model)
        {
            if (model.WardId == 0)
            {
                throw new IsRequiredException("ward");
            }

            if (!await _wardRepository.AnyByIdAsync(model.WardId))
            {
                throw new NotFound400Exception("ward");
            }

            if (model.CategoryId == 0)
            {
                throw new IsRequiredException("category");
            }

            if (!await _categoryRepository.AnyByIdAsync(model.CategoryId))
            {
                throw new NotFound400Exception("category");
            }

            if (string.IsNullOrWhiteSpace(model.Name))
            {
                throw new IsRequiredException("name");
            }

            if (model.Name.Length > 50)
            {
                throw new NameIsInvalidException();
            }

            if (string.IsNullOrWhiteSpace(model.Description))
            {
                throw new IsRequiredException("description");
            }

            if (model.Description.Length < 20)
            {
                throw new DescriptionIsInvalidException();
            }

            if (string.IsNullOrWhiteSpace(model.Location))
            {
                throw new IsRequiredException("location");
            }

            if (model.Location.Length > 100)
            {
                throw new LocationIsInvalidException();
            }

            if ((int)model.Type < 0 || (int)model.Type > Enum.GetNames(typeof(CampaignType)).Length)
            {
                throw new TypeIsInvalidException();
            }

            if (model.RegistrationStartDate == DateTime.MinValue)
            {
                throw new IsRequiredException("startDate");
            }

            if (model.RegistrationEndDate == DateTime.MinValue)
            {
                throw new IsRequiredException("endDate");
            }

            if (model.RegistrationStartDate >= model.RegistrationEndDate)
            {
                throw new StartDateMustBeBeforeEndDateException();
            }

            if (model.ExecutionStartDate == DateTime.MinValue)
            {
                throw new IsRequiredException("startDate");
            }

            if (model.ExecutionEndDate == DateTime.MinValue)
            {
                throw new IsRequiredException("endDate");
            }

            if (model.ExecutionStartDate >= model.ExecutionEndDate)
            {
                throw new StartDateMustBeBeforeEndDateException();
            }

            if (model.RegistrationEndDate.AddDays(2) >= model.ExecutionStartDate)
            {
                throw new ExecutionDateRangeMustBeAfterRegistrationDateRangeException();
            }

            var now = DateTime.Now;

            var campaign = new Campaign
            {
                AccountId             = CurrentAccountId,
                CategoryId            = model.CategoryId,
                WardId                = model.WardId,
                Name                  = model.Name,
                Image                 = model.Image,
                Description           = model.Description,
                Location              = model.Location,
                Type                  = model.Type,
                RegistrationStartDate = model.RegistrationStartDate,
                RegistrationEndDate   = model.RegistrationEndDate,
                ExecutionStartDate    = model.ExecutionStartDate,
                ExecutionEndDate      = model.ExecutionEndDate,
                CreatedDate           = now,
                UpdatedDate           = now
            };

            await _campaignRepository.CreateCampaignAsync(campaign);

            return(Ok(CampaignDTO.GetFrom(campaign)));
        }