public void Register(RegisterRequest request)
        {
            if (_accountManager.Get(request.Username) != null)
            {
                throw new AppException("User already exists");
            }

            var account = new Account
            {
                Username     = request.Username,
                PasswordHash = _hashService.Hash(request.Password).Digest,
                Created      = DateTime.Now,
                Updated      = DateTime.Now
            };

            _accountManager.Create(account);
        }
Example #2
0
        public async Task <IActionResult> Create_Or_Update(Accounts inputModel)
        {
            try
            {
                if (string.IsNullOrEmpty(inputModel.UserName))
                {
                    throw new Exception($"Tên {MessageConst.NOT_EMPTY_INPUT}");
                }
                if (string.IsNullOrEmpty(inputModel.Password))
                {
                    throw new Exception($"Mật khẩu {MessageConst.NOT_EMPTY_INPUT}");
                }
                if (inputModel.Password.Length < 6)
                {
                    throw new Exception($"Mật khẩu phải trên 6 ký tự");
                }
                if (inputModel.Id == 0)
                {
                    var data = await _accountManager.Find_By_UserName(inputModel.UserName);

                    if (data != null)
                    {
                        throw new Exception($"Tên tài khoản đã tồn tại");
                    }
                    inputModel.CreatedDate = DateTime.Now;
                    await _accountManager.Create(inputModel);

                    return(Json(new { Result = true, Message = "Thêm mới dữ liệu thành công" }));
                }
                else
                {
                    var data = await _accountManager.Find_By_Id_Ok(inputModel.Id);

                    inputModel.StatusActing = data.StatusActing;
                    inputModel.UpdatedDate  = DateTime.Now;
                    await _accountManager.Update(inputModel);

                    return(Json(new { Result = true, Message = "Cập nhật dữ liệu thành công" }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new { Result = false, Message = ex.Message }));
            }
        }
Example #3
0
        public async Task <ActionResult <AccountViewModel> > Post(AccountCreateViewModel accountInfo)
        {
            if (ModelState.IsValid)
            {
                Account createAccount = _mapper.Map <Account>(accountInfo);
                createAccount = await _accountManager.Create(createAccount);

                if (createAccount != null)
                {
                    AccountViewModel createdAccountInfo = _mapper.Map <AccountViewModel>(createAccount);
                    return(Ok(createdAccountInfo));
                }

                return(BadRequest(new { ErrorMessage = "Account create failed! Try again." }));
            }

            return(BadRequest(ModelState));
        }
Example #4
0
        public async Task <IActionResult> Create(AccountRequest accountRequest)
        {
            try
            {
                //Request validation
                if (ModelState.IsValid == false)
                {
                    return(BadRequest(ModelState));
                }

                var account = new Core.Abstractions.Models.Account()
                {
                    CustomerId = accountRequest.CustomerId
                };

                await _accountManager.Create(account, accountRequest.initialCredit).ConfigureAwait(false);

                return(Ok(account));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new { error = ex.Message }));
            }
        }
 public async Task <ApiResponse> Create(RegisterViewModel parameters)
 => ModelState.IsValid ? await _accountManager.Create(parameters) : _invalidData;