<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hadouken.Http;
using Hadouken.Data;
using Hadouken.Data.Models;

namespace Hadouken.Impl.Http.Controllers.Api
{
    public class ConfigController : Controller
    {
        private IDataRepository _data;

        public ConfigController(IDataRepository data)
        {
            _data = data;
        }

        [HttpGet]
        [Route("/api/config")]
        public ActionResult GetConfig()
        {
            if (!String.IsNullOrEmpty(Context.Request.QueryString["k"]))
            {
                string[] keys = Context.Request.QueryString["k"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                var settings = _data.List<Setting>(s => keys.Contains(s.Key));

                return Json(settings.ToDictionary(s => s.Key, s => s.Value));
            }

            if (!String.IsNullOrEmpty(Context.Request.QueryString["g"]))
            {
                string group = Context.Request.QueryString["g"];
                var settings = _data.List<Setting>(s => s.Key.StartsWith(group));
                return Json(settings.ToDictionary(s => s.Key, s => s.Value));
            }

            return Json(_data.List<Setting>().ToDictionary(x => x.Key, x => x.Value));
        }

        [HttpPost]
        [Route("/api/config")]
        public ActionResult ChangeConfig()
        {
            var dictionary = BindModel<Dictionary<string, object>>();

            foreach (var key in dictionary.Keys)
            {
                string value = dictionary[key].ToString();

                Setting setting = _data.Single<Setting>(x => x.Key == key);

                if (setting == null)
                {
                    setting = new Setting() { Key = key, Value = value };
                    _data.Save(setting);
                }
                else
                {
                    setting.Value = value;
                    _data.Update(setting);
                }<commit_msg>Send the ISettingChanged message when a setting changes.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hadouken.Http;
using Hadouken.Data;
using Hadouken.Data.Models;
using Hadouken.Messaging;
using Hadouken.Messages;

namespace Hadouken.Impl.Http.Controllers.Api
{
    public class ConfigController : Controller
    {
        private IDataRepository _data;
        private IMessageBus _mbus;

        public ConfigController(IDataRepository data, IMessageBus mbus)
        {
            _data = data;
            _mbus = mbus;
        }

        [HttpGet]
        [Route("/api/config")]
        public ActionResult GetConfig()
        {
            if (!String.IsNullOrEmpty(Context.Request.QueryString["k"]))
            {
                string[] keys = Context.Request.QueryString["k"].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

                var settings = _data.List<Setting>(s => keys.Contains(s.Key));

                return Json(settings.ToDictionary(s => s.Key, s => s.Value));
            }

            if (!String.IsNullOrEmpty(Context.Request.QueryString["g"]))
            {
                string group = Context.Request.QueryString["g"];
                var settings = _data.List<Setting>(s => s.Key.StartsWith(group));
                return Json(settings.ToDictionary(s => s.Key, s => s.Value));
            }

            return Json(_data.List<Setting>().ToDictionary(x => x.Key, x => x.Value));
        }

        [HttpPost]
        [Route("/api/config")]
        public ActionResult ChangeConfig()
        {
            var dictionary = BindModel<Dictionary<string, object>>();

            foreach (var key in dictionary.Keys)
            {
                string value = dictionary[key].ToString();

                Setting setting = _data.Single<Setting>(x => x.Key == key);

                if (setting == null)
                {
                    setting = new Setting() { Key = key };
                }

                string oldValue = setting.Value;

                setting.Value = value;
                _data.SaveOrUpdate(setting);

                _mbus.Send<ISettingChanged>(m => { m.Key = setting.Key; m.OldValue = oldValue; m.NewValue = setting.Value; });
{'fraction_non_alphanumeric': 0.12024001846295869, 'fraction_numerical': 0.0, 'mean_word_length': 1.8216145833333333, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3019149', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1117, 'n_words': 234}