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

67 lines
1.8 KiB
Zig
Raw Normal View History

2022-12-07 07:12:11 +00:00
const util = @import("util");
2022-12-07 09:59:46 +00:00
const api = @import("api");
2022-09-05 08:52:49 +00:00
2022-09-08 06:56:29 +00:00
pub const create = struct {
pub const method = .POST;
2022-11-27 01:52:30 +00:00
pub const path = "/users";
2022-09-05 08:52:49 +00:00
2022-10-11 03:28:23 +00:00
pub const Body = struct {
username: []const u8,
password: []const u8,
invite_code: ?[]const u8 = null,
email: ?[]const u8 = null,
};
2022-09-05 08:52:49 +00:00
2022-10-11 03:28:23 +00:00
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const options = .{
2023-01-08 23:35:58 +00:00
.username = req.body.username,
.password = req.body.password,
2022-10-11 03:28:23 +00:00
.invite_code = req.body.invite_code,
.email = req.body.email,
};
2023-01-08 23:35:58 +00:00
const user = srv.register(options) catch |err| switch (err) {
2022-10-11 03:28:23 +00:00
error.UsernameTaken => return res.err(.unprocessable_entity, "Username Unavailable", {}),
2022-09-08 06:56:29 +00:00
else => return err,
};
2022-09-05 08:52:49 +00:00
2022-10-11 03:28:23 +00:00
try res.json(.created, user);
2022-09-08 06:56:29 +00:00
}
};
2022-12-07 07:12:11 +00:00
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 {
2023-01-04 19:03:23 +00:00
const result = try srv.getActor(req.args.id);
2022-12-07 07:12:11 +00:00
defer util.deepFree(srv.allocator, result);
try res.json(.ok, result);
}
};
2022-12-07 09:59:46 +00:00
pub const update_profile = struct {
pub const method = .PUT;
pub const path = "/users/:id";
pub const Args = struct {
id: util.Uuid,
};
2023-01-04 19:03:23 +00:00
pub const Body = api.actors.ProfileUpdateArgs;
2022-12-07 09:59:46 +00:00
2023-01-04 19:03:23 +00:00
// TODO: I don't like that the request body and response body are substantially different
2022-12-07 09:59:46 +00:00
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
try srv.updateUserProfile(req.args.id, req.body);
2023-01-04 19:03:23 +00:00
const result = try srv.getActor(req.args.id);
2022-12-07 09:59:46 +00:00
defer util.deepFree(srv.allocator, result);
try res.json(.ok, result);
}
};