|
1 | | -import { eq } from 'drizzle-orm'; |
2 | 1 | import { Metadata } from 'next'; |
3 | 2 | import { notFound } from 'next/navigation'; |
4 | 3 | import { z } from 'zod'; |
5 | 4 |
|
6 | | -import { db } from '@/db'; |
7 | | -import { productPrices, products } from '@/db/schema'; |
| 5 | +import { ProductNotFoundError } from '@/lib/errors/products'; |
8 | 6 | import { issueCsrfToken } from '@/lib/security/csrf'; |
| 7 | +import { getAdminProductByIdWithPrices } from '@/lib/services/products'; |
9 | 8 | import type { CurrencyCode } from '@/lib/shop/currency'; |
10 | 9 | import { currencyValues } from '@/lib/shop/currency'; |
11 | 10 |
|
@@ -37,22 +36,18 @@ export default async function EditProductPage({ |
37 | 36 | const parsed = paramsSchema.safeParse(rawParams); |
38 | 37 | if (!parsed.success) notFound(); |
39 | 38 |
|
40 | | - const [product] = await db |
41 | | - .select() |
42 | | - .from(products) |
43 | | - .where(eq(products.id, parsed.data.id)) |
44 | | - .limit(1); |
| 39 | + let product; |
| 40 | + try { |
| 41 | + product = await getAdminProductByIdWithPrices(parsed.data.id); |
| 42 | + } catch (error) { |
| 43 | + if (error instanceof ProductNotFoundError) { |
| 44 | + notFound(); |
| 45 | + } |
45 | 46 |
|
46 | | - if (!product) notFound(); |
| 47 | + throw error; |
| 48 | + } |
47 | 49 |
|
48 | | - const prices = await db |
49 | | - .select({ |
50 | | - currency: productPrices.currency, |
51 | | - price: productPrices.price, |
52 | | - originalPrice: productPrices.originalPrice, |
53 | | - }) |
54 | | - .from(productPrices) |
55 | | - .where(eq(productPrices.productId, product.id)); |
| 50 | + const prices = product.prices; |
56 | 51 |
|
57 | 52 | const initialPrices = prices.length |
58 | 53 | ? prices |
@@ -98,6 +93,7 @@ export default async function EditProductPage({ |
98 | 93 | stock: product.stock, |
99 | 94 | sku: product.sku ?? undefined, |
100 | 95 | imageUrl: product.imageUrl, |
| 96 | + images: product.images, |
101 | 97 | }} |
102 | 98 | /> |
103 | 99 | </main> |
|
0 commit comments