From 39fdf4c88d026604451e5f29f25495fff09a627a Mon Sep 17 00:00:00 2001 From: Leon Xu Date: Sat, 30 Dec 2023 14:12:12 -0800 Subject: [PATCH] feat: created getProductById feature in actions library --- lib/actions/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/actions/index.ts b/lib/actions/index.ts index 21390d1..56a422d 100644 --- a/lib/actions/index.ts +++ b/lib/actions/index.ts @@ -45,4 +45,18 @@ export async function scrapeAndStoreProduct(productUrl: string) { } catch (error: any) { throw new Error(`Failed to create/update product; ${error.message}`) } +} + +export async function getProductById(productId: string) { + try { + connectToDB(); + + const product = await Product.findOne({ _id: productId }); + + if (!product) return null; + + return product + } catch (error) { + console.log(error); + } } \ No newline at end of file