public async Task HandleUpdates(HttpContext httpContext, ITransactor transactor) { string result = null; int queries = GetQueriesValue(httpContext); TransactOptions opts = new TransactOptions(OnConflict); IWorld[] results = new IWorld[queries]; int[] wantedIds = new int[queries]; for (int i = 0; i < queries; i++) { wantedIds[i] = GetRandomWantedId(); } await transactor.TransactAsync(db => { for (int i = 0; i < queries; i++) { IWorld w = GetRandomWorld(db, wantedIds[i]); w.RandomNumber = _random.Next(1, World.Count + 1); results[i] = new WorldOrm(w); } }, opts); result = JsonConvert.SerializeObject(results, _jsonSerializerSettings); httpContext.Response.StatusCode = StatusCodes.Status200OK; httpContext.Response.ContentType = "application/json"; httpContext.Response.ContentLength = result.Length; await httpContext.Response.WriteAsync(result); }
/// <summary> /// This function is needed to implement `update` action verification by Techempower. /// </summary> /// <param name="httpContext"></param> /// <returns></returns> public async Task HandleSelectWorlds(HttpContext httpContext, ITransactor transactor) { string json = null; Dictionary <int, int> data = null; await transactor.TransactAsync(db => { data = db.Sql <World>("SELECT w FROM Starcounter.Techempower.World w").ToDictionary(k => k.Id, v => v.RandomNumber); }); json = JsonConvert.SerializeObject(data, _jsonSerializerSettings); await httpContext.Response.WriteAsync(json); }
public async Task HandleFortunes(HttpContext httpContext, ITransactor transactor) { List <IFortune> fortunes = null; await transactor.TransactAsync(db => { fortunes = new List <IFortune>(); foreach (Fortune f in db.Sql <Fortune>("SELECT f FROM Starcounter.Techempower.Fortune f")) { fortunes.Add(new FortuneOrm(f)); } Debug.Assert(fortunes.Count == Fortune.Count); fortunes.Add(new FortuneOrm() { Message = "Additional fortune added at request time." }); }); fortunes.Sort(); await RenderFortunesHtml(fortunes, httpContext, _htmlEncoder); }