refactoring db seed
This commit is contained in:
@@ -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';
|
||||
@@ -37,6 +37,8 @@ function App() {
|
||||
<Route path="/collection" element={<Collection />} />
|
||||
<Route path="/explore" element={<Browser />} />
|
||||
<Route path="/recipe/:id" element={<Recipe />} />
|
||||
<Route path="/subscriptions" element={<Subscriptions />} />
|
||||
<Route path="/subscriptions/:id" element={<Collection />} />
|
||||
</Routes>
|
||||
</div>
|
||||
</AuthContext.Provider>
|
||||
|
||||
@@ -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 (
|
||||
<Page>
|
||||
<Protect>
|
||||
{ isDefault ?
|
||||
|
||||
<>
|
||||
<h1>Mikayla's collection</h1>
|
||||
<p>37 recipes</p>
|
||||
<p>71 ingredients</p>
|
||||
<p>11 types of cuisine</p>
|
||||
</>
|
||||
|
||||
:
|
||||
|
||||
<>
|
||||
|
||||
</>
|
||||
|
||||
}
|
||||
|
||||
|
||||
{/* recipes */}
|
||||
</Page>
|
||||
</Protect>
|
||||
)
|
||||
}
|
||||
|
||||
export default Collection;
|
||||
@@ -37,6 +37,7 @@ export default function Login() {
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (authContext) navigate('/');
|
||||
setForm(new Form<IUserAuth>(formConfig).mount())
|
||||
}, [])
|
||||
|
||||
|
||||
@@ -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<IAuthContext>(defaultValue);
|
||||
|
||||
2
server/appRoot.ts
Normal file
2
server/appRoot.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import path from 'path';
|
||||
export const appRoot = path.resolve(__dirname);
|
||||
@@ -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 {
|
||||
|
||||
11
server/db/sql/create/createappusers.sql
Normal file
11
server/db/sql/create/createappusers.sql
Normal file
@@ -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
|
||||
);
|
||||
5
server/db/sql/create/createcmp_recipecollection.sql
Normal file
5
server/db/sql/create/createcmp_recipecollection.sql
Normal file
@@ -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)
|
||||
);
|
||||
7
server/db/sql/create/createcmp_recipeingredient.sql
Normal file
7
server/db/sql/create/createcmp_recipeingredient.sql
Normal file
@@ -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)
|
||||
);
|
||||
8
server/db/sql/create/createcmp_userfriendships.sql
Normal file
8
server/db/sql/create/createcmp_userfriendships.sql
Normal file
@@ -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)
|
||||
);
|
||||
5
server/db/sql/create/createcmp_usersubscriptions.sql
Normal file
5
server/db/sql/create/createcmp_usersubscriptions.sql
Normal file
@@ -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)
|
||||
);
|
||||
9
server/db/sql/create/createcollection.sql
Normal file
9
server/db/sql/create/createcollection.sql
Normal file
@@ -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)
|
||||
);
|
||||
8
server/db/sql/create/creategrocerylist.sql
Normal file
8
server/db/sql/create/creategrocerylist.sql
Normal file
@@ -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)
|
||||
);
|
||||
7
server/db/sql/create/createingredient.sql
Normal file
7
server/db/sql/create/createingredient.sql
Normal file
@@ -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)
|
||||
);
|
||||
9
server/db/sql/create/createrecipe.sql
Normal file
9
server/db/sql/create/createrecipe.sql
Normal file
@@ -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
|
||||
);
|
||||
7
server/db/sql/create/createrecipecomments.sql
Normal file
7
server/db/sql/create/createrecipecomments.sql
Normal file
@@ -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
|
||||
);
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user