Example #1
0
        public async Task <IActionResult> CreateAccountAsync([FromBody] AccountCreateModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Password))
            {
                throw new IsRequiredException("password");
            }

            if (model.Password.Length < 8 || model.Password.Length > 20)
            {
                throw new PasswordIsInvalidException();
            }

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

            if (model.BirthDate.HasValue)
            {
                if (model.BirthDate.Value.Year < Constants.MinBirthDate.Year || model.BirthDate.Value.Year > DateTime.Now.Year - Constants.MinAge)
                {
                    throw new BirthDateIsInvalidException();
                }
            }

            if (model.Email != null)
            {
                if (!model.Email.IsEmail())
                {
                    throw new EmailIsInvalidException();
                }

                if (await _accountRepository.AnyByEmailAsync(model.Email))
                {
                    throw new AlreadyExistsException("email");
                }
            }

            if (model.Phone != null)
            {
                if (!model.Phone.IsMobile())
                {
                    throw new PhoneIsInvalidException();
                }

                if (await _accountRepository.AnyByPhoneAsync(model.Phone))
                {
                    throw new AlreadyExistsException("phone");
                }
            }

            if (string.IsNullOrWhiteSpace(model.AccountId))
            {
                throw new IsRequiredException("accountId");
            }

            if (model.AccountId.Length > 20)
            {
                throw new AccountIdIsInvalidException();
            }

            if (await _accountRepository.AnyByIdAsync(model.AccountId))
            {
                throw new AlreadyExistsException("account");
            }

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

            var now = DateTime.Now;

            var account = new Account
            {
                AccountId   = model.AccountId,
                GroupId     = 4,
                WardId      = model.WardId.HasValue ? model.WardId.Value : 0,
                Password    = model.Password,
                Name        = model.Name != null ? model.Name : null,
                Gender      = model.Gender.HasValue ? model.Gender.Value : null,
                BirthDate   = model.BirthDate.HasValue ? model.BirthDate : null,
                Address     = model.Address != null ? model.Address : null,
                Email       = model.Email != null ? model.Email : null,
                Phone       = model.Phone != null ? model.Phone : null,
                CreatedDate = now,
                UpdatedDate = now
            };

            await _accountRepository.CreateAccountAsync(account);

            return(Ok(AccountDTO.GetFrom(account)));
        }
        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)));
        }