mgtzm/src/request.zig

35 lines
1011 B
Zig
Raw Normal View History

2022-10-27 00:54:00 +00:00
const std = @import("std");
const sqlite = @import("sqlite");
const Db = @import("Db.zig");
const Item = @import("Item.zig");
const Tag = @import("Tag.zig");
const json = @import("json.zig");
pub fn process(jobj: *json.Obj, db: *sqlite.Db) !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
// Test the action to carry and pass the object
if (jobj.objectGet("add") catch null) |*jadd| {
try add(jadd, db, allocator);
}
}
pub fn add(jobj: *json.Obj, db: *sqlite.Db, allocator: std.mem.Allocator) !void {
// TODO: Maybe return error when no items in the array?
var iter = jobj.arrayGetIterator();
while(iter.next()) |*jtags| {
var item = Item {
.id = null,
.tags = try Item.tagsFromJson(jtags, allocator),
};
defer item.deinit();
// TODO: Return the things that have been added with ID
try item.persist(db);
}
}