Making the api work again

This commit is contained in:
jaina heartles 2022-10-03 23:38:26 -07:00
parent 69a51a7d7f
commit b12b9c766f

View file

@ -1,9 +1,9 @@
const root = @import("root"); const root = @import("root");
const std = @import("std");
const http = @import("http"); const http = @import("http");
const Uuid = @import("util").Uuid; const Uuid = @import("util").Uuid;
const utils = @import("../controllers.zig").utils; const utils = @import("../controllers.zig").utils;
const InviteRequest = @import("../api.zig").InviteRequest;
const RequestServer = root.RequestServer; const RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs; const RouteArgs = http.RouteArgs;
@ -12,25 +12,30 @@ pub const create = struct {
pub const path = "/invites"; pub const path = "/invites";
pub const method = .POST; pub const method = .POST;
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { 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); const opt = try utils.parseRequestBody(InviteRequest, ctx);
defer utils.freeRequestBody(opt, ctx.alloc); defer utils.freeRequestBody(opt, ctx.alloc);
var api = try utils.getApiConn(srv, ctx); var api = try utils.getApiConn(srv, ctx);
defer api.close(); 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); 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);
}