diff options
| author | physcik <mynameisgennadiy@vk.com> | 2026-04-15 22:16:17 +0500 |
|---|---|---|
| committer | physcik <mynameisgennadiy@vk.com> | 2026-04-15 22:16:17 +0500 |
| commit | 9848b5589773a2d3e172fe72285b9203c36fd260 (patch) | |
| tree | a079f11e46c1e1eed76ff84d94dc90303dd05c9c /backend/src/API/AuthenticationAPI.zig | |
| parent | df1052bad682d957a60ded13cd6243bd44ca83e2 (diff) | |
Register endpoint
Diffstat (limited to 'backend/src/API/AuthenticationAPI.zig')
| -rw-r--r-- | backend/src/API/AuthenticationAPI.zig | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/backend/src/API/AuthenticationAPI.zig b/backend/src/API/AuthenticationAPI.zig new file mode 100644 index 0000000..23a979f --- /dev/null +++ b/backend/src/API/AuthenticationAPI.zig @@ -0,0 +1,30 @@ +const std = @import("std"); +const httpz = @import("httpz"); +const model = @import("../Models/User.zig"); +const db = @import("../Database/Connection.zig"); +const errDesc = @import("ErrorDescription.zig"); +const Handler = @import("../Handler.zig"); + +pub fn RegisterEndpoints(router: *httpz.Router(*Handler.Handler,*const fn (*Handler.Handler, *httpz.request.Request, *httpz.response.Response) anyerror!void)) void { + router.post("/auth/register", register, .{}); +} + +fn register(_: *Handler.Handler, req: *httpz.Request, res: *httpz.Response) !void { + var body = try req.json(model.RequestBody) orelse { + res.setStatus(.bad_request); + return; + }; + + var body_model = try body.ToModel(req.arena); + + db.Users.Create(&body_model) catch |err| { + if (err == db.ResultErrors.AlreadyExists) { + res.setStatus(.bad_request); + try res.json(errDesc.AlreadyExistsDescriptor, .{}); + return; + } + res.setStatus(.internal_server_error); + return; + }; + res.setStatus(.created); +} |
