41 lines
1.3 KiB
Zig
41 lines
1.3 KiB
Zig
const root = @import("root");
|
|
const http = @import("http");
|
|
const Uuid = @import("util").Uuid;
|
|
|
|
const utils = @import("../controllers.zig").utils;
|
|
const NoteCreateInfo = @import("api").NoteCreateInfo;
|
|
|
|
const RequestServer = root.RequestServer;
|
|
const RouteArgs = http.RouteArgs;
|
|
|
|
pub const create = struct {
|
|
pub const method = .POST;
|
|
pub const path = "/notes";
|
|
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, _: RouteArgs) !void {
|
|
const info = try utils.parseRequestBody(struct { content: []const u8 }, ctx);
|
|
defer utils.freeRequestBody(info, ctx.alloc);
|
|
|
|
var api = try utils.getApiConn(srv, ctx);
|
|
defer api.close();
|
|
|
|
const note = try api.createNote(info.content);
|
|
|
|
try utils.respondJson(ctx, .created, note);
|
|
}
|
|
};
|
|
|
|
pub const get = struct {
|
|
pub const method = .GET;
|
|
pub const path = "/notes/:id";
|
|
pub fn handler(srv: *RequestServer, ctx: *http.server.Context, args: RouteArgs) !void {
|
|
const id_str = args.get("id") orelse return error.NotFound;
|
|
const id = Uuid.parse(id_str) catch return utils.respondError(ctx, .bad_request, "Invalid UUID");
|
|
var api = try utils.getApiConn(srv, ctx);
|
|
defer api.close();
|
|
|
|
const note = try api.getNote(id);
|
|
|
|
try utils.respondJson(ctx, .ok, note);
|
|
}
|
|
};
|