mirror of
https://gitlab.massbug.com/massbug/judge4c.git
synced 2025-07-04 12:12:12 +00:00
feat(workspaces): refactor workspace routes to separate GET and POST handlers
This commit is contained in:
parent
377b37fe03
commit
af74979e09
@ -5,49 +5,60 @@ import { createWorkspaceSchema } from "../schemas";
|
|||||||
import { sessionMiddleware } from "@/lib/session-middleware";
|
import { sessionMiddleware } from "@/lib/session-middleware";
|
||||||
import { DATABASE_ID, IMAGES_BUCKET_ID, WORKSPACES_ID } from "@/config";
|
import { DATABASE_ID, IMAGES_BUCKET_ID, WORKSPACES_ID } from "@/config";
|
||||||
|
|
||||||
const app = new Hono().post(
|
const app = new Hono()
|
||||||
"/",
|
.get("/", sessionMiddleware, async (c) => {
|
||||||
zValidator("form", createWorkspaceSchema),
|
|
||||||
sessionMiddleware,
|
|
||||||
async (c) => {
|
|
||||||
const databases = c.get("databases");
|
const databases = c.get("databases");
|
||||||
const storage = c.get("storage");
|
|
||||||
const user = c.get("user");
|
|
||||||
|
|
||||||
const { name, image } = c.req.valid("form");
|
const workspaces = await databases.listDocuments(
|
||||||
|
|
||||||
let uploadImageUrl: string | undefined;
|
|
||||||
|
|
||||||
if (image instanceof File) {
|
|
||||||
const file = await storage.createFile(
|
|
||||||
IMAGES_BUCKET_ID,
|
|
||||||
ID.unique(),
|
|
||||||
image
|
|
||||||
);
|
|
||||||
|
|
||||||
const arrayBuffer = await storage.getFilePreview(
|
|
||||||
IMAGES_BUCKET_ID,
|
|
||||||
file.$id
|
|
||||||
);
|
|
||||||
|
|
||||||
uploadImageUrl = `data:image/png;base64,${Buffer.from(
|
|
||||||
arrayBuffer
|
|
||||||
).toString("base64")}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const workspace = await databases.createDocument(
|
|
||||||
DATABASE_ID,
|
DATABASE_ID,
|
||||||
WORKSPACES_ID,
|
WORKSPACES_ID
|
||||||
ID.unique(),
|
|
||||||
{
|
|
||||||
name,
|
|
||||||
userId: user.$id,
|
|
||||||
imageUrl: uploadImageUrl,
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return c.json({ data: workspace });
|
return c.json({ data: workspaces });
|
||||||
}
|
})
|
||||||
);
|
.post(
|
||||||
|
"/",
|
||||||
|
zValidator("form", createWorkspaceSchema),
|
||||||
|
sessionMiddleware,
|
||||||
|
async (c) => {
|
||||||
|
const databases = c.get("databases");
|
||||||
|
const storage = c.get("storage");
|
||||||
|
const user = c.get("user");
|
||||||
|
|
||||||
|
const { name, image } = c.req.valid("form");
|
||||||
|
|
||||||
|
let uploadImageUrl: string | undefined;
|
||||||
|
|
||||||
|
if (image instanceof File) {
|
||||||
|
const file = await storage.createFile(
|
||||||
|
IMAGES_BUCKET_ID,
|
||||||
|
ID.unique(),
|
||||||
|
image
|
||||||
|
);
|
||||||
|
|
||||||
|
const arrayBuffer = await storage.getFilePreview(
|
||||||
|
IMAGES_BUCKET_ID,
|
||||||
|
file.$id
|
||||||
|
);
|
||||||
|
|
||||||
|
uploadImageUrl = `data:image/png;base64,${Buffer.from(
|
||||||
|
arrayBuffer
|
||||||
|
).toString("base64")}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workspace = await databases.createDocument(
|
||||||
|
DATABASE_ID,
|
||||||
|
WORKSPACES_ID,
|
||||||
|
ID.unique(),
|
||||||
|
{
|
||||||
|
name,
|
||||||
|
userId: user.$id,
|
||||||
|
imageUrl: uploadImageUrl,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return c.json({ data: workspace });
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
Loading…
Reference in New Issue
Block a user