|
| 1 | +using System.Text.Json; |
| 2 | + |
| 3 | +static class Handlers |
| 4 | +{ |
| 5 | + public static IResult GetBaseline(HttpRequest req) |
| 6 | + { |
| 7 | + return Results.Text(SumQuery(req).ToString()); |
| 8 | + } |
| 9 | + |
| 10 | + public static async Task<IResult> PostBaseline(HttpRequest req) |
| 11 | + { |
| 12 | + int sum = SumQuery(req); |
| 13 | + using var reader = new StreamReader(req.Body); |
| 14 | + var body = await reader.ReadToEndAsync(); |
| 15 | + if (int.TryParse(body, out int b)) |
| 16 | + sum += b; |
| 17 | + return Results.Text(sum.ToString()); |
| 18 | + } |
| 19 | + |
| 20 | + public static IResult GetBaseline2(HttpRequest req) |
| 21 | + { |
| 22 | + return Results.Text(SumQuery(req).ToString()); |
| 23 | + } |
| 24 | + |
| 25 | + public static IResult Pipeline() |
| 26 | + { |
| 27 | + return Results.Text("ok"); |
| 28 | + } |
| 29 | + |
| 30 | + public static async Task<IResult> Upload(HttpRequest req) |
| 31 | + { |
| 32 | + using var ms = new MemoryStream(); |
| 33 | + await req.Body.CopyToAsync(ms); |
| 34 | + return Results.Text(ms.Length.ToString()); |
| 35 | + } |
| 36 | + |
| 37 | + public static IResult Json() |
| 38 | + { |
| 39 | + if (AppData.DatasetItems == null) |
| 40 | + return Results.Problem("Dataset not loaded"); |
| 41 | + |
| 42 | + var items = new List<ProcessedItem>(AppData.DatasetItems.Count); |
| 43 | + foreach (var item in AppData.DatasetItems) |
| 44 | + { |
| 45 | + items.Add(new ProcessedItem |
| 46 | + { |
| 47 | + Id = item.Id, Name = item.Name, Category = item.Category, |
| 48 | + Price = item.Price, Quantity = item.Quantity, Active = item.Active, |
| 49 | + Tags = item.Tags, Rating = item.Rating, |
| 50 | + Total = Math.Round(item.Price * item.Quantity, 2) |
| 51 | + }); |
| 52 | + } |
| 53 | + return Results.Json(new { items, count = items.Count }); |
| 54 | + } |
| 55 | + |
| 56 | + public static async Task Compression(HttpContext ctx) |
| 57 | + { |
| 58 | + if (AppData.LargeJsonResponse == null) |
| 59 | + { |
| 60 | + ctx.Response.StatusCode = 500; |
| 61 | + return; |
| 62 | + } |
| 63 | + ctx.Response.ContentType = "application/json"; |
| 64 | + await ctx.Response.Body.WriteAsync(AppData.LargeJsonResponse); |
| 65 | + } |
| 66 | + |
| 67 | + public static IResult StaticFile(string filename) |
| 68 | + { |
| 69 | + if (AppData.StaticFiles.TryGetValue(filename, out var sf)) |
| 70 | + return Results.Bytes(sf.Data, sf.ContentType); |
| 71 | + return Results.NotFound(); |
| 72 | + } |
| 73 | + |
| 74 | + public static IResult Database(HttpRequest req) |
| 75 | + { |
| 76 | + if (AppData.DbConnection == null) |
| 77 | + return Results.Problem("DB not available"); |
| 78 | + |
| 79 | + double min = 10, max = 50; |
| 80 | + if (req.Query.ContainsKey("min") && double.TryParse(req.Query["min"], out double pmin)) |
| 81 | + min = pmin; |
| 82 | + if (req.Query.ContainsKey("max") && double.TryParse(req.Query["max"], out double pmax)) |
| 83 | + max = pmax; |
| 84 | + |
| 85 | + using var cmd = AppData.DbConnection.CreateCommand(); |
| 86 | + cmd.CommandText = "SELECT id, name, category, price, quantity, active, tags, rating_score, rating_count FROM items WHERE price BETWEEN @min AND @max LIMIT 50"; |
| 87 | + cmd.Parameters.AddWithValue("@min", min); |
| 88 | + cmd.Parameters.AddWithValue("@max", max); |
| 89 | + using var reader = cmd.ExecuteReader(); |
| 90 | + |
| 91 | + var items = new List<object>(); |
| 92 | + while (reader.Read()) |
| 93 | + { |
| 94 | + items.Add(new |
| 95 | + { |
| 96 | + id = reader.GetInt32(0), |
| 97 | + name = reader.GetString(1), |
| 98 | + category = reader.GetString(2), |
| 99 | + price = reader.GetDouble(3), |
| 100 | + quantity = reader.GetInt32(4), |
| 101 | + active = reader.GetInt32(5) == 1, |
| 102 | + tags = JsonSerializer.Deserialize<List<string>>(reader.GetString(6)), |
| 103 | + rating = new { score = reader.GetDouble(7), count = reader.GetInt32(8) }, |
| 104 | + }); |
| 105 | + } |
| 106 | + return Results.Json(new { items, count = items.Count }); |
| 107 | + } |
| 108 | + |
| 109 | + static int SumQuery(HttpRequest req) |
| 110 | + { |
| 111 | + int sum = 0; |
| 112 | + foreach (var (_, values) in req.Query) |
| 113 | + foreach (var v in values) |
| 114 | + if (int.TryParse(v, out int n)) sum += n; |
| 115 | + return sum; |
| 116 | + } |
| 117 | +} |
0 commit comments