fediglam/src/main/controllers/api/users.zig

43 lines
1.1 KiB
Zig

const util = @import("util");
pub const create = struct {
pub const method = .POST;
pub const path = "/users";
pub const Body = struct {
username: []const u8,
password: []const u8,
invite_code: ?[]const u8 = null,
email: ?[]const u8 = null,
};
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const options = .{
.invite_code = req.body.invite_code,
.email = req.body.email,
};
const user = srv.register(req.body.username, req.body.password, options) catch |err| switch (err) {
error.UsernameTaken => return res.err(.unprocessable_entity, "Username Unavailable", {}),
else => return err,
};
try res.json(.created, user);
}
};
pub const get = struct {
pub const method = .GET;
pub const path = "/users/:id";
pub const Args = struct {
id: util.Uuid,
};
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const result = try srv.getUser(req.args.id);
defer util.deepFree(srv.allocator, result);
try res.json(.ok, result);
}
};