From b0f730d1625caadc0d0bbe3385a61a782b37d3e9 Mon Sep 17 00:00:00 2001 From: Dendy Date: Sat, 5 Nov 2022 04:38:33 +0100 Subject: [PATCH] Implement searching no tag --- src/main.zig | 2 +- src/request.zig | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main.zig b/src/main.zig index f19a2ad..90d8d00 100644 --- a/src/main.zig +++ b/src/main.zig @@ -60,7 +60,7 @@ pub fn main() !void { // -------------------- QUERY --------------------------- const jsonQuery: [:0]const u8 = - \\{ "query" : "fur made_with:krita date:2022", "limit" : 20 } + \\{ "query" : "" } ; var jquery = json.Obj.newFromString(jsonQuery); diff --git a/src/request.zig b/src/request.zig index c954293..82dfe88 100644 --- a/src/request.zig +++ b/src/request.zig @@ -63,9 +63,6 @@ pub fn query(jobj: *json.Obj, db: *Db, allocator: std.mem.Allocator) !json.Obj { const query_str = jobj.getString(); var jret = json.Obj.newArray(); - // Go through each tag - var tag_iter = std.mem.split(u8, query_str, " "); - var ids = std.ArrayList([]u8).init(allocator); defer { for (ids.items) |item| { @@ -74,16 +71,22 @@ pub fn query(jobj: *json.Obj, db: *Db, allocator: std.mem.Allocator) !json.Obj { ids.deinit(); } - while (tag_iter.next()) |tag| { - // Get the tag selector: "tag:" - const tag_sel = try std.mem.concat(allocator, u8, &[_][]const u8{ "tag:", tag }); - defer allocator.free(tag_sel); + 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:" + const tag_sel = try std.mem.concat(allocator, u8, &[_][]const u8{ "tag:", tag }); + defer allocator.free(tag_sel); - // Get the items that have that tag: " " - try ids.append(db.get(tag_sel) orelse return jret); + // Get the items that have that tag: " " + try ids.append(db.get(tag_sel) orelse return jret); - // This has been deferred before. - //defer Db.free(tag_str.ptr); + // This has been deferred before. + //defer Db.free(tag_str.ptr); + } + } else { + try ids.append(db.get("item") orelse return jret); } var id_iter = util.intersection(ids.items);