pollo/app/routes/api.room.delete.$roomId.tsx

48 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2023-04-20 04:20:00 -06:00
import { getAuth } from "@clerk/remix/ssr.server";
2023-12-13 14:55:49 -07:00
import { type ActionFunctionArgs, json } from "@remix-run/node";
2023-04-20 04:20:00 -06:00
import { eq } from "drizzle-orm";
import { db } from "~/services/db.server";
import { emitter } from "~/services/emitter.server";
2023-12-11 22:58:49 -07:00
import { rooms } from "~/services/schema.server";
2023-04-20 04:20:00 -06:00
export async function action({ request, params, context }: ActionFunctionArgs) {
const { userId } = await getAuth({ context, params, request });
if (!userId) {
return json("Not Signed In!", {
status: 403,
statusText: "UNAUTHORIZED!",
});
}
const roomId = params.roomId;
if (!roomId) {
return json("RoomId Missing!", {
status: 400,
statusText: "BAD REQUEST!",
});
}
const deletedRoom = await db
.delete(rooms)
.where(eq(rooms.id, roomId))
.returning();
const success = deletedRoom.length > 0;
if (success) {
2023-12-01 14:57:31 -07:00
emitter.emit("nodes", "roomlist");
2023-04-20 04:20:00 -06:00
return json(deletedRoom, {
status: 200,
statusText: "SUCCESS",
});
}
2023-12-01 16:15:21 -07:00
return json(null, {
status: 404,
statusText: "NOT FOUND",
});
2023-04-20 04:20:00 -06:00
}