Skip to content

Commit

Permalink
Merge pull request sadmann7#100 from JDLanctot/product_server_actions
Browse files Browse the repository at this point in the history
checkProduct in Fetchers/Product.ts exposed DB on New Product Page
  • Loading branch information
sadmann7 authored Dec 5, 2023
2 parents acaf56f + f7da135 commit 1e578ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/components/forms/add-product-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { type z } from "zod"

import { getSubcategories } from "@/config/products"
import { addProduct } from "@/lib/actions/product"
import { checkProduct } from "@/lib/fetchers/product"
import { checkProduct } from "@/lib/actions/product"
import { catchError, isArrayOfFile } from "@/lib/utils"
import { productSchema } from "@/lib/validations/product"
import { Button } from "@/components/ui/button"
Expand Down
25 changes: 23 additions & 2 deletions src/lib/actions/product.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use server"

import { revalidatePath } from "next/cache"
import { unstable_noStore as noStore, revalidatePath } from "next/cache"
import { db } from "@/db"
import { products, type Product } from "@/db/schema"
import { faker } from "@faker-js/faker"
import { and, desc, eq, like } from "drizzle-orm"
import { and, desc, eq, like, not } from "drizzle-orm"
import { z } from "zod"

import { getSubcategories, productTags } from "@/config/products"
Expand Down Expand Up @@ -93,6 +93,27 @@ export async function filterProducts(query: string) {
return productsByCategory
}

export async function checkProduct(input: { name: string; id?: number }) {
noStore()
try {
const productWithSameName = await db.query.products.findFirst({
columns: {
id: true,
},
where: input.id
? and(not(eq(products.id, input.id)), eq(products.name, input.name))
: eq(products.name, input.name),
})

if (productWithSameName) {
throw new Error("Product name already taken.")
}
} catch (err) {
console.error(err)
return null
}
}

export async function addProduct(
rawInput: z.infer<typeof extendedProductSchema>
) {
Expand Down
22 changes: 0 additions & 22 deletions src/lib/fetchers/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
inArray,
lt,
lte,
not,
sql,
} from "drizzle-orm"
import { stores } from "drizzle/schema"
Expand Down Expand Up @@ -122,27 +121,6 @@ export async function getProducts(rawInput: z.infer<typeof getProductsSchema>) {
}
}

export async function checkProduct(input: { name: string; id?: number }) {
noStore()
try {
const productWithSameName = await db.query.products.findFirst({
columns: {
id: true,
},
where: input.id
? and(not(eq(products.id, input.id)), eq(products.name, input.name))
: eq(products.name, input.name),
})

if (productWithSameName) {
throw new Error("Product name already taken.")
}
} catch (err) {
console.error(err)
return null
}
}

export async function getNextProductId(
rawInput: z.infer<typeof getProductSchema>
) {
Expand Down

0 comments on commit 1e578ff

Please sign in to comment.