Ejemplo n.º 1
0
    /// <summary>
    /// json 문자열을 Dictionary<string, object> 형태로 변환합니다.
    /// 이 때 가독성을 위해 해당 딕셔너리를 상속받은 ResponseData 를 사용합니다.
    /// 추가로 json 데이터와 에러코드 데이터를 가지고 있습니다.
    /// </summary>
    /// <param name="jsonStr"> ResponseData로 변환할 문자열입니다. json 형식을 기반으로 컨버팅합니다. </param>
    /// <returns>컨버팅된 ResponseData입니다.</returns>
    public ResponseData JsonToResponseData(string jsonStr)
    {
        ResponseData responseData = new ResponseData();

        // 제이슨 데이터 할당
        JsonData jsonData = JsonMapper.ToObject(jsonStr);

        responseData.SetJsonString(jsonStr);
        responseData.SetJsonData(jsonData);

        // 순회를 위해 IEnumerator 를 가져온 뒤 데이터를 Dictionary에 추가합니다.
        var e = jsonData.Keys.GetEnumerator();

        while (e.MoveNext())
        {
            responseData.Add(e.Current, jsonData[e.Current]);
        }

        // 에러코드 할당
        if (responseData.ContainsKey(SalinAPIKey.code))
        {
            int errorCode = SalinConstants.defaultInt;
            int.TryParse((responseData[SalinAPIKey.code].ToString()), out errorCode);
            responseData.SetErrorCode(errorCode);
        }
        else if (responseData.ContainsKey("status"))
        {
            JsonData statusData = null;
            statusData = (JsonData)responseData["status"];
            if (statusData["message"] != null)
            {
                int errorCode = statusData["message"].ToString() == "SUCCESS" ? 200 : 0;
                responseData.SetErrorCode(errorCode);
            }
            else
            {
                Debug.LogError("There is no code in the json. must something is wrong!!!!");
            }
        }
        else
        {
            Debug.LogError("There is no code in the json. must something is wrong!!!!");
        }

        return(responseData);
    }
Ejemplo n.º 2
0
        public static ResponseData createResponseData(Response response)
        {
            var responseData = new ResponseData();

            foreach (ReplySentence reply in response)
            {
                if (reply.Re)
                {
                    var responseItem = new ResponseItem();
                    foreach (Word word in reply)
                    {
                        AttributeWord attribute = word as AttributeWord;
                        if (attribute != null)
                        {
                            responseItem[attribute.Key] = attribute.Value;
                        }
                    }

                    responseData.Add(responseItem);
                }
            }

            return(responseData);
        }
Ejemplo n.º 3
0
        public async Task <APIResponseModel> Process(UserSignupAction action)
        {
            // just be safe
            action.Email = action.Email.ToLower();

            if (string.IsNullOrWhiteSpace(action.Email))
            {
                return(APIResponseModel.Error(ResponseCode.InvalidParameter, "Email"));
            }
            if (string.IsNullOrWhiteSpace(action.Password))
            {
                return(APIResponseModel.Error(ResponseCode.InvalidParameter, "Password"));
            }
            if (action.Language == LanguageId.None)
            {
                action.Language = LanguageId.en;
            }

            var passwordStrength = new PasswordStrengthValidator().Test(action.Password);

            if (!passwordStrength.Good)
            {
                return(APIResponseModel.Error(ResponseCode.InvalidParameter, "Password Strength"));
            }

            // prepare the user
            UserPasswordModel password = new UserPasswordModel(await this.PasswordManager.CreatePasswordHash(action.Password));

            UserModel user = UserModel.Create(UserType.Standard, UserStatusId.Registered, action.Language, action.Email, password, action.DisplayName);

            var rs = await this.DataManager.CreateUserAsync(user);

            // failed - get out
            if (rs.Code != ResponseCode.Ok)
            {
                //await this.DataManager.LogEventAsync( LogEventModel.Failure( action.Action, user.ToJson(), user.UserId ) );
                return(APIResponseModel.Result(rs));
            }

            try
            {
                var j = new Newtonsoft.Json.Linq.JObject(
                    new Newtonsoft.Json.Linq.JProperty("userId", user.UserId.Encode()),
                    new Newtonsoft.Json.Linq.JProperty("email", user.Email ?? "{null}"),
                    new Newtonsoft.Json.Linq.JProperty("name", user.Name ?? "{null}")
                    ).ToString(Newtonsoft.Json.Formatting.Indented);
                this.DataManager.WriteEvent("user-created", j);                   // don't await - fire & forget
            }
            catch { }


            // move everything from the email address to the user
            try
            {
                rs = await this.DataManager.ConvertEmailToUserId(user);

                if (rs.Code != ResponseCode.Ok)
                {
                    await this.DataManager.LogErrorAsync("Process(UserSignupAction)", rs.ToJson());
                }
            }
            catch (Exception ex)
            {
                this.DataManager.LogExceptionAsync("Process(UserSignupAction)", ex);
            }

            // pipeline the email verification request
            try
            {
                APIResponseModel rs1 = await this.Process(new InternalSendEmailVerificationAction()
                {
                    UserId = user.UserId
                });
            }
            catch (Exception ex)
            {
                this.DataManager.LogErrorAsync("InternalSendEmailVerificationAction", ex.Message + Environment.NewLine + (ex.StackTrace ?? string.Empty));
            }

            this.SlackProvider.Send($"New user! {action.Email}");               //fire and forget

            ResponseData response = new ResponseData();

            response.Add(ResponseType.User, new UserViewModel(user));
            return(APIResponseModel.ResultWithData(response));
        }