freshter-logs/routes/api/conv/index.ts

35 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-04-28 19:48:34 -04:00
import { HandlerContext } from "$fresh/server.ts";
import { catcher_async, json_response } from "utils";
import { new_conversation } from "db";
export async function handler(
req: Request,
_ctx: HandlerContext
): Promise<Response> {
if (req.method == "PUT") {
return await put(req);
} else {
return new Response(JSON.stringify({ err: "expected a PUT" }));
}
}
async function put(req: Request): Promise<Response> {
const json = await catcher_async(() => req.json());
if (json.isNone()) {
return json_response({ err: "expected JSON :(" }, { status: 400 });
}
const json_ = json.unwrap();
if (!json_.name || !json_.description) {
return json_response(
{ err: "i need a `name` and a `description`" },
{ status: 400 }
);
}
const new_id = await new_conversation(json_.name, json_.description);
return json_response(new_id.unwrapOr("fail"));
}
//TODO: validate that they give a valid message object
//TODO: put under some id
//TODO: e.g. {under: string, message: Message}