Files
parakasa/app/(dashboard)/dashboard/products/actions.ts

60 lines
1.6 KiB
TypeScript

"use server"
import { createClient } from "@/lib/supabase-server"
import { revalidatePath } from "next/cache"
interface ProductData {
name: string
category: string
description?: string
price: number
image_url?: string
}
export async function createProduct(data: ProductData) {
const supabase = createClient()
// Validate data manually or use Zod schema here again securely
// For simplicity, we assume data is coming from the strongly typed Client Form
// In production, ALWAYS validate server-side strictly.
try {
const { error } = await supabase.from("products").insert({
name: data.name,
category: data.category,
description: data.description,
price: data.price,
image_url: data.image_url,
})
if (error) throw error
revalidatePath("/dashboard/products")
return { success: true }
} catch (error) {
return { success: false, error: (error as Error).message }
}
}
export async function updateProduct(id: number, data: ProductData) {
const supabase = createClient()
try {
const { error } = await supabase.from("products").update({
name: data.name,
category: data.category,
description: data.description,
price: data.price,
image_url: data.image_url,
}).eq("id", id)
if (error) throw error
revalidatePath("/dashboard/products")
revalidatePath(`/dashboard/products/${id}`)
return { success: true }
} catch (error) {
return { success: false, error: (error as Error).message }
}
}