mgtzm/src/request.zig

82 lines
2.4 KiB
Zig
Raw Normal View History

2022-10-27 00:54:00 +00:00
const std = @import("std");
const Db = @import("Db.zig");
const Item = @import("Item.zig");
const Tag = @import("Tag.zig");
const json = @import("json.zig");
2022-10-31 07:55:35 +00:00
pub fn process(jobj: *json.Obj, db: *Db) !void {
2022-10-27 00:54:00 +00:00
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
2022-10-27 01:33:47 +00:00
var jret = json.Obj.newObject();
defer jret.deinit();
2022-10-27 00:54:00 +00:00
// Test the action to carry and pass the object
if (jobj.objectGet("add") catch null) |*jaction| {
var ret = try add(jaction, db, allocator);
2022-10-27 01:33:47 +00:00
jret.objectAdd("added", &ret);
2022-10-27 00:54:00 +00:00
}
2022-10-27 01:33:47 +00:00
if (jobj.objectGet("query") catch null) |*jaction| {
var ret = try query(jaction, db, allocator);
jret.objectAdd("queried", &ret);
}
2022-10-31 07:55:35 +00:00
std.debug.print("{s}", .{jret.toString()});
2022-10-27 00:54:00 +00:00
}
2022-10-31 07:55:35 +00:00
pub fn add(jobj: *json.Obj, db: *Db, allocator: std.mem.Allocator) !json.Obj {
2022-10-27 00:54:00 +00:00
// TODO: Maybe return error when no items in the array?
2022-10-27 01:33:47 +00:00
// Freed by the caller
2022-10-27 01:26:14 +00:00
var jret = json.Obj.newArray();
2022-10-27 00:54:00 +00:00
var iter = jobj.arrayGetIterator();
2022-10-31 07:55:35 +00:00
while (iter.next()) |*jtags| {
var item = Item{
2022-10-27 00:54:00 +00:00
.id = null,
.tags = try Item.tagsFromJson(jtags, allocator),
};
2022-10-27 01:26:14 +00:00
item.deinit();
2022-10-27 00:54:00 +00:00
2022-10-27 01:26:14 +00:00
// Insert new items into the DB
2022-10-31 07:55:35 +00:00
try item.persist(db, allocator);
2022-10-27 01:26:14 +00:00
// Add item to new json array (Makes a deep copy, freed with jret.deinit())
jret.arrayAdd(&item.toJson());
2022-10-27 00:54:00 +00:00
}
2022-10-27 01:26:14 +00:00
2022-10-27 01:33:47 +00:00
return jret;
2022-10-27 00:54:00 +00:00
}
2022-10-31 07:55:35 +00:00
pub fn query(jobj: *json.Obj, db: *Db, allocator: std.mem.Allocator) !json.Obj {
const query_str = jobj.getString();
var jret = json.Obj.newArray();
2022-10-31 07:55:35 +00:00
// Go through each tag
var iter = std.mem.split(u8, query_str, " ");
const opt_tag = iter.next();
2022-10-31 07:55:35 +00:00
if (opt_tag) |tag| {
// Get the tag selector: "tag:<tag>"
const tag_sel = try std.mem.concat(allocator, u8, &[_][]const u8{"tag:", tag});
defer allocator.free(tag_sel);
2022-10-31 07:55:35 +00:00
// Get the items that have that tag: "<item1> <item2> <item3>"
const tag_str = db.get(tag_sel) orelse return jret;
defer Db.free(tag_str.ptr);
2022-10-31 07:55:35 +00:00
// Iterate through the items id
var item_iter = std.mem.split(u8, tag_str, " ");
while (item_iter.next()) |item_id| {
const item = (try Item.getById(item_id, db, allocator)) orelse continue;
2022-10-31 07:55:35 +00:00
jret.arrayAdd(&item.toJson());
}
}
return jret;
}