aboutsummaryrefslogtreecommitdiffstats
path: root/src/web/picks/pick.zig
blob: 51e05c4db96ca7cd97b6679c486108b27b7bb37d (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
const Team = struct {
    team_id: i16,
    rank: ?i16,
    name: []const u8,
    division: []const u8,
    league_user_id: ?i32,
};

pub fn handler(env: *Env, req: *httpz.Request, res: *httpz.Response) !void {
    const query = try req.query();
    if (query.get("team_id")) |team_id| {
        if (req.params.get("id")) |draft_id| {
            var result = try env.app.pool.query("SELECT cr.*, picks.league_user_id from current_rankings cr left join picks on cr.team_id = picks.team_id AND picks.draft_id = $1 WHERE cr.team_id = $2", .{ draft_id, team_id });
            defer result.deinit();

            var team: Team = undefined;
            while (try result.next()) |row| {
                team = try row.to(Team, .{});
            }
            try web.renderWithDataPartials("pick", .{}, .{
                .team_id = team.team_id,
                .rank = team.rank,
                .name = team.name,
                .division = team.division,
                .league_user_id = team.league_user_id,
                .draft_id = draft_id,
            }, req, res);
        }
    } else {
        @panic("oh no");
    }
}

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