Add version files and update imports for trip model; enhance error handling
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { fail, handleOptions, json } from "../_shared/http.ts";
|
||||
import { requireUser } from "../_shared/supabase.ts";
|
||||
|
||||
Deno.serve(async (req) => {
|
||||
const preflight = handleOptions(req);
|
||||
if (preflight) return preflight;
|
||||
|
||||
if (req.method !== "POST") return fail("Method not allowed", 405);
|
||||
|
||||
const { client, user, error: userError } = await requireUser(req);
|
||||
if (!user) return fail(userError ?? "Unauthorized", 401);
|
||||
|
||||
let body: { channel_id?: string; content?: string };
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return fail("Invalid JSON body");
|
||||
}
|
||||
|
||||
const channelId = body.channel_id;
|
||||
const content = (body.content ?? "").trim();
|
||||
|
||||
if (!channelId) return fail("channel_id is required");
|
||||
if (!content) return fail("content is required");
|
||||
if (content.length > 4000) return fail("content is too long");
|
||||
|
||||
const { data: message, error } = await client
|
||||
.from("messages")
|
||||
.insert({
|
||||
channel_id: channelId,
|
||||
author_user_id: user.id,
|
||||
content,
|
||||
})
|
||||
.select("id, channel_id, author_user_id, content, created_at, edited_at, deleted_at")
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
if (error.code === "42501") return fail("forbidden", 403);
|
||||
return fail(error.message, 400);
|
||||
}
|
||||
|
||||
return json({ message }, 201);
|
||||
});
|
||||
Reference in New Issue
Block a user