From 11d4a52b2d26b44b17bc8e69bf4fd17fb366133a Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Sun, 17 Jul 2022 23:11:42 -0700 Subject: [PATCH] Collect author id from user context --- src/main/api.zig | 19 +++++++++++++++++++ src/main/controllers.zig | 22 +++++++++++++++------- src/main/db.zig | 15 +++++++++------ src/main/models.zig | 1 + 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/main/api.zig b/src/main/api.zig index 7bc8ba2..d1da30c 100644 --- a/src/main/api.zig +++ b/src/main/api.zig @@ -53,6 +53,10 @@ pub const UserContext = struct { user: models.User, }; +pub const NoteCreate = struct { + content: []const u8, +}; + pub const ApiServer = struct { prng: std.rand.DefaultPrng, db: db.Database, @@ -76,6 +80,21 @@ pub const ApiServer = struct { }; } + pub fn createNoteUser(self: *ApiServer, info: NoteCreate, ctx: UserContext) !models.Note { + const id = Uuid.randV4(self.prng.random()); + // TODO: check for dupes + std.debug.print("user {s} making a note\n", .{ctx.user.handle}); + + const note = models.Note{ + .id = id, + .author_id = ctx.user.id, + .content = info.content, + }; + try self.db.insert(models.Note, note); + + return note; + } + pub fn createNote(self: *ApiServer, info: CreateInfo(models.Note)) !models.Note { const id = Uuid.randV4(self.prng.random()); // TODO: check for dupes diff --git a/src/main/controllers.zig b/src/main/controllers.zig index b7cd067..7465af4 100644 --- a/src/main/controllers.zig +++ b/src/main/controllers.zig @@ -47,16 +47,27 @@ const utils = struct { fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void { std.json.parseFree(@TypeOf(value), value, .{ .allocator = alloc }); } + + fn getUserContext(srv: *RequestServer, ctx: *http.server.Context) !api.UserContext { + const header = ctx.request.headers.get("authorization") orelse "(null)"; + + const token = header[("bearer ").len..]; + + return try srv.api.makeUserContext(token, srv.alloc); + // TODO: defer api.free(srv.alloc, user_ctx); + } }; const RequestServer = root.RequestServer; const RouteArgs = http.RouteArgs; pub fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { - const info = try utils.parseRequestBody(api.CreateInfo(models.Note), ctx, srv.alloc); + const user_context = try utils.getUserContext(srv, ctx); + // TODO: defer free usercontext + const info = try utils.parseRequestBody(api.NoteCreate, ctx, srv.alloc); defer utils.freeRequestBody(info, srv.alloc); - const note = try srv.api.createNote(info); + const note = try srv.api.createNoteUser(info, user_context); try utils.respondJson(ctx, .created, note, srv.alloc); } @@ -92,11 +103,8 @@ pub fn getUser(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) } pub fn authenticate(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { - const header = ctx.request.headers.get("authorization") orelse "(null)"; - - const token = header[("bearer ").len..]; - - const user_ctx = try srv.api.makeUserContext(token, srv.alloc); + const user_ctx = try utils.getUserContext(srv, ctx); + // TODO: defer api.free(srv.alloc, user_ctx); try utils.respondJson(ctx, .ok, user_ctx, srv.alloc); } diff --git a/src/main/db.zig b/src/main/db.zig index c73e51f..9b1c4f7 100644 --- a/src/main/db.zig +++ b/src/main/db.zig @@ -93,17 +93,20 @@ pub const Database = struct { db: sql.Sqlite, const init_sql_stmts = [_][]const u8{ - \\CREATE TABLE IF NOT EXISTS - \\note( - \\ id TEXT PRIMARY KEY, - \\ content TEXT NOT NULL - \\) STRICT; - , \\CREATE TABLE IF NOT EXISTS \\user( \\ id TEXT PRIMARY KEY, \\ handle TEXT NOT NULL \\) STRICT; + , + \\CREATE TABLE IF NOT EXISTS + \\note( + \\ id TEXT PRIMARY KEY, + \\ content TEXT NOT NULL, + \\ author_id TEXT NOT NULL, + \\ + \\ FOREIGN KEY (author_id) REFERENCES user(id) + \\) STRICT; }; pub fn init() !Database { diff --git a/src/main/models.zig b/src/main/models.zig index 589ca35..f7e1b63 100644 --- a/src/main/models.zig +++ b/src/main/models.zig @@ -3,6 +3,7 @@ const Uuid = @import("util").Uuid; pub const Note = struct { id: Uuid, content: []const u8, + author_id: Uuid, }; pub const User = struct {