Exemple #1
0
        public async Task <IActionResult> Create([FromBody] Accounts inputModel)
        {
            try
            {
                if (string.IsNullOrEmpty(inputModel.UserName))
                {
                    throw new Exception($"Tên tài khoản  {MessageConst.NOT_EMPTY_INPUT}");
                }

                if (Validation.HasSpecialChar(inputModel.UserName))
                {
                    throw new Exception($"Tên tài khoản  {MessageConst.SPECIAL_CHAR}");
                }
                var exist = await _accountsManager.Find_By_Name(inputModel.UserName);

                if (exist != null)
                {
                    throw new Exception($"Tên tài khoản {MessageConst.EXIST}");
                }

                if (!Validation.IsValidEmail(inputModel.Email))
                {
                    throw new Exception($"Email không hợp lệ");
                }
                var checkEmai = await _accountsManager.FindByEmail(inputModel.Email);

                if (checkEmai != null)
                {
                    throw new Exception($"Email đã được dùng cho tài khoản khác");
                }
                inputModel.FullName    = Extensions.StringStandar(inputModel.FullName, 1);
                inputModel.CreatedDate = DateTime.Now;
                var data = await _accountsManager.Create(inputModel);
                await CreateAccountRole(data.Id, inputModel.ListRoleId);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }