mgtzm/src/request.zig

122 lines
3.5 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-11-05 01:37:58 +00:00
const util = @import("util.zig");
2022-10-27 00:54:00 +00:00
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-11-05 03:23:28 +00:00
if (jobj.objectGet("delete") catch null) |*jaction| {
var ret = try delete(jaction, db, allocator);
jret.objectAdd("deleted", &ret);
}
std.debug.print("{s}\n", .{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 {
2022-11-05 03:23:28 +00:00
// TODO: Have into account limits so it is scalable
// TODO: Do not fetch EVERY id at once, iterate where possible
2022-10-31 07:55:35 +00:00
const query_str = jobj.getString();
var jret = json.Obj.newArray();
2022-11-05 01:37:58 +00:00
var ids = std.ArrayList([]u8).init(allocator);
defer {
for (ids.items) |item| {
Db.free(item.ptr);
}
ids.deinit();
}
2022-11-05 03:38:33 +00:00
if (query_str.len > 0) {
// Get all the items under the individual tags
var tag_iter = std.mem.split(u8, query_str, " ");
while (tag_iter.next()) |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-11-05 03:38:33 +00:00
// Get the items that have that tag: "<item1> <item2> <item3>"
try ids.append(db.get(tag_sel) orelse return jret);
2022-11-05 03:38:33 +00:00
// This has been deferred before.
//defer Db.free(tag_str.ptr);
}
} else {
try ids.append(db.get("item") orelse return jret);
2022-11-05 01:37:58 +00:00
}
2022-11-05 01:37:58 +00:00
var id_iter = util.intersection(ids.items);
while (id_iter.next()) |item_id| {
2022-11-05 03:23:28 +00:00
var item = (try Item.getById(item_id, db, allocator)) orelse continue;
defer item.deinit();
2022-11-05 01:37:58 +00:00
jret.arrayAdd(&item.toJson());
}
return jret;
}
2022-11-05 03:23:28 +00:00
pub fn delete(jobj: *json.Obj, db: *Db, allocator: std.mem.Allocator) !json.Obj {
var jret = json.Obj.newArray();
// Go over each tag
var id_iter = jobj.arrayGetIterator();
while (id_iter.next()) |*jid| {
const id = jid.getString();
// TODO: Return some kind of error or somethign telling that some were not found
var item = (try Item.getById(id, db, allocator)) orelse continue;
defer item.deinit();
jret.arrayAdd(&item.toJson());
try item.delete(db);
}
return jret;
}