diff options
Diffstat (limited to 'backend/src/services/common.zig')
-rw-r--r-- | backend/src/services/common.zig | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/backend/src/services/common.zig b/backend/src/services/common.zig new file mode 100644 index 0000000..42d18e9 --- /dev/null +++ b/backend/src/services/common.zig @@ -0,0 +1,30 @@ +const std = @import("std"); +const zqlite = @import("zqlite"); +const httpz = @import("httpz"); + +const users_repo = @import("../repos/users_repo.zig"); + +pub const Env = struct { + conn: zqlite.Conn, + secure_tokens: bool, + user: ?users_repo.User, +}; + +pub const ServiceError = error{ + BadRequest, + NotFound, + Forbidden, +}; + +pub fn with_body(comptime T: type, req: *httpz.Request) !T { + if (req.body()) |body| { + if (std.json.parseFromSlice(T, req.arena, body, .{})) |parsed| { + defer parsed.deinit(); + return parsed.value; + } else |_| { + return ServiceError.BadRequest; + } + } else { + return ServiceError.BadRequest; + } +} |