first commit
This commit is contained in:
39
app/items/actions.ts
Normal file
39
app/items/actions.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
"use server";
|
||||
|
||||
import { db } from "@/db";
|
||||
import { items } from "@/db/schema";
|
||||
import { eq, desc } from "drizzle-orm";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
export async function getItems() {
|
||||
return await db.query.items.findMany({
|
||||
orderBy: [desc(items.createdAt)],
|
||||
});
|
||||
}
|
||||
|
||||
export async function createItem(formData: FormData) {
|
||||
const key = formData.get("key") as string;
|
||||
const title = formData.get("title") as string;
|
||||
const content = formData.get("content") as string;
|
||||
const type = formData.get("type") as string;
|
||||
|
||||
await db.insert(items).values({
|
||||
key,
|
||||
title,
|
||||
content,
|
||||
type,
|
||||
});
|
||||
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function deleteItem(id: number) {
|
||||
await db.delete(items).where(eq(items.id, id));
|
||||
revalidatePath("/");
|
||||
}
|
||||
|
||||
export async function getItemByKey(key: string) {
|
||||
return await db.query.items.findFirst({
|
||||
where: eq(items.key, key),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user