From ffbf48bc8ac16377ae25e9f99040388ff023ff75 Mon Sep 17 00:00:00 2001 From: ChrQR Date: Sat, 25 May 2024 00:46:33 +0200 Subject: [PATCH] more query action --- package.json | 2 +- src/app/_components/WineCards.tsx | 5 ++--- src/app/_components/WineProducer.tsx | 27 +++++++++++++++++++----- src/app/page.tsx | 9 +++++--- src/server/actions/getProducer.ts | 13 ++++++------ src/server/actions/getWineDetails.ts | 13 +++++++----- src/server/db/schema.ts | 31 ++++++++++++++-------------- 7 files changed, 60 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index f090400..9621eea 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "build": "next build", "db:push": "drizzle-kit push", "db:studio": "drizzle-kit studio", - "dev": "next dev --turbo", + "dev": "next dev", "lint": "next lint", "start": "next start" }, diff --git a/src/app/_components/WineCards.tsx b/src/app/_components/WineCards.tsx index 08cdac2..b33720a 100644 --- a/src/app/_components/WineCards.tsx +++ b/src/app/_components/WineCards.tsx @@ -1,4 +1,4 @@ -import { getWines } from "../../server/actions/allWines"; +import { getWines } from "~/server/actions/allWines"; import WineName from "./WineName"; import WineProducer from "./WineProducer"; @@ -6,10 +6,9 @@ const allWines = await getWines(); export default async function WineCards() { return ( <> -

{JSON.stringify(allWines)}

{allWines.map((wine, i) => { return ( -
+
-
); diff --git a/src/app/_components/WineProducer.tsx b/src/app/_components/WineProducer.tsx index 18fc3ed..415d9c2 100644 --- a/src/app/_components/WineProducer.tsx +++ b/src/app/_components/WineProducer.tsx @@ -1,8 +1,25 @@ -import { UUID } from "crypto"; -import { PgUUID } from "drizzle-orm/pg-core"; import getProducer from "~/server/actions/getProducer"; -export default async function (props: { id: string }) { - const result = await getProducer(props.id); - return

{result?.name}

; +export default async function WineProducer(props: { id: string }) { + const { id } = props; + + // Validate the id before making the database call + if (!id) { + return

Invalid producer ID

; + } + + try { + const result = await getProducer(id); + return !result ?

Unable to retrieve producer

:

{result?.name}

; + } catch (error) { + console.error("Error fetching producer:", error); + return

Error fetching producer

; + } +} + +// Utility function to validate UUID +function isValidUUID(uuid: string) { + const uuidRegex = + /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; + return uuidRegex.test(uuid); } diff --git a/src/app/page.tsx b/src/app/page.tsx index 20a8359..9e52930 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,3 +1,4 @@ +import { Suspense } from "react"; import CreateWine from "./_components/CreateWine"; import WineCards from "./_components/WineCards"; @@ -5,11 +6,13 @@ export const dynamic = "force-dynamic"; export default async function HomePage() { return ( -
+

Yes hello

- - + Loading....

}> + + +
); diff --git a/src/server/actions/getProducer.ts b/src/server/actions/getProducer.ts index 021a889..9f65fd7 100644 --- a/src/server/actions/getProducer.ts +++ b/src/server/actions/getProducer.ts @@ -1,11 +1,10 @@ -"use server" +"use server"; import { producers } from "../db/schema"; import { db } from "../db"; import { eq } from "drizzle-orm"; -export default async function getProducer(id: string){ - return db.query.producers.findFirst({ - where: eq(producers.id, id) - }); - -} \ No newline at end of file +export default async function getProducer(id: string) { + return db.query.producers.findFirst({ + where: eq(producers.id, id), + }); +} diff --git a/src/server/actions/getWineDetails.ts b/src/server/actions/getWineDetails.ts index aa966fc..87bbac7 100644 --- a/src/server/actions/getWineDetails.ts +++ b/src/server/actions/getWineDetails.ts @@ -1,10 +1,13 @@ -"use server" +"use server"; import { sql } from "drizzle-orm"; import { db } from "../db"; import { wines, producers } from "../db/schema"; import { UUID } from "crypto"; -export async function getWineDetails(id: UUID) { - const results = await db.select().from(producers).where(sql `${producers.id} = ${id}`) - return results; - } \ No newline at end of file +export async function getWineDetails(id: string) { + const results = await db + .select() + .from(producers) + .where(sql`${producers.id} = ${id}`); + return results; +} diff --git a/src/server/db/schema.ts b/src/server/db/schema.ts index c3afa55..c6a4710 100644 --- a/src/server/db/schema.ts +++ b/src/server/db/schema.ts @@ -5,23 +5,23 @@ import { uniqueIndex, uuid, } from "drizzle-orm/pg-core"; -import { relations } from 'drizzle-orm'; +import { relations } from "drizzle-orm"; export const createTable = pgTableCreator((name) => `wine-shop_${name}`); export const producers = createTable( "producer", { - id: uuid('id').primaryKey().defaultRandom(), - name: text('name').notNull(), - createdAt: timestamp('createdAt').defaultNow().notNull(), - country: text('country').notNull(), - region: text('region').notNull(), - email: text('email').notNull() + id: uuid("id").primaryKey().defaultRandom(), + name: text("name").notNull(), + createdAt: timestamp("createdAt").defaultNow().notNull(), + country: text("country").notNull(), + region: text("region").notNull(), + email: text("email").notNull(), }, (producers) => { return { - uniqueIdx: uniqueIndex('email').on(producers.email), + uniqueIdx: uniqueIndex("email").on(producers.email), }; }, ); @@ -33,15 +33,15 @@ export const producersRelations = relations(producers, ({ many }) => ({ export const wines = createTable( "wine", { - id: uuid('id').primaryKey().defaultRandom(), - name: text('name').notNull(), - createdAt: timestamp('createdAt').defaultNow().notNull(), - updatedAt: timestamp('updatedAt').defaultNow(), - producer: uuid('producer').notNull(), + id: uuid("id").primaryKey().defaultRandom(), + name: text("name").notNull(), + createdAt: timestamp("createdAt").defaultNow().notNull(), + updatedAt: timestamp("updatedAt").defaultNow(), + producer: uuid("producer").notNull(), }, (wines) => { return { - uniqueIdx: uniqueIndex('unique_idx').on(wines.id), + uniqueIdx: uniqueIndex("unique_idx").on(wines.id), }; }, ); @@ -55,6 +55,5 @@ export const winesRelations = relations(wines, ({ one }) => ({ export type SelectWine = typeof wines.$inferSelect; - export type SelectProducer = typeof producers.$inferSelect; -export type InsertProducer = typeof producers.$inferInsert; \ No newline at end of file +export type InsertProducer = typeof producers.$inferInsert;