Use new serialization format

This commit is contained in:
jaina heartles 2022-12-16 01:04:58 -08:00
parent 91edd17801
commit b015bb8356

View file

@ -1,4 +1,5 @@
const api = @import("api");
const util = @import("util");
const controller_utils = @import("../../controllers.zig").helpers;
const QueryArgs = api.CommunityQueryArgs;
@ -23,11 +24,85 @@ pub const query = struct {
pub const method = .GET;
pub const path = "/communities";
pub const Query = QueryArgs;
pub const Query = struct {
const OrderBy = api.CommunityQueryArgs.OrderBy;
const Direction = api.CommunityQueryArgs.Direction;
const PageDirection = api.CommunityQueryArgs.PageDirection;
// Max items to fetch
max_items: usize = 20,
// Selection filters
owner_id: ?util.Uuid = null,
like: ?[]const u8 = null,
created_before: ?util.DateTime = null,
created_after: ?util.DateTime = null,
// Ordering parameter
order_by: OrderBy = .created_at,
direction: Direction = .ascending,
// Page start parameter
prev: ?union(OrderBy) {
name: struct {
id: util.Uuid,
name: []const u8,
},
host: struct {
id: util.Uuid,
host: []const u8,
},
created_at: struct {
id: util.Uuid,
created_at: util.DateTime,
},
} = null,
page_direction: PageDirection = .forward,
};
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
const results = try srv.queryCommunities(req.query);
const q = req.query;
const results = try srv.queryCommunities(.{
.max_items = q.max_items,
.owner_id = q.owner_id,
.like = q.like,
.created_before = q.created_before,
.created_after = q.created_after,
.order_by = q.order_by,
.direction = q.direction,
.prev = if (q.prev) |prev| switch (prev) {
.name => |p| .{ .id = p.id, .order_val = .{ .name = p.name } },
.host => |p| .{ .id = p.id, .order_val = .{ .host = p.host } },
.created_at => |p| .{ .id = p.id, .order_val = .{ .created_at = p.created_at } },
} else null,
.page_direction = q.page_direction,
});
try controller_utils.paginate(results, res, req.allocator);
const convert = struct {
fn func(args: api.CommunityQueryArgs) Query {
return .{
.max_items = args.max_items,
.owner_id = args.owner_id,
.like = args.like,
.created_before = args.created_before,
.created_after = args.created_after,
.order_by = args.order_by,
.direction = args.direction,
.prev = if (args.prev) |prev| switch (prev.order_val) {
.name => |v| .{ .name = .{ .id = prev.id, .name = v } },
.host => |v| .{ .host = .{ .id = prev.id, .host = v } },
.created_at => |v| .{ .created_at = .{ .id = prev.id, .created_at = v } },
} else null,
.page_direction = args.page_direction,
};
}
}.func;
try controller_utils.paginate(.{
.items = results.items,
.next_page = convert(results.next_page),
.prev_page = convert(results.prev_page),
}, res, req.allocator);
}
};