From b12b9c766f67c0f3c8e74a0404ab0c7bd41cd514 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Mon, 3 Oct 2022 23:38:26 -0700 Subject: [PATCH] Making the api work again --- src/main/controllers/invites.zig | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/controllers/invites.zig b/src/main/controllers/invites.zig index 8d5ce25..d8d6ef5 100644 --- a/src/main/controllers/invites.zig +++ b/src/main/controllers/invites.zig @@ -1,9 +1,9 @@ const root = @import("root"); +const std = @import("std"); const http = @import("http"); const Uuid = @import("util").Uuid; const utils = @import("../controllers.zig").utils; -const InviteRequest = @import("../api.zig").InviteRequest; const RequestServer = root.RequestServer; const RouteArgs = http.RouteArgs; @@ -12,25 +12,30 @@ pub const create = struct { pub const path = "/invites"; pub const method = .POST; pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { + // TODO: get rid of this temporary struct, find json library that lets me extend + // it to parse UUIDs/etc in place + const InviteRequest = struct { + const Kind = @import("../api.zig").InviteRequest.Kind; + + name: ?[]const u8 = null, + + // admin only options + kind: Kind = .user, + to_community: ?[]const u8 = null, + }; + const opt = try utils.parseRequestBody(InviteRequest, ctx); defer utils.freeRequestBody(opt, ctx.alloc); var api = try utils.getApiConn(srv, ctx); defer api.close(); - const invite = try api.createInvite(opt); + const invite = try api.createInvite(.{ + .name = opt.name, + .kind = opt.kind, + .to_community = if (opt.to_community) |id| try Uuid.parse(id) else null, + }); try utils.respondJson(ctx, .created, invite); } }; - -pub fn get(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void { - const id_str = args.get("id") orelse return error.NotFound; - const id = Uuid.parse(id_str) catch return utils.respondError(ctx, .bad_request, "Invalid UUID"); - var api = try utils.getApiConn(srv, ctx); - defer api.close(); - - const invite = (try api.getInvite(id)) orelse return utils.respondError(ctx, .not_found, "Invite not found"); - - try utils.respondJson(ctx, .ok, invite); -}