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,
};
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

View file

@ -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);
}

View file

@ -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 {

View file

@ -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 {