Beispiel #1
0
        public ActionResult <LoggedUserInfo> Register([FromBody] RegisterModel registerModel)
        {
            StartInfo(ControllerContext.RouteData.Values["action"].ToString(), registerModel);
            try
            {
                if (registerModel.Name == string.Empty ||
                    registerModel.Email == string.Empty ||
                    registerModel.Password == string.Empty)
                {
                    throw new InvalidOperationException(ConstStrings.RegisterDataNullError);
                }

                if (_databasePW.IsUserNameExists(registerModel.Name))
                {
                    throw new InvalidOperationException(ConstStrings.UserNameExistsError);
                }

                if (_databasePW.IsUserEmailExists(registerModel.Email))
                {
                    throw new InvalidOperationException(ConstStrings.UserEmailExistsError);
                }

                UserDb userDb = new UserDb()
                {
                    Name     = registerModel.Name,
                    Email    = registerModel.Email,
                    Password = registerModel.Password,
                    Balance  = 500
                };

                UserInfo userInfo = _databasePW.RegisterUser(userDb);

                Guid token = Guid.NewGuid();
                if (!_tokenCache.Add(token, userInfo.Id))
                {
                    isServerError = true;
                    throw new InvalidOperationException(ConstStrings.TokenSaveError);
                }

                LoggedUserInfo loggedUserInfo = new LoggedUserInfo()
                {
                    Id      = userInfo.Id,
                    Name    = userInfo.Name,
                    Email   = userInfo.Email,
                    Balance = userInfo.Balance,
                    Token   = token
                };

                _logger.Info(string.Format("End. Return: {0}",
                                           JsonConvert.SerializeObject(loggedUserInfo)));
                _logger.EndLogMethod();

                return(Ok(loggedUserInfo));
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.EndLogMethod();
                if (isServerError)
                {
                    return(StatusCode(500));
                }
                else
                {
                    return(BadRequest(ex.Message));
                }
            }
        }