public static byte[] ConfigToByteArray(ConfigSettings obj) { if (obj == null) return null; using (var ms = new MemoryStream()) { var bf = new BinaryFormatter(); bf.Serialize(ms, obj); return ms.ToArray(); } }
public SettingsModule() : base("/api") { Get["/settings/{id}"] = _ => { var s = _.id; var g = Guid.Parse(s); User u = User.LoadUser(g); var array = u.GetApplicationSettings(Program.Title); if (array.Length == 0) { var config = new ConfigSettings { ScreenerExpiry = 20, TimePeriod = 1, AvgScreenY = 3, AvgScreenR = 6, AvgSearchY = 20, AvgSearchR = 40, RejectRateY = 6.0, RejectRateR = 10.0, ThroughputY = 60.0, ThroughputR = 80.0, DailyRejectY = 6.0, DailyRejectR = 8.0, TimeHealthyY = 90.0, TimeHealthyR = 85.0, TimeBlockedY = 1.0, TimeBlockedR = 2.0, TimeStarvedY = 1.0, TimeStarvedR = 2.0, TimeFaultY = 1.0, TimeFaultR = 2.0 }; var b = Serialization.ConfigToByteArray(config); u.SetApplicationSettings(b, Program.Title); return Response.AsJson(config); } var settings = Serialization.ArrayToConfig(array); return Response.AsJson(settings); }; Post["settingspost/{user}"] = _ => { string s = _.user; var g = Guid.Parse(s); var u = User.LoadUser(g); var posts = this.Bind<ConfigSettings>(); var b = Serialization.ConfigToByteArray(posts); u.SetApplicationSettings(b, Program.Title); return HttpStatusCode.OK; }; }