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 { 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 { 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 Browser from './components/pages/Browser';
|
||||||
import Collection from './components/pages/Collection';
|
import Collection from './components/pages/Collection';
|
||||||
import Login from './components/pages/Login';
|
import Login from './components/pages/Login';
|
||||||
@@ -30,13 +30,15 @@ function App() {
|
|||||||
<AuthContext.Provider value={ user }>
|
<AuthContext.Provider value={ user }>
|
||||||
<div className="App">
|
<div className="App">
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<Welcome />} />
|
<Route path="/" element={<Welcome />} />
|
||||||
<Route path="/register" element={<Register />} />
|
<Route path="/register" element={<Register />} />
|
||||||
<Route path="/login" element={<Login />} />
|
<Route path="/login" element={<Login />} />
|
||||||
<Route path="/profile" element={<Profile />} />
|
<Route path="/profile" element={<Profile />} />
|
||||||
<Route path="/collection" element={<Collection />} />
|
<Route path="/collection" element={<Collection />} />
|
||||||
<Route path="/explore" element={<Browser />} />
|
<Route path="/explore" element={<Browser />} />
|
||||||
<Route path="/recipe/:id" element={<Recipe />} />
|
<Route path="/recipe/:id" element={<Recipe />} />
|
||||||
|
<Route path="/subscriptions" element={<Subscriptions />} />
|
||||||
|
<Route path="/subscriptions/:id" element={<Collection />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</div>
|
||||||
</AuthContext.Provider>
|
</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 (
|
return (
|
||||||
<Page>
|
<Protect>
|
||||||
|
{ isDefault ?
|
||||||
|
|
||||||
|
<>
|
||||||
<h1>Mikayla's collection</h1>
|
<h1>Mikayla's collection</h1>
|
||||||
<p>37 recipes</p>
|
<p>37 recipes</p>
|
||||||
<p>71 ingredients</p>
|
<p>71 ingredients</p>
|
||||||
<p>11 types of cuisine</p>
|
<p>11 types of cuisine</p>
|
||||||
|
</>
|
||||||
|
|
||||||
|
:
|
||||||
|
|
||||||
|
<>
|
||||||
|
|
||||||
|
</>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
{/* recipes */}
|
{/* recipes */}
|
||||||
</Page>
|
</Protect>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default Collection;
|
||||||
@@ -37,6 +37,7 @@ export default function Login() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (authContext) navigate('/');
|
||||||
setForm(new Form<IUserAuth>(formConfig).mount())
|
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";
|
import { IUser } from "../schemas";
|
||||||
|
|
||||||
|
|
||||||
export interface IAuthContext {
|
export interface IAuthContext {
|
||||||
user?: IUser
|
user?: IUser
|
||||||
}
|
}
|
||||||
|
|
||||||
export const defaultValue: IAuthContext = {
|
export const defaultValue: IAuthContext = {
|
||||||
user: undefined,
|
user: undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AuthContext = createContext<IAuthContext>(defaultValue);
|
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 dotenv from 'dotenv';
|
||||||
import populate from "./populate";
|
import populate from "./populate";
|
||||||
import pool from ".";
|
import pool from ".";
|
||||||
|
import fs from "fs";
|
||||||
|
import { appRoot } from "../appRoot";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
@@ -12,109 +13,21 @@ dotenv.config();
|
|||||||
CREATE SCHEMA IF NOT EXISTS recipin;
|
CREATE SCHEMA IF NOT EXISTS recipin;
|
||||||
`
|
`
|
||||||
|
|
||||||
const appusers = `
|
const appusers = fs.readFileSync(appRoot + '/db/sql/create/createappusers.sql').toString();
|
||||||
CREATE TABLE IF NOT EXISTS recipin.appusers (
|
const ingredient = fs.readFileSync(appRoot + '/db/sql/create/createingredient.sql').toString();
|
||||||
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
const collection = fs.readFileSync(appRoot + '/db/sql/create/createcollection.sql').toString();
|
||||||
firstname varchar NOT NULL,
|
const groceryList = fs.readFileSync(appRoot + '/db/sql/create/creategrocerylist.sql').toString();
|
||||||
lastname varchar NOT NULL,
|
const recipe = fs.readFileSync(appRoot + '/db/sql/create/createrecipe.sql').toString();
|
||||||
handle varchar NOT NULL UNIQUE,
|
const recipecomments = fs.readFileSync(appRoot + '/db/sql/create/createrecipecomments.sql').toString();
|
||||||
email varchar NOT NULL UNIQUE,
|
const recipeingredient = fs.readFileSync(appRoot + '/db/sql/create/createcmp_recipeingredient.sql').toString();
|
||||||
password varchar NOT NULL,
|
const recipecollection = fs.readFileSync(appRoot + '/db/sql/create/createcmp_recipecollection.sql').toString();
|
||||||
active boolean NOT NULL,
|
const usersubscriptions = fs.readFileSync(appRoot + '/db/sql/create/createcmp_usersubscriptions.sql').toString();
|
||||||
datecreated varchar NOT NULL,
|
const userfriendships = fs.readFileSync(appRoot + '/db/sql/create/createcmp_userfriendships.sql').toString();
|
||||||
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 allStatements = [
|
const allStatements = [
|
||||||
setRole, appusers, ingredient, collection, recipe, recipecomments,
|
setRole, appusers, ingredient, collection, recipe, recipecomments,
|
||||||
groceryList, recipeingredient, userscollections, userfriendships
|
groceryList, recipeingredient, recipecollection, usersubscriptions,
|
||||||
|
userfriendships
|
||||||
]
|
]
|
||||||
|
|
||||||
try {
|
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) {
|
async getFriends(id: string) {
|
||||||
try {
|
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]);
|
const result = await pool.query(sql, [id]);
|
||||||
if (result.rows.length) return result.rows;
|
if (result.rows.length) return result.rows;
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user