From 2092bbdffa7e9ee9fb24096842a8e227f4b54fbe Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Tue, 12 Jul 2022 22:35:39 -0700 Subject: [PATCH] Check users for duplicates --- src/main/api.zig | 14 +++++++++++++- src/main/db.zig | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/api.zig b/src/main/api.zig index 197ed0f..62e7b83 100644 --- a/src/main/api.zig +++ b/src/main/api.zig @@ -64,8 +64,20 @@ pub const ApiServer = struct { } pub fn createUser(self: *ApiServer, info: CreateInfo(models.User)) !models.User { + try self.db.users.lock(); + defer self.db.users.unlock(); + // TODO; real queries + const id = Uuid.randV4(self.prng.random()); - // TODO: check for dupes + + // check for handle dupes + var iter = self.db.users.data.iterator(); + while (iter.next()) |it| { + if (std.mem.eql(u8, it.value_ptr.handle, info.handle)) { + return error.DuplicateHandle; + } + } + // TODO: check for id dupes const user = reify(models.User, id, info); try self.db.users.put(user); diff --git a/src/main/db.zig b/src/main/db.zig index 1ce96e1..432df2c 100644 --- a/src/main/db.zig +++ b/src/main/db.zig @@ -93,6 +93,15 @@ pub fn Table(comptime T: type) type { } try self.data.put(key, copy); } + + // TODO + pub fn lock(_: *Self) !void { + return; + } + + pub fn unlock(_: *Self) void { + return; + } }; }