Files
app-admin/app/apps/actions.ts
2026-03-24 15:46:27 +03:00

73 lines
1.9 KiB
TypeScript

"use server";
import { db } from "@/db";
import { apps } from "@/db/schema";
import { eq, desc } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
export async function getApps() {
return await db.query.apps.findMany({
orderBy: [desc(apps.createdAt)],
});
}
export async function getAppById(id: number) {
if (isNaN(id)) return null;
return await db.query.apps.findFirst({
where: eq(apps.id, id),
});
}
export async function createApp(formData: FormData) {
const name = (formData.get("name") as string)?.trim();
const bundleId = (formData.get("bundleId") as string)?.trim();
const platform = formData.get("platform") as string;
const appleId = (formData.get("appleId") as string)?.trim() || null;
if (!name || !bundleId) throw new Error("İsim ve Bundle ID zorunludur.");
await db.insert(apps).values({
name,
bundleId,
platform: platform as any,
appleId: appleId,
});
revalidatePath("/apps");
revalidatePath("/");
redirect("/apps");
}
export async function updateApp(id: number, formData: FormData) {
if (isNaN(id)) throw new Error("Geçersiz Uygulama ID");
const name = (formData.get("name") as string)?.trim();
const bundleId = (formData.get("bundleId") as string)?.trim();
const platform = formData.get("platform") as string;
const appleId = (formData.get("appleId") as string)?.trim() || null;
const status = formData.get("status") as string;
if (!name || !bundleId) throw new Error("İsim ve Bundle ID zorunludur.");
await db.update(apps).set({
name,
bundleId,
platform: platform as any,
appleId: appleId,
status: status as any,
updatedAt: new Date(),
}).where(eq(apps.id, id));
revalidatePath("/apps");
revalidatePath("/");
redirect("/apps");
}
export async function deleteApp(id: number) {
if (isNaN(id)) return;
await db.delete(apps).where(eq(apps.id, id));
revalidatePath("/apps");
revalidatePath("/");
}