güncelleme

This commit is contained in:
2026-01-29 16:49:51 +03:00
parent 10bfa3089e
commit 54113726f4
38 changed files with 1469 additions and 642 deletions

View File

@@ -9,26 +9,41 @@ interface ProductData {
description?: string
price: number
image_url?: string
is_active?: boolean
images?: 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({
// 1. Create Product
const { data: product, error } = await supabase.from("products").insert({
name: data.name,
category: data.category,
description: data.description,
price: data.price,
image_url: data.image_url,
})
image_url: data.image_url, // Main image (can be first of images)
is_active: data.is_active ?? true
}).select().single()
if (error) throw error
// 2. Insert Images (if any)
if (data.images && data.images.length > 0) {
const imageInserts = data.images.map((url, index) => ({
product_id: product.id,
image_url: url,
display_order: index
}))
const { error: imgError } = await supabase.from("product_images").insert(imageInserts)
if (imgError) {
console.error("Error inserting images:", imgError)
// We don't throw here to avoid failing the whole product creation if just images fail
}
}
revalidatePath("/dashboard/products")
return { success: true }
} catch (error) {
@@ -40,16 +55,38 @@ export async function updateProduct(id: number, data: ProductData) {
const supabase = createClient()
try {
// 1. Update Product
const { error } = await supabase.from("products").update({
name: data.name,
category: data.category,
description: data.description,
price: data.price,
image_url: data.image_url,
is_active: data.is_active
}).eq("id", id)
if (error) throw error
// 2. Update Images
// Strategy: Delete all and re-insert is simplest for now.
// Or better: Differential update. For simplicity in MVP: Delete all for this product and re-insert *if* new images provided.
// Actually, if we want to keep existing ones, we need more complex logic.
// For now, let's assume the form sends the FULL list of current images.
if (data.images) {
// Delete old
await supabase.from("product_images").delete().eq("product_id", id)
// Insert new
if (data.images.length > 0) {
const imageInserts = data.images.map((url, index) => ({
product_id: id,
image_url: url,
display_order: index
}))
await supabase.from("product_images").insert(imageInserts)
}
}
revalidatePath("/dashboard/products")
revalidatePath(`/dashboard/products/${id}`)
return { success: true }