summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--README.md5
-rw-r--r--backend/src/API/WeaponsAPI.zig2
-rw-r--r--backend/src/Models/User.zig47
-rw-r--r--backend/src/main.zig1
-rw-r--r--db/create_script.sql7
6 files changed, 61 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index a20b79e..3161178 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
zig-out
.zig-cache
+zig-pkg
.env
diff --git a/README.md b/README.md
index 9829b4b..3e0a474 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,4 @@
-# CYBERPUNK 2020 KNOWLEDGE
+# CYBERPUNK 2020 KNOWLEDGE SITE
+
+## Backend
+! WARNING! Currently does not support zig 0.16 due to outdated packages.
diff --git a/backend/src/API/WeaponsAPI.zig b/backend/src/API/WeaponsAPI.zig
index c15738e..75cd6e8 100644
--- a/backend/src/API/WeaponsAPI.zig
+++ b/backend/src/API/WeaponsAPI.zig
@@ -48,7 +48,7 @@ fn newRangedWeapon(_: *Handler.Handler, req: *httpz.Request, res: *httpz.Respons
var weapon = body.ToModel();
db.RangedWeapons.Create(&weapon) catch |err| {
if (err == db.ResultErrors.AlreadyExists) {
- res.status = 400;
+ res.setStatus(.bad_request);
try res.json(errDesc.ErrorDescriptor {
.Message = "A weapon with that ID already exists"
}, .{});
diff --git a/backend/src/Models/User.zig b/backend/src/Models/User.zig
new file mode 100644
index 0000000..1b7943a
--- /dev/null
+++ b/backend/src/Models/User.zig
@@ -0,0 +1,47 @@
+const std = @import("std");
+const pg = @import("pg");
+
+const RoleError = error {
+ NotSupported
+};
+
+const RolesMap = std.static_string_map.StaticStringMap(Role).initComptime(.{
+ .{ "user", .user },
+ .{ "editor", .editor },
+});
+
+pub const Role = enum {
+ user,
+ editor,
+
+ pub fn ToString(self: Role) []const u8 {
+ return switch (self) {
+ .user => "user",
+ .editor => "editor"
+ };
+ }
+
+ pub fn FromString(string: []const u8) !Role {
+ return RolesMap.get(string) orelse RoleError.NotSupported;
+ }
+};
+
+pub const User = struct {
+ // UUID
+ Id: [36]u8,
+ Username: []const u8,
+ PasswordHash: []const u8,
+ Role: []const u8
+
+ // pub fn Map(row: *const pg.Row) !User {
+ // }
+};
+
+test "Roles transformation" {
+ const roleString = "user";
+ const roleVar: Role = .editor;
+
+ try std.testing.expectEqualStrings("editor", Role.ToString(roleVar));
+ const actual = try Role.FromString(roleString);
+ try std.testing.expectEqual(Role.user, actual);
+}
diff --git a/backend/src/main.zig b/backend/src/main.zig
index a949b4d..e7641cc 100644
--- a/backend/src/main.zig
+++ b/backend/src/main.zig
@@ -42,4 +42,5 @@ fn index(_: *handler.Handler, _: *httpz.Request, res: *httpz.Response) !void {
test "TestRunner" {
_ = @import("Models/RangedWeapon.zig");
+ _ = @import("Models/User.zig");
}
diff --git a/db/create_script.sql b/db/create_script.sql
index 5b016e6..150e9c4 100644
--- a/db/create_script.sql
+++ b/db/create_script.sql
@@ -14,3 +14,10 @@ CREATE TABLE RangedWeapons (
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
+CREATE TABLE Users (
+ Id UUID PRIMARY KEY,
+ Username TEXT NOT NULL UNIQUE,
+ /* SHA512 encryption */
+ PasswordHash VARCHAR(512) NOT NULL UNIQUE,
+ Role TEXT NOT NULL
+);