Ejemplo n.º 1
0
        public async Task <string> GetSettings(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("Invalid argument for get settings", nameof(path));
            }

            string[] items    = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
            bool     needJson = items.Length > 1;

            JsonWriter w        = new JsonWriter(Formatting.None);
            JsonValue  settings = null;

            if (!_cache.TryGetValue(items[0], out string s))
            {
                using (MdbContext mdb = _mdbFactory.GetContext(Properties.Strings.DefaultConnection))
                    using (var dr = await mdb.GetReaderAsync(_sql_get_settings,
                                                             new MdbParameter("@identifier", items[0])))
                    {
                        if (!dr.Read())
                        {
                            throw new KeyNotFoundException($"No found setting with Identifier = '{items[0]}'");
                        }

                        w.WriteStartObject();
                        for (int i = 0; i < dr.FieldCount; i++)
                        {
                            string name = dr.GetName(i);
                            if (name == "ValueString")
                            {
                                string source = (string)dr.GetValue(i);
                                if (needJson && !JsonValue.TryParse(source, out settings))
                                {
                                    throw new KeyNotFoundException($"No found setting with path = '{path}' Or ValueString is not json format");
                                }

                                w.WritePropertyName("Settings");
                                if (needJson || JsonValue.Test(source))
                                {
                                    w.WriteRaw(source);
                                }
                                else
                                {
                                    w.WriteValue(source);
                                }
                            }
                            else
                            {
                                w.WriteProperty(name, dr.GetValue(i));
                            }
                        }
                        w.WriteEndObject();
                        s = w.ToString();
                        _cache.TryAdd(items[0], s);
                    }
            }
            else if (needJson)
            {
                settings = new JsonReader(s).Read()["Settings"];
            }

            if (!needJson)
            {
                return(s);
            }
            else if (settings == null)
            {
                throw new InvalidCastException($"ValueString has not valid json format");
            }

            var result = settings[items[1]];

            for (int i = 2; i < items.Length; i++)
            {
                result = result[items[i]];
            }
            return(result.ToString());
        }