Create note endpoint

This commit is contained in:
jaina heartles 2022-05-12 18:49:26 -07:00
parent 5bd4df8740
commit c484af6179
2 changed files with 44 additions and 0 deletions

View file

@ -1,3 +1,4 @@
const std = @import("std");
const root = @import("root");
const Uuid = root.util.Uuid;
@ -9,6 +10,36 @@ pub const Actor = struct {
bio: ?[]const u8 = null,
};
pub const Note = struct {
id: Uuid,
author: Uuid,
content: []const u8 = "",
};
pub fn CreateInfo(comptime T: type) type {
if (@typeInfo(T) != .Struct) @compileError("type is not struct");
if (!std.meta.trait.hasField("id")(T)) @compileError("type does not have id field");
if (@typeInfo(T).Struct.is_tuple) @compileError("type cannot be tuple");
var fields: [std.meta.fields(T).len - 1]std.builtin.TypeInfo.StructField = undefined;
var i = 0;
for (std.meta.fields(T)) |field| {
if (std.mem.eql(u8, field.name, "id")) continue;
fields[i] = field;
i += 1;
}
return @Type(.{
.Struct = .{
.layout = @typeInfo(T).Struct.layout,
.fields = &fields,
.is_tuple = false,
.decls = &[_]std.builtin.TypeInfo.Declaration{},
},
});
}
const this_host = "localhost:8080";
const actor = Actor{
@ -22,3 +53,7 @@ pub fn getActorById(id: Uuid) !?Actor {
return null;
}
pub fn createNote(_: CreateInfo(Note)) !Uuid {
return try Uuid.parse("f75f5160-12d3-42c2-a81d-ad2245b7a74b");
}

View file

@ -15,11 +15,20 @@ pub const routes = [_]Route{
Route.from(.GET, "/", staticString("Index Page")),
Route.from(.GET, "/abc", staticString("abc")),
Route.from(.GET, "/user/:id", getUser),
Route.from(.POST, "/note/", postNote),
};
const this_scheme = "http";
const this_host = "localhost:8080";
fn postNote(ctx: *http.Context, _: *const Route) anyerror!void {
_ = try db.createNote(.{
.author = try Uuid.parse("f75f5160-12d3-42c2-a81d-ad2245b7a74b"),
.content = "test post",
});
try ctx.response.statusOnly(200);
}
fn getUser(ctx: *http.Context, route: *const Route) anyerror!void {
const id_str = route.arg("id", ctx.request.path);