From f83fbdb17ba9a334c42e367af9c3394a7c617272 Mon Sep 17 00:00:00 2001 From: labintsev Date: Wed, 11 Dec 2024 15:16:14 +0300 Subject: [PATCH] fix db tables --- packages/nextjs/app/lib/data.ts | 28 +++ packages/nextjs/app/lib/definitions.ts | 9 + packages/nextjs/app/seed/placeholder-data.ts | 182 ++---------------- packages/nextjs/app/seed/route.ts | 123 +----------- .../app/voting/_components/VotingRadio.tsx | 4 + packages/nextjs/package.json | 1 - 6 files changed, 67 insertions(+), 280 deletions(-) create mode 100644 packages/nextjs/app/lib/data.ts create mode 100644 packages/nextjs/app/lib/definitions.ts diff --git a/packages/nextjs/app/lib/data.ts b/packages/nextjs/app/lib/data.ts new file mode 100644 index 0000000..07f2260 --- /dev/null +++ b/packages/nextjs/app/lib/data.ts @@ -0,0 +1,28 @@ +import { Choice, Question } from "./definitions"; +import { sql } from "@vercel/postgres"; + +export async function fetchQuestions() { + try { + // Artificially delay a response for demo purposes. + // Don't do this in production :) + + // console.log('Fetching revenue data...'); + // await new Promise((resolve) => setTimeout(resolve, 3000)); + + const questions = await sql`SELECT * FROM questions`; + + const choices = await sql`SELECT * FROM choices`; + + // const q_rows = questions.rows; + + // todo join questions and choices + + console.log("Questions fetched ", questions); + console.log("Choices fetched ", choices); + + return questions.rows; + } catch (error) { + console.error("Database Error:", error); + throw new Error("Failed to fetch data."); + } +} diff --git a/packages/nextjs/app/lib/definitions.ts b/packages/nextjs/app/lib/definitions.ts new file mode 100644 index 0000000..95d4a0e --- /dev/null +++ b/packages/nextjs/app/lib/definitions.ts @@ -0,0 +1,9 @@ +export type Question = { + question: string; + is_active: number; +}; + +export type Choice = { + question_id: string; + text: string; +}; diff --git a/packages/nextjs/app/seed/placeholder-data.ts b/packages/nextjs/app/seed/placeholder-data.ts index 65a11eb..a791169 100644 --- a/packages/nextjs/app/seed/placeholder-data.ts +++ b/packages/nextjs/app/seed/placeholder-data.ts @@ -9,17 +9,17 @@ const shareholders = [ const questions = [ { - id: 0, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd81a0", question: "Do you enjoy this voting application?", is_active: 1, }, { - id: 1, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd81a1", question: "Who will be Chief Executive Officer in 2025?", is_active: 1, }, { - id: 2, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd81a2", question: "Do you approve this financial report?", is_active: 1, }, @@ -27,45 +27,45 @@ const questions = [ const question_choices = [ { - id: 0, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8100", question_id: questions[0].id, - choice: "Yes", + text: "Yes", }, { - id: 1, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8101", question_id: questions[0].id, - choice: "No", + text: "No", }, { - id: 2, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8102", question_id: questions[1].id, - choice: "Donald Duck", + text: "Donald Duck", }, { - id: 3, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8103", question_id: questions[1].id, - choice: "John Smith", + text: "John Smith", }, { - id: 4, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8104", question_id: questions[1].id, - choice: "Billy Bones", + text: "Billy Bones", }, { - id: 5, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8105", question_id: questions[2].id, - choice: "Yes", + text: "Yes", }, { - id: 6, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8106", question_id: questions[2].id, - choice: "No", + text: "No", }, ]; const shareholder_question_answers = [ { - id: 0, + id: "d6e15727-9fe1-4961-8c5b-ea44a9bd8107", sh_id: shareholders[0].id, q_id: question_choices[0].id, choice_id: question_choices[0].id, @@ -73,148 +73,4 @@ const shareholder_question_answers = [ }, ]; -const users = [ - { - id: "410544b2-4001-4271-9855-fec4b6a6442a", - name: "User", - email: "user@nextmail.com", - password: "123456", - }, -]; - -const customers = [ - { - id: "d6e15727-9fe1-4961-8c5b-ea44a9bd81aa", - name: "Evil Rabbit", - email: "evil@rabbit.com", - image_url: "/customers/evil-rabbit.png", - }, - { - id: "3958dc9e-712f-4377-85e9-fec4b6a6442a", - name: "Delba de Oliveira", - email: "delba@oliveira.com", - image_url: "/customers/delba-de-oliveira.png", - }, - { - id: "3958dc9e-742f-4377-85e9-fec4b6a6442a", - name: "Lee Robinson", - email: "lee@robinson.com", - image_url: "/customers/lee-robinson.png", - }, - { - id: "76d65c26-f784-44a2-ac19-586678f7c2f2", - name: "Michael Novotny", - email: "michael@novotny.com", - image_url: "/customers/michael-novotny.png", - }, - { - id: "CC27C14A-0ACF-4F4A-A6C9-D45682C144B9", - name: "Amy Burns", - email: "amy@burns.com", - image_url: "/customers/amy-burns.png", - }, - { - id: "13D07535-C59E-4157-A011-F8D2EF4E0CBB", - name: "Balazs Orban", - email: "balazs@orban.com", - image_url: "/customers/balazs-orban.png", - }, -]; - -const invoices = [ - { - customer_id: customers[0].id, - amount: 15795, - status: "pending", - date: "2022-12-06", - }, - { - customer_id: customers[1].id, - amount: 20348, - status: "pending", - date: "2022-11-14", - }, - { - customer_id: customers[4].id, - amount: 3040, - status: "paid", - date: "2022-10-29", - }, - { - customer_id: customers[3].id, - amount: 44800, - status: "paid", - date: "2023-09-10", - }, - { - customer_id: customers[5].id, - amount: 34577, - status: "pending", - date: "2023-08-05", - }, - { - customer_id: customers[2].id, - amount: 54246, - status: "pending", - date: "2023-07-16", - }, - { - customer_id: customers[0].id, - amount: 666, - status: "pending", - date: "2023-06-27", - }, - { - customer_id: customers[3].id, - amount: 32545, - status: "paid", - date: "2023-06-09", - }, - { - customer_id: customers[4].id, - amount: 1250, - status: "paid", - date: "2023-06-17", - }, - { - customer_id: customers[5].id, - amount: 8546, - status: "paid", - date: "2023-06-07", - }, - { - customer_id: customers[1].id, - amount: 500, - status: "paid", - date: "2023-08-19", - }, - { - customer_id: customers[5].id, - amount: 8945, - status: "paid", - date: "2023-06-03", - }, - { - customer_id: customers[2].id, - amount: 1000, - status: "paid", - date: "2022-06-05", - }, -]; - -const revenue = [ - { month: "Jan", revenue: 2000 }, - { month: "Feb", revenue: 1800 }, - { month: "Mar", revenue: 2200 }, - { month: "Apr", revenue: 2500 }, - { month: "May", revenue: 2300 }, - { month: "Jun", revenue: 3200 }, - { month: "Jul", revenue: 3500 }, - { month: "Aug", revenue: 3700 }, - { month: "Sep", revenue: 2500 }, - { month: "Oct", revenue: 2800 }, - { month: "Nov", revenue: 3000 }, - { month: "Dec", revenue: 4800 }, -]; - -export { users, customers, invoices, revenue, shareholders, questions, question_choices, shareholder_question_answers }; +export { shareholders, questions, question_choices, shareholder_question_answers }; diff --git a/packages/nextjs/app/seed/route.ts b/packages/nextjs/app/seed/route.ts index dfb5be9..6176186 100644 --- a/packages/nextjs/app/seed/route.ts +++ b/packages/nextjs/app/seed/route.ts @@ -1,15 +1,7 @@ -import { - customers, - invoices, - question_choices, - questions, - revenue, - shareholder_question_answers, - shareholders, - users, -} from "./placeholder-data"; +import { question_choices, questions, shareholder_question_answers, shareholders } from "./placeholder-data"; +import { sha256 } from "@noble/hashes/sha2"; +import { bytesToHex } from "@noble/hashes/utils"; import { db } from "@vercel/postgres"; -import bcrypt from "bcrypt"; const client = await db.connect(); @@ -26,7 +18,7 @@ async function seedShareholders() { const insertedShareholders = await Promise.all( shareholders.map(async sholder => { - const hashedPassword = await bcrypt.hash(sholder.password, 10); + const hashedPassword = bytesToHex(sha256(sholder.password)); return client.sql` INSERT INTO shareholders (id, name, email, password) VALUES (${sholder.id}, ${sholder.name}, ${sholder.email}, ${hashedPassword}) @@ -69,7 +61,7 @@ async function seedChoices() { CREATE TABLE IF NOT EXISTS choices ( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, question_id UUID NOT NULL, - choice VARCHAR(255) NOT NULL, + choice TEXT NOT NULL ); `; @@ -77,7 +69,7 @@ async function seedChoices() { question_choices.map( choice => client.sql` INSERT INTO choices (id, question_id, choice) - VALUES (${choice.id}, ${choice.question_id}, ${choice.choice} ) + VALUES (${choice.id}, ${choice.question_id}, ${choice.text} ) ON CONFLICT (id) DO NOTHING; `, ), @@ -103,7 +95,7 @@ async function seedShareholderAnswers() { shareholder_question_answers.map( answer => client.sql` INSERT INTO answers (id, sh_id, question_id, choice_id, answer_time) - VALUES (${answer.id}, ${answer.q_id}, ${answer.choice_id}, to_timestamp(${Date.now()} / 1000.0) ) + VALUES (${answer.id}, ${answer.sh_id}, ${answer.q_id}, ${answer.choice_id}, now() ) ON CONFLICT (id) DO NOTHING; `, ), @@ -112,103 +104,6 @@ async function seedShareholderAnswers() { return insertedAnswers; } -async function seedUsers() { - await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; - await client.sql` - CREATE TABLE IF NOT EXISTS users ( - id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, - name VARCHAR(255) NOT NULL, - email TEXT NOT NULL UNIQUE, - password TEXT NOT NULL - ); - `; - - const insertedUsers = await Promise.all( - users.map(async user => { - const hashedPassword = await bcrypt.hash(user.password, 10); - return client.sql` - INSERT INTO users (id, name, email, password) - VALUES (${user.id}, ${user.name}, ${user.email}, ${hashedPassword}) - ON CONFLICT (id) DO NOTHING; - `; - }), - ); - - return insertedUsers; -} - -async function seedInvoices() { - await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; - - await client.sql` - CREATE TABLE IF NOT EXISTS invoices ( - id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, - customer_id UUID NOT NULL, - amount INT NOT NULL, - status VARCHAR(255) NOT NULL, - date DATE NOT NULL - ); - `; - - const insertedInvoices = await Promise.all( - invoices.map( - invoice => client.sql` - INSERT INTO invoices (customer_id, amount, status, date) - VALUES (${invoice.customer_id}, ${invoice.amount}, ${invoice.status}, ${invoice.date}) - ON CONFLICT (id) DO NOTHING; - `, - ), - ); - - return insertedInvoices; -} - -async function seedCustomers() { - await client.sql`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`; - - await client.sql` - CREATE TABLE IF NOT EXISTS customers ( - id UUID DEFAULT uuid_generate_v4() PRIMARY KEY, - name VARCHAR(255) NOT NULL, - email VARCHAR(255) NOT NULL, - image_url VARCHAR(255) NOT NULL - ); - `; - - const insertedCustomers = await Promise.all( - customers.map( - customer => client.sql` - INSERT INTO customers (id, name, email, image_url) - VALUES (${customer.id}, ${customer.name}, ${customer.email}, ${customer.image_url}) - ON CONFLICT (id) DO NOTHING; - `, - ), - ); - - return insertedCustomers; -} - -async function seedRevenue() { - await client.sql` - CREATE TABLE IF NOT EXISTS revenue ( - month VARCHAR(4) NOT NULL UNIQUE, - revenue INT NOT NULL - ); - `; - - const insertedRevenue = await Promise.all( - revenue.map( - rev => client.sql` - INSERT INTO revenue (month, revenue) - VALUES (${rev.month}, ${rev.revenue}) - ON CONFLICT (month) DO NOTHING; - `, - ), - ); - - return insertedRevenue; -} - export async function GET() { // return Response.json({ // message: @@ -220,10 +115,6 @@ export async function GET() { await seedQuestions(); await seedChoices(); await seedShareholderAnswers(); - await seedUsers(); - await seedCustomers(); - await seedInvoices(); - await seedRevenue(); await client.sql`COMMIT`; return Response.json({ message: "Database seeded successfully" }); diff --git a/packages/nextjs/app/voting/_components/VotingRadio.tsx b/packages/nextjs/app/voting/_components/VotingRadio.tsx index 79b3f59..972a5ce 100644 --- a/packages/nextjs/app/voting/_components/VotingRadio.tsx +++ b/packages/nextjs/app/voting/_components/VotingRadio.tsx @@ -1,6 +1,7 @@ "use client"; import React, { useState } from "react"; +import { fetchQuestions } from "~~/app/lib/data"; const questions = [ { @@ -13,6 +14,9 @@ const questions = [ }, ]; +const db_questions = fetchQuestions(); +console.log(db_questions); + function VotingRadio() { const [checkBox, setCheckBox] = useState(); diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 26b9e3e..b4c639d 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -17,7 +17,6 @@ "@heroicons/react": "^2.0.11", "@rainbow-me/rainbowkit": "2.1.2", "@tanstack/react-query": "^5.28.6", - "@types/bcrypt": "^5.0.0", "@uniswap/sdk-core": "^4.0.1", "@uniswap/v2-sdk": "^3.0.1", "@vercel/postgres": "^0.10.0",