46 lines
1.4 KiB
Zig
46 lines
1.4 KiB
Zig
const std = @import("std");
|
|
const util = @import("util");
|
|
const types = @import("../types.zig");
|
|
|
|
const Uuid = util.Uuid;
|
|
const DateTime = util.DateTime;
|
|
const QueryResult = types.QueryResult;
|
|
const Community = types.Community;
|
|
|
|
pub fn methods(comptime models: type) type {
|
|
return struct {
|
|
pub fn createCommunity(self: anytype, origin: []const u8, name: ?[]const u8) !Community {
|
|
if (!self.isAdmin()) {
|
|
return error.PermissionDenied;
|
|
}
|
|
|
|
const tx = try self.db.begin();
|
|
errdefer tx.rollback();
|
|
const community_id = try models.communities.create(
|
|
tx,
|
|
origin,
|
|
.{ .name = name },
|
|
self.allocator,
|
|
);
|
|
|
|
const community = models.communities.get(
|
|
tx,
|
|
community_id,
|
|
self.allocator,
|
|
) catch |err| return switch (err) {
|
|
error.NotFound => error.DatabaseError,
|
|
else => |err2| err2,
|
|
};
|
|
|
|
try tx.commit();
|
|
|
|
return community;
|
|
}
|
|
|
|
pub fn queryCommunities(self: anytype, args: Community.QueryArgs) !QueryResult(Community) {
|
|
if (!self.context.isAdmin()) return error.PermissionDenied;
|
|
return try models.communities.query(self.db, args, self.allocator);
|
|
}
|
|
};
|
|
}
|