fediglam/src/main/api.zig

94 lines
2.6 KiB
Zig
Raw Normal View History

2022-07-13 03:40:48 +00:00
const std = @import("std");
2022-07-13 04:16:33 +00:00
const util = @import("util");
2022-07-13 03:40:48 +00:00
const db = @import("./db.zig");
pub const models = @import("./models.zig");
2022-07-13 04:16:33 +00:00
pub const Uuid = util.Uuid;
2022-07-13 03:40:48 +00:00
2022-07-16 18:44:46 +00:00
// Frees an api struct and its fields allocated from alloc
pub fn free(alloc: std.mem.Allocator, val: anytype) void {
inline for (std.meta.fields(@TypeOf(val))) |f| {
// TODO
if (f.field_type == []u8 or f.field_type == []const u8) {
alloc.free(@field(val, f.name));
} else if (f.field_type == Uuid) {
// nothing
} else {
@compileError("unsupported field type " ++ @typeName(f.field_type));
}
}
}
2022-07-13 03:40:48 +00:00
pub fn CreateInfo(comptime T: type) type {
const t_fields = std.meta.fields(T);
var fields: [t_fields.len - 1]std.builtin.Type.StructField = undefined;
var count = 0;
inline for (t_fields) |f| {
if (std.mem.eql(u8, f.name, "id")) continue;
fields[count] = f;
count += 1;
}
return @Type(.{ .Struct = .{
.layout = .Auto,
.fields = &fields,
.decls = &[0]std.builtin.Type.Declaration{},
.is_tuple = false,
} });
}
2022-07-13 04:16:33 +00:00
fn reify(comptime T: type, id: Uuid, val: CreateInfo(T)) T {
2022-07-13 03:40:48 +00:00
var result: T = undefined;
result.id = id;
inline for (std.meta.fields(CreateInfo(T))) |f| {
@field(result, f.name) = @field(val, f.name);
}
return result;
}
2022-07-13 04:16:33 +00:00
pub const ApiServer = struct {
prng: std.rand.DefaultPrng,
2022-07-15 07:27:27 +00:00
db: db.Database,
2022-07-15 00:58:08 +00:00
2022-07-16 18:44:46 +00:00
pub fn init(_: std.mem.Allocator) !ApiServer {
2022-07-13 03:40:48 +00:00
return ApiServer{
2022-07-16 18:41:09 +00:00
.prng = std.rand.DefaultPrng.init(@bitCast(u64, std.time.milliTimestamp())),
2022-07-15 07:27:27 +00:00
.db = try db.Database.init(),
2022-07-13 03:40:48 +00:00
};
}
2022-07-13 04:28:54 +00:00
pub fn createNote(self: *ApiServer, info: CreateInfo(models.Note)) !models.Note {
2022-07-13 04:16:33 +00:00
const id = Uuid.randV4(self.prng.random());
2022-07-13 04:28:54 +00:00
// TODO: check for dupes
2022-07-13 03:40:48 +00:00
const note = reify(models.Note, id, info);
2022-07-16 18:41:09 +00:00
try self.db.insert(models.Note, note);
2022-07-13 03:40:48 +00:00
2022-07-13 04:28:54 +00:00
return note;
2022-07-13 03:40:48 +00:00
}
2022-07-13 04:56:47 +00:00
pub fn createUser(self: *ApiServer, info: CreateInfo(models.User)) !models.User {
const id = Uuid.randV4(self.prng.random());
2022-07-13 05:35:39 +00:00
2022-07-16 19:30:47 +00:00
if (try self.db.existsWhereEq(models.User, .handle, info.handle)) {
return error.HandleNotAvailable;
}
2022-07-13 04:56:47 +00:00
const user = reify(models.User, id, info);
2022-07-16 18:41:09 +00:00
try self.db.insert(models.User, user);
2022-07-13 04:56:47 +00:00
return user;
}
2022-07-13 04:16:33 +00:00
pub fn getNote(self: *ApiServer, id: Uuid, alloc: std.mem.Allocator) !?models.Note {
2022-07-16 19:30:47 +00:00
return self.db.getBy(models.Note, .id, id, alloc);
2022-07-16 18:41:09 +00:00
}
pub fn getUser(self: *ApiServer, id: Uuid, alloc: std.mem.Allocator) !?models.User {
2022-07-16 19:30:47 +00:00
return self.db.getBy(models.User, .id, id, alloc);
2022-07-13 03:40:48 +00:00
}
};