fediglam/src/main/api.zig

97 lines
2.5 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-15 00:58:08 +00:00
const sql = @import("sql");
2022-07-13 03:40:48 +00:00
const db = @import("./db.zig");
pub const models = @import("./models.zig");
pub const free = db.free;
2022-07-13 04:16:33 +00:00
pub const Uuid = util.Uuid;
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 {
2022-07-13 03:40:48 +00:00
db: db.Database,
2022-07-13 04:16:33 +00:00
prng: std.rand.DefaultPrng,
2022-07-13 03:40:48 +00:00
last_id: u64 = 0,
2022-07-15 00:58:08 +00:00
db2: sql.Sqlite,
2022-07-13 03:40:48 +00:00
pub fn init(alloc: std.mem.Allocator) !ApiServer {
return ApiServer{
.db = try db.Database.init(alloc),
2022-07-13 04:16:33 +00:00
.prng = std.rand.DefaultPrng.init(1998),
2022-07-15 00:58:08 +00:00
.db2 = try sql.Sqlite.open("./test.db"),
2022-07-13 03:40:48 +00:00
};
}
2022-07-13 04:16:33 +00:00
fn genUuid(self: *ApiServer, alloc: std.mem.Allocator) ![]const u8 {
2022-07-13 03:40:48 +00:00
self.last_id += 1;
return std.fmt.allocPrint(alloc, "{}", .{self.last_id});
}
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-13 05:08:18 +00:00
try self.db.notes.put(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 {
2022-07-13 05:35:39 +00:00
try self.db.users.lock();
defer self.db.users.unlock();
// TODO; real queries
2022-07-13 04:56:47 +00:00
const id = Uuid.randV4(self.prng.random());
2022-07-13 05:35:39 +00:00
// check for handle dupes
var iter = self.db.users.data.iterator();
while (iter.next()) |it| {
if (std.mem.eql(u8, it.value_ptr.handle, info.handle)) {
return error.DuplicateHandle;
}
}
// TODO: check for id dupes
2022-07-13 04:56:47 +00:00
const user = reify(models.User, id, info);
2022-07-13 05:08:18 +00:00
try self.db.users.put(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-13 05:08:18 +00:00
return self.db.notes.get(id, alloc);
2022-07-13 03:40:48 +00:00
}
};