feat: walking skeleton - study space uploads
This commit is contained in:
36
src/routes/api/study_spaces/+server.ts
Normal file
36
src/routes/api/study_spaces/+server.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { PUBLIC_SUPABASE_ANON_KEY, PUBLIC_SUPABASE_URL } from "$env/static/public";
|
||||
import type { Database } from "$lib/database";
|
||||
import { createClient } from "@supabase/supabase-js";
|
||||
import { error, type RequestHandler } from "@sveltejs/kit";
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const supabase = createClient<Database>(PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY);
|
||||
const body = await request.json();
|
||||
const title = body.title;
|
||||
const imgB64 = body.img;
|
||||
const imgTitle = body.imgTitle;
|
||||
if (!title || !imgB64 || !imgTitle) error(400, "Missing required fields: title, img, imgTitle");
|
||||
|
||||
const img = await fetch(imgB64).then((res) => res.blob());
|
||||
const { data: imageData, error: imageError } = await supabase.storage
|
||||
.from("files_bucket")
|
||||
.upload(`public/${imgTitle}`, img, {
|
||||
contentType: img.type,
|
||||
upsert: false
|
||||
});
|
||||
if (imageError) error(500, `Failed to upload image: ${imageError.message}`);
|
||||
|
||||
const { data: studySpaceData, error: studySpaceError } = await supabase
|
||||
.from("study_spaces")
|
||||
.insert({ title })
|
||||
.select()
|
||||
.single();
|
||||
if (studySpaceError) error(500, "Failed to create study space");
|
||||
|
||||
const { error: imageLinkError } = await supabase
|
||||
.from("study_space_images")
|
||||
.insert({ study_space_id: studySpaceData.id, image_path: imageData.path });
|
||||
|
||||
if (imageLinkError) error(500, "Failed to link image to study space");
|
||||
return new Response(JSON.stringify({ id: studySpaceData.id }), { status: 200 });
|
||||
};
|
||||
Reference in New Issue
Block a user