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(); var jret = json.Obj.newObject(); defer jret.deinit(); // Test the action to carry and pass the object if (jobj.objectGet("add") catch null) |*jadd| { var ret = try add(jadd, db, allocator); jret.objectAdd("added", &ret); } std.debug.print("{s}", .{ jret.toString() }); } pub fn add(jobj: *json.Obj, db: *sqlite.Db, allocator: std.mem.Allocator) !json.Obj { // TODO: Maybe return error when no items in the array? // Freed by the caller var jret = json.Obj.newArray(); var iter = jobj.arrayGetIterator(); while(iter.next()) |*jtags| { var item = Item { .id = null, .tags = try Item.tagsFromJson(jtags, allocator), }; item.deinit(); // Insert new items into the DB try item.persist(db); // Add item to new json array (Makes a deep copy, freed with jret.deinit()) jret.arrayAdd(&item.toJson()); } return jret; }