2022-10-11 03:28:23 +00:00
|
|
|
const api = @import("api");
|
2022-12-16 09:04:58 +00:00
|
|
|
const util = @import("util");
|
2022-11-15 05:38:08 +00:00
|
|
|
const controller_utils = @import("../../controllers.zig").helpers;
|
2022-10-11 04:49:36 +00:00
|
|
|
|
2023-01-04 19:03:23 +00:00
|
|
|
const QueryArgs = api.communities.QueryArgs;
|
2022-09-08 02:19:46 +00:00
|
|
|
|
|
|
|
pub const create = struct {
|
2022-09-08 06:56:29 +00:00
|
|
|
pub const method = .POST;
|
2022-11-27 01:52:30 +00:00
|
|
|
pub const path = "/communities";
|
2022-09-08 02:19:46 +00:00
|
|
|
|
2022-10-11 03:28:23 +00:00
|
|
|
pub const Body = struct {
|
|
|
|
origin: []const u8,
|
2022-12-12 03:52:11 +00:00
|
|
|
name: ?[]const u8 = null,
|
2022-10-11 03:28:23 +00:00
|
|
|
};
|
2022-09-08 02:19:46 +00:00
|
|
|
|
2022-10-11 03:28:23 +00:00
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
2022-12-12 03:52:11 +00:00
|
|
|
const invite = try srv.createCommunity(req.body.origin, req.body.name);
|
2022-09-08 02:19:46 +00:00
|
|
|
|
2022-10-11 03:28:23 +00:00
|
|
|
try res.json(.created, invite);
|
2022-09-08 02:19:46 +00:00
|
|
|
}
|
|
|
|
};
|
2022-10-11 04:49:36 +00:00
|
|
|
|
|
|
|
pub const query = struct {
|
|
|
|
pub const method = .GET;
|
2022-11-27 01:52:30 +00:00
|
|
|
pub const path = "/communities";
|
2022-10-11 04:49:36 +00:00
|
|
|
|
2022-12-16 09:04:58 +00:00
|
|
|
pub const Query = struct {
|
2023-01-04 19:03:23 +00:00
|
|
|
const OrderBy = QueryArgs.OrderBy;
|
|
|
|
const Direction = QueryArgs.Direction;
|
|
|
|
const PageDirection = QueryArgs.PageDirection;
|
2022-12-16 09:04:58 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
};
|
2022-10-11 04:49:36 +00:00
|
|
|
|
2022-11-14 04:40:13 +00:00
|
|
|
pub fn handler(req: anytype, res: anytype, srv: anytype) !void {
|
2022-12-16 09:04:58 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
const convert = struct {
|
2023-01-04 19:03:23 +00:00
|
|
|
fn func(args: QueryArgs) Query {
|
2022-12-16 09:04:58 +00:00
|
|
|
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;
|
2022-10-11 04:49:36 +00:00
|
|
|
|
2022-12-16 09:04:58 +00:00
|
|
|
try controller_utils.paginate(.{
|
|
|
|
.items = results.items,
|
|
|
|
.next_page = convert(results.next_page),
|
|
|
|
.prev_page = convert(results.prev_page),
|
|
|
|
}, res, req.allocator);
|
2022-10-11 04:49:36 +00:00
|
|
|
}
|
|
|
|
};
|