From b7e33b74e05254b52254be1730faf4c534017853 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Wed, 23 Nov 2022 12:28:22 -0600 Subject: [PATCH] refactoring db seed --- client/src/App.tsx | 22 ++-- client/src/components/pages/Collection.tsx | 36 +++++- client/src/components/pages/Login.tsx | 1 + client/src/context/AuthContext.tsx | 5 +- server/appRoot.ts | 2 + server/db/seed.ts | 115 +++--------------- server/db/sql/create/createappusers.sql | 11 ++ .../sql/create/createcmp_recipecollection.sql | 5 + .../sql/create/createcmp_recipeingredient.sql | 7 ++ .../sql/create/createcmp_userfriendships.sql | 8 ++ .../create/createcmp_usersubscriptions.sql | 5 + server/db/sql/create/createcollection.sql | 9 ++ server/db/sql/create/creategrocerylist.sql | 8 ++ server/db/sql/create/createingredient.sql | 7 ++ server/db/sql/create/createrecipe.sql | 9 ++ server/db/sql/create/createrecipecomments.sql | 7 ++ server/db/sql/{ => derived}/friendships.sql | 0 server/models/user.ts | 2 +- 18 files changed, 139 insertions(+), 120 deletions(-) create mode 100644 server/appRoot.ts create mode 100644 server/db/sql/create/createappusers.sql create mode 100644 server/db/sql/create/createcmp_recipecollection.sql create mode 100644 server/db/sql/create/createcmp_recipeingredient.sql create mode 100644 server/db/sql/create/createcmp_userfriendships.sql create mode 100644 server/db/sql/create/createcmp_usersubscriptions.sql create mode 100644 server/db/sql/create/createcollection.sql create mode 100644 server/db/sql/create/creategrocerylist.sql create mode 100644 server/db/sql/create/createingredient.sql create mode 100644 server/db/sql/create/createrecipe.sql create mode 100644 server/db/sql/create/createrecipecomments.sql rename server/db/sql/{ => derived}/friendships.sql (100%) diff --git a/client/src/App.tsx b/client/src/App.tsx index 6b077a1..fd8d6eb 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,8 +1,8 @@ -import { useEffect, useState } from 'react'; import { BrowserRouter, Routes, Route } from 'react-router-dom'; -import { IUser } from './schemas'; +import { useEffect, useState } from 'react'; +import { AuthContext, IAuthContext } from './context/AuthContext'; import { checkCredientials } from './util/apiUtils'; -import { AuthContext, defaultValue, IAuthContext } from './context/AuthContext'; +import Subscriptions from './components/pages/Subscriptions/Subscriptions'; import Browser from './components/pages/Browser'; import Collection from './components/pages/Collection'; import Login from './components/pages/Login'; @@ -30,13 +30,15 @@ function App() {
- } /> - } /> - } /> - } /> - } /> - } /> - } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } /> + } />
diff --git a/client/src/components/pages/Collection.tsx b/client/src/components/pages/Collection.tsx index 31a9c3c..ffcc498 100644 --- a/client/src/components/pages/Collection.tsx +++ b/client/src/components/pages/Collection.tsx @@ -1,14 +1,40 @@ -import { Page } from "../ui"; +import { useAuthContext } from "../../context/AuthContext"; +import Protect from "../../util/Protect"; +import { useParams } from "react-router-dom"; +import { useState } from "react"; + +const Collection = () => { + const [isDefault, setIsDefault] = useState(true); + const { user } = useAuthContext(); + const { id } = useParams(); + + if (id) { + setIsDefault(false); + } -export default function Collection() { return ( - + + { isDefault ? + + <>

Mikayla's collection

37 recipes

71 ingredients

11 types of cuisine

+ + + : + + <> + + + + } + {/* recipes */} -
+ ) -} \ No newline at end of file +} + +export default Collection; \ No newline at end of file diff --git a/client/src/components/pages/Login.tsx b/client/src/components/pages/Login.tsx index ebf186d..7e32af5 100644 --- a/client/src/components/pages/Login.tsx +++ b/client/src/components/pages/Login.tsx @@ -37,6 +37,7 @@ export default function Login() { } useEffect(() => { + if (authContext) navigate('/'); setForm(new Form(formConfig).mount()) }, []) diff --git a/client/src/context/AuthContext.tsx b/client/src/context/AuthContext.tsx index 535442a..eb0ed00 100644 --- a/client/src/context/AuthContext.tsx +++ b/client/src/context/AuthContext.tsx @@ -1,13 +1,12 @@ -import { createContext, useContext } from "react"; +import { createContext, Dispatch, SetStateAction, useContext } from "react"; import { IUser } from "../schemas"; - export interface IAuthContext { user?: IUser } export const defaultValue: IAuthContext = { - user: undefined, + user: undefined } export const AuthContext = createContext(defaultValue); diff --git a/server/appRoot.ts b/server/appRoot.ts new file mode 100644 index 0000000..690f7be --- /dev/null +++ b/server/appRoot.ts @@ -0,0 +1,2 @@ +import path from 'path'; +export const appRoot = path.resolve(__dirname); \ No newline at end of file diff --git a/server/db/seed.ts b/server/db/seed.ts index c586402..533d2c3 100644 --- a/server/db/seed.ts +++ b/server/db/seed.ts @@ -1,7 +1,8 @@ -import { Client } from 'pg'; import dotenv from 'dotenv'; import populate from "./populate"; import pool from "."; +import fs from "fs"; +import { appRoot } from "../appRoot"; dotenv.config(); @@ -12,109 +13,21 @@ dotenv.config(); CREATE SCHEMA IF NOT EXISTS recipin; ` - const appusers = ` - CREATE TABLE IF NOT EXISTS recipin.appusers ( - id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - firstname varchar NOT NULL, - lastname varchar NOT NULL, - handle varchar NOT NULL UNIQUE, - email varchar NOT NULL UNIQUE, - password varchar NOT NULL, - active boolean NOT NULL, - datecreated varchar NOT NULL, - datemodified varchar NOT NULL - ); - ` - - const ingredient = ` - CREATE TABLE IF NOT EXISTS recipin.ingredient ( - id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - name varchar NOT NULL, - description varchar, - datecreated varchar NOT NULL, - createdbyid int REFERENCES recipin.appusers (id) - ); - ` - - const collection = ` - CREATE TABLE IF NOT EXISTS recipin.collection ( - id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - name varchar NOT NULL, - active boolean NOT NULL, - ismaincollection boolean NOT NULL, - datecreated varchar NOT NULL, - datemodified varchar NOT NULL, - ownerid int REFERENCES recipin.appusers (id) - ); - ` - - const groceryList = ` - CREATE TABLE IF NOT EXISTS recipin.grocerylist ( - id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - name varchar NOT NULL, - active boolean NOT NULL, - datecreated varchar NOT NULL, - datemodified varchar NOT NULL, - ownerid int REFERENCES recipin.appusers (id) - ); - ` - - - const recipe = ` - CREATE TABLE IF NOT EXISTS recipin.recipe ( - id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - name varchar NOT NULL, - preptime varchar NOT NULL, - datecreated varchar NOT NULL, - datemodified varchar NOT NULL, - description varchar, - authoruserid int REFERENCES recipin.appusers (id) NOT NULL - ); - ` - - const recipecomments = ` - CREATE TABLE IF NOT EXISTS recipin.recipecomments ( - id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - commentbody varchar NOT NULL, - datecreated varchar NOT NULL, - recipeid int REFERENCES recipin.recipe (id) NOT NULL, - authorid int REFERENCES recipin.appusers (id) NOT NULL - ); - ` - - const recipeingredient = ` - CREATE TABLE IF NOT EXISTS recipin.cmp_recipeingredient ( - recipeingredientid INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - quantity decimal NOT NULL, - unit varchar NOT NULL, - ingredientid int REFERENCES recipin.ingredient (id), - recipeid int REFERENCES recipin.recipe (id), - collectionid int REFERENCES recipin.collection (id) - ); - ` - - const userscollections = ` - CREATE TABLE IF NOT EXISTS recipin.cmp_userscollections ( - userscollectionsid INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - collectionid int REFERENCES recipin.collection (id), - usermemberid int REFERENCES recipin.appusers (id) - ); - `; - - const userfriendships = ` - CREATE TABLE IF NOT EXISTS recipin.cmp_userfriendships ( - id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, - datecreated varchar NOT NULL, - active boolean NOT NULL, - dateterminated varchar, - firstuserid int REFERENCES recipin.appusers (id), - seconduserid int REFERENCES recipin.appusers (id) - ); - ` + const appusers = fs.readFileSync(appRoot + '/db/sql/create/createappusers.sql').toString(); + const ingredient = fs.readFileSync(appRoot + '/db/sql/create/createingredient.sql').toString(); + const collection = fs.readFileSync(appRoot + '/db/sql/create/createcollection.sql').toString(); + const groceryList = fs.readFileSync(appRoot + '/db/sql/create/creategrocerylist.sql').toString(); + const recipe = fs.readFileSync(appRoot + '/db/sql/create/createrecipe.sql').toString(); + const recipecomments = fs.readFileSync(appRoot + '/db/sql/create/createrecipecomments.sql').toString(); + const recipeingredient = fs.readFileSync(appRoot + '/db/sql/create/createcmp_recipeingredient.sql').toString(); + const recipecollection = fs.readFileSync(appRoot + '/db/sql/create/createcmp_recipecollection.sql').toString(); + const usersubscriptions = fs.readFileSync(appRoot + '/db/sql/create/createcmp_usersubscriptions.sql').toString(); + const userfriendships = fs.readFileSync(appRoot + '/db/sql/create/createcmp_userfriendships.sql').toString(); const allStatements = [ setRole, appusers, ingredient, collection, recipe, recipecomments, - groceryList, recipeingredient, userscollections, userfriendships + groceryList, recipeingredient, recipecollection, usersubscriptions, + userfriendships ] try { diff --git a/server/db/sql/create/createappusers.sql b/server/db/sql/create/createappusers.sql new file mode 100644 index 0000000..b1fd575 --- /dev/null +++ b/server/db/sql/create/createappusers.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS recipin.appusers ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + firstname varchar NOT NULL, + lastname varchar NOT NULL, + handle varchar NOT NULL UNIQUE, + email varchar NOT NULL UNIQUE, + password varchar NOT NULL, + active boolean NOT NULL, + datecreated varchar NOT NULL, + datemodified varchar NOT NULL +); \ No newline at end of file diff --git a/server/db/sql/create/createcmp_recipecollection.sql b/server/db/sql/create/createcmp_recipecollection.sql new file mode 100644 index 0000000..042a374 --- /dev/null +++ b/server/db/sql/create/createcmp_recipecollection.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS recipin.cmp_recipecollection ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + recipeid int REFERENCES recipin.recipe (id), + collectionid int REFERENCES recipin.collection (id) +); \ No newline at end of file diff --git a/server/db/sql/create/createcmp_recipeingredient.sql b/server/db/sql/create/createcmp_recipeingredient.sql new file mode 100644 index 0000000..714b8fd --- /dev/null +++ b/server/db/sql/create/createcmp_recipeingredient.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS recipin.cmp_recipeingredient ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + quantity decimal NOT NULL, + unit varchar NOT NULL, + ingredientid int REFERENCES recipin.ingredient (id), + recipeid int REFERENCES recipin.recipe (id) +); \ No newline at end of file diff --git a/server/db/sql/create/createcmp_userfriendships.sql b/server/db/sql/create/createcmp_userfriendships.sql new file mode 100644 index 0000000..73e8a49 --- /dev/null +++ b/server/db/sql/create/createcmp_userfriendships.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS recipin.cmp_userfriendships ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + datecreated varchar NOT NULL, + active boolean NOT NULL, + dateterminated varchar, + firstuserid int REFERENCES recipin.appusers (id), + seconduserid int REFERENCES recipin.appusers (id) +); \ No newline at end of file diff --git a/server/db/sql/create/createcmp_usersubscriptions.sql b/server/db/sql/create/createcmp_usersubscriptions.sql new file mode 100644 index 0000000..47e8ecb --- /dev/null +++ b/server/db/sql/create/createcmp_usersubscriptions.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS recipin.cmp_usersubscriptions ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + collectionid int REFERENCES recipin.collection (id), + usermemberid int REFERENCES recipin.appusers (id) +); \ No newline at end of file diff --git a/server/db/sql/create/createcollection.sql b/server/db/sql/create/createcollection.sql new file mode 100644 index 0000000..e836bcb --- /dev/null +++ b/server/db/sql/create/createcollection.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS recipin.collection ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name varchar NOT NULL, + active boolean NOT NULL, + ismaincollection boolean NOT NULL, + datecreated varchar NOT NULL, + datemodified varchar NOT NULL, + ownerid int REFERENCES recipin.appusers (id) +); \ No newline at end of file diff --git a/server/db/sql/create/creategrocerylist.sql b/server/db/sql/create/creategrocerylist.sql new file mode 100644 index 0000000..18cb4d3 --- /dev/null +++ b/server/db/sql/create/creategrocerylist.sql @@ -0,0 +1,8 @@ +CREATE TABLE IF NOT EXISTS recipin.grocerylist ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name varchar NOT NULL, + active boolean NOT NULL, + datecreated varchar NOT NULL, + datemodified varchar NOT NULL, + ownerid int REFERENCES recipin.appusers (id) +); \ No newline at end of file diff --git a/server/db/sql/create/createingredient.sql b/server/db/sql/create/createingredient.sql new file mode 100644 index 0000000..477e06c --- /dev/null +++ b/server/db/sql/create/createingredient.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS recipin.ingredient ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name varchar NOT NULL, + description varchar, + datecreated varchar NOT NULL, + createdbyid int REFERENCES recipin.appusers (id) +); \ No newline at end of file diff --git a/server/db/sql/create/createrecipe.sql b/server/db/sql/create/createrecipe.sql new file mode 100644 index 0000000..4996c5f --- /dev/null +++ b/server/db/sql/create/createrecipe.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS recipin.recipe ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + name varchar NOT NULL, + preptime varchar NOT NULL, + datecreated varchar NOT NULL, + datemodified varchar NOT NULL, + description varchar, + authoruserid int REFERENCES recipin.appusers (id) NOT NULL +); \ No newline at end of file diff --git a/server/db/sql/create/createrecipecomments.sql b/server/db/sql/create/createrecipecomments.sql new file mode 100644 index 0000000..fa65a1a --- /dev/null +++ b/server/db/sql/create/createrecipecomments.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS recipin.recipecomments ( + id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + commentbody varchar NOT NULL, + datecreated varchar NOT NULL, + recipeid int REFERENCES recipin.recipe (id) NOT NULL, + authorid int REFERENCES recipin.appusers (id) NOT NULL +); \ No newline at end of file diff --git a/server/db/sql/friendships.sql b/server/db/sql/derived/friendships.sql similarity index 100% rename from server/db/sql/friendships.sql rename to server/db/sql/derived/friendships.sql diff --git a/server/models/user.ts b/server/models/user.ts index 37f874c..48be583 100644 --- a/server/models/user.ts +++ b/server/models/user.ts @@ -96,7 +96,7 @@ export class User { async getFriends(id: string) { try { - const sql = fs.readFileSync(appRoot + '/db/sql/friendships.sql').toString(); + const sql = fs.readFileSync(appRoot + '/db/sql/derived/friendships.sql').toString(); const result = await pool.query(sql, [id]); if (result.rows.length) return result.rows; return null;