mirror of
https://github.com/SrIzan10/helium.git
synced 2026-06-06 00:56:58 +00:00
feat: preliminary presets index
This commit is contained in:
34
server/api/presets/[id].delete.ts
Normal file
34
server/api/presets/[id].delete.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { db } from "~/lib/db";
|
||||
import { presets, presetUsers } from "~/lib/db/schema";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { isAuthenticated, userId } = event.context.auth();
|
||||
|
||||
if (!isAuthenticated || !userId) {
|
||||
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, "id");
|
||||
if (!id) {
|
||||
throw createError({ statusCode: 400, statusMessage: "Missing preset ID" });
|
||||
}
|
||||
|
||||
// Check if the user is the creator of the preset
|
||||
const preset = await db.query.presets.findFirst({
|
||||
where: eq(presets.id, id),
|
||||
});
|
||||
|
||||
if (!preset) {
|
||||
throw createError({ statusCode: 404, statusMessage: "Preset not found" });
|
||||
}
|
||||
|
||||
if (preset.createdBy !== userId) {
|
||||
throw createError({ statusCode: 403, statusMessage: "Forbidden: You can only delete your own presets" });
|
||||
}
|
||||
|
||||
// Delete the preset (cascades to presetUsers)
|
||||
await db.delete(presets).where(eq(presets.id, id));
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
39
server/api/presets/[id].get.ts
Normal file
39
server/api/presets/[id].get.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "~/lib/db";
|
||||
import { presets, presetUsers } from "~/lib/db/schema";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { isAuthenticated, userId } = event.context.auth();
|
||||
|
||||
if (!isAuthenticated || !userId) {
|
||||
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, "id");
|
||||
if (!id) {
|
||||
throw createError({ statusCode: 400, statusMessage: "Missing preset ID" });
|
||||
}
|
||||
|
||||
// Fetch the preset
|
||||
const preset = await db.query.presets.findFirst({
|
||||
where: eq(presets.id, id),
|
||||
});
|
||||
|
||||
if (!preset) {
|
||||
throw createError({ statusCode: 404, statusMessage: "Preset not found" });
|
||||
}
|
||||
|
||||
// Check if user has access (either creator or has it in their presetUsers)
|
||||
const userPreset = await db.query.presetUsers.findFirst({
|
||||
where: eq(presetUsers.presetId, id),
|
||||
});
|
||||
|
||||
if (preset.createdBy !== userId && (!userPreset || userPreset.userId !== userId)) {
|
||||
throw createError({ statusCode: 403, statusMessage: "Forbidden" });
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: preset,
|
||||
};
|
||||
});
|
||||
59
server/api/presets/[id].put.ts
Normal file
59
server/api/presets/[id].put.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { db } from "~/lib/db";
|
||||
import { presets, presetUsers } from "~/lib/db/schema";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const { isAuthenticated, userId } = event.context.auth();
|
||||
|
||||
if (!isAuthenticated || !userId) {
|
||||
throw createError({ statusCode: 401, statusMessage: "Unauthorized" });
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, "id");
|
||||
if (!id) {
|
||||
throw createError({ statusCode: 400, statusMessage: "Missing preset ID" });
|
||||
}
|
||||
|
||||
const body = await readBody(event);
|
||||
|
||||
// Verify ownership
|
||||
const preset = await db.query.presets.findFirst({
|
||||
where: eq(presets.id, id),
|
||||
});
|
||||
|
||||
if (!preset) {
|
||||
throw createError({ statusCode: 404, statusMessage: "Preset not found" });
|
||||
}
|
||||
|
||||
if (preset.createdBy !== userId) {
|
||||
throw createError({ statusCode: 403, statusMessage: "Forbidden: You can only edit your own presets" });
|
||||
}
|
||||
|
||||
// Update preset
|
||||
await db.update(presets)
|
||||
.set({
|
||||
name: body.name,
|
||||
iceServers: JSON.stringify(body.iceServers),
|
||||
})
|
||||
.where(eq(presets.id, id));
|
||||
|
||||
// Update default status in presetUsers
|
||||
if (body.default !== undefined) {
|
||||
// If setting as default, first unset all other defaults for this user
|
||||
if (body.default) {
|
||||
await db.update(presetUsers)
|
||||
.set({ isDefault: false })
|
||||
.where(eq(presetUsers.userId, userId));
|
||||
}
|
||||
|
||||
// Update the default status for this preset
|
||||
await db.update(presetUsers)
|
||||
.set({ isDefault: body.default })
|
||||
.where(and(
|
||||
eq(presetUsers.presetId, id),
|
||||
eq(presetUsers.userId, userId)
|
||||
));
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
Reference in New Issue
Block a user