Collect author id from user context

This commit is contained in:
jaina heartles 2022-07-17 23:11:42 -07:00
parent 87903ca17b
commit 11d4a52b2d
4 changed files with 44 additions and 13 deletions

View file

@ -53,6 +53,10 @@ pub const UserContext = struct {
user: models.User, user: models.User,
}; };
pub const NoteCreate = struct {
content: []const u8,
};
pub const ApiServer = struct { pub const ApiServer = struct {
prng: std.rand.DefaultPrng, prng: std.rand.DefaultPrng,
db: db.Database, 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 { pub fn createNote(self: *ApiServer, info: CreateInfo(models.Note)) !models.Note {
const id = Uuid.randV4(self.prng.random()); const id = Uuid.randV4(self.prng.random());
// TODO: check for dupes // TODO: check for dupes

View file

@ -47,16 +47,27 @@ const utils = struct {
fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void { fn freeRequestBody(value: anytype, alloc: std.mem.Allocator) void {
std.json.parseFree(@TypeOf(value), value, .{ .allocator = alloc }); 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 RequestServer = root.RequestServer;
const RouteArgs = http.RouteArgs; const RouteArgs = http.RouteArgs;
pub fn createNote(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void { 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); 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); 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 { pub fn authenticate(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
const header = ctx.request.headers.get("authorization") orelse "(null)"; const user_ctx = try utils.getUserContext(srv, ctx);
// TODO: defer api.free(srv.alloc, user_ctx);
const token = header[("bearer ").len..];
const user_ctx = try srv.api.makeUserContext(token, srv.alloc);
try utils.respondJson(ctx, .ok, user_ctx, srv.alloc); try utils.respondJson(ctx, .ok, user_ctx, srv.alloc);
} }

View file

@ -93,17 +93,20 @@ pub const Database = struct {
db: sql.Sqlite, db: sql.Sqlite,
const init_sql_stmts = [_][]const u8{ 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 \\CREATE TABLE IF NOT EXISTS
\\user( \\user(
\\ id TEXT PRIMARY KEY, \\ id TEXT PRIMARY KEY,
\\ handle TEXT NOT NULL \\ handle TEXT NOT NULL
\\) STRICT; \\) 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 { pub fn init() !Database {

View file

@ -3,6 +3,7 @@ const Uuid = @import("util").Uuid;
pub const Note = struct { pub const Note = struct {
id: Uuid, id: Uuid,
content: []const u8, content: []const u8,
author_id: Uuid,
}; };
pub const User = struct { pub const User = struct {