aboutsummaryrefslogtreecommitdiffstats
path: root/src/web/leagues/invite.zig
blob: 66352ccae360f9879ccdd3a80df69c8492a664c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const League = struct {
    name: []const u8,
};

const LeaguePickScore = struct {
    league_id: i32,
    team: []const u8,
    player: []const u8,
    win: i32,
    playoffs: ?i16,
    divisional: ?i16,
    conference: ?i16,
    superbowl: ?i16,
    champion: ?i16,
    total: i32,
};

const PlayerScore = struct {
    league_id: i32,
    player: []const u8,
    score: i32,
};

pub fn handler(env: *Env, req: *httpz.Request, res: *httpz.Response) !void {
    if (req.params.get("id")) |league_id| {
        var league: League = undefined;
        const league_query =
            \\SELECT name FROM leagues WHERE league_id = $1
        ;
        {
            const row = try env.app.pool.row(league_query, .{league_id});
            if (row) |r| {
                league = try r.to(League, .{});
            }
        }

        const scores_query =
            \\SELECT * FROM league_pick_scores WHERE league_id = $1
        ;
        var scores_result = try env.app.pool.query(scores_query, .{league_id});
        defer scores_result.deinit();

        var scores: [32]LeaguePickScore = undefined;
        {
            var i: u8 = 0;
            while (try scores_result.next()) |row| : (i += 1) {
                const score = try row.to(LeaguePickScore, .{});
                scores[i] = score;
            }
        }

        const player_scores_query =
            \\SELECT * FROM player_scores WHERE league_id = $1
        ;
        var player_scores_result = try env.app.pool.query(player_scores_query, .{league_id});
        defer player_scores_result.deinit();

        var player_scores: [2]PlayerScore = undefined;
        {
            var i: u8 = 0;
            while (try player_scores_result.next()) |row| : (i += 1) {
                const score = try row.to(PlayerScore, .{});
                player_scores[i] = score;
            }
        }

        try web.renderWithData("league", .{
            .league = league,
            .scores = scores,
            .player_scores = player_scores,
        }, req, res);
    }
}

const httpz = @import("httpz");
const Env = @import("../../env.zig").Env;
const web = @import("../../web/web.zig");
const std = @import("std");