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

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| {
            _ = try env.app.pool.exec("CALL auto_draft($1)", .{draft_id});
            _ = try env.app.pool.exec("INSERT INTO picks (league_user_id, draft_id, team_id) VALUES (current_picker($1), $1, $2)", .{ draft_id, team_id });
            var result = try env.app.pool.query("SELECT cr.*, picks.league_user_id, users.name AS pick_user FROM current_rankings cr LEFT JOIN picks ON cr.team_id = picks.team_id AND picks.draft_id = $1 LEFT JOIN league_users USING(league_user_id) LEFT JOIN users USING (user_id) 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,
                .pick_user = team.pick_user,
                .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");