server recognizes db, read/write is up

This commit is contained in:
Mikayla Dobson
2022-11-18 10:07:41 -06:00
parent f57c96a80e
commit 51a34bf0e4
7 changed files with 54 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
import createError from 'http-errors'; import createError from 'http-errors';
import { IUser } from '../schemas';
import { User } from "../models/user"; import { User } from "../models/user";
const UserInstance = new User(); const UserInstance = new User();
@@ -13,6 +14,16 @@ export default class UserCtl {
} }
} }
async post(data: IUser) {
try {
const response = await UserInstance.post(data);
// if (!response) throw createError(400, "Bad request");
return response;
} catch (error: any) {
throw new Error(error);
}
}
// async getOne(id: string) { // async getOne(id: string) {
// try { // try {
// const user = await UserInstance.getOneByID(id); // const user = await UserInstance.getOneByID(id);

View File

@@ -1,8 +1,6 @@
import createConnectionPool, { sql } from "@databases/pg"; import { Pool } from 'pg';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
dotenv.config({ path: '../.env' }); dotenv.config();
const db = createConnectionPool(process.env.CONSTRING); const pool = new Pool({ connectionString: process.env.CONSTRING });
export default pool;
export { sql };
export default db;

View File

@@ -9,10 +9,10 @@ dotenv.config();
` `
const appusers = ` const appusers = `
CREATE TABLE IF NOT EXISTS recipin.app_users ( CREATE TABLE IF NOT EXISTS recipin.appusers (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
first_name varchar NOT NULL, firstname varchar NOT NULL,
last_name varchar NOT NULL, lastname varchar NOT NULL,
handle varchar NOT NULL, handle varchar NOT NULL,
email varchar NOT NULL, email varchar NOT NULL,
password varchar NOT NULL password varchar NOT NULL
@@ -31,7 +31,7 @@ dotenv.config();
CREATE TABLE IF NOT EXISTS recipin.collection ( CREATE TABLE IF NOT EXISTS recipin.collection (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name varchar NOT NULL, name varchar NOT NULL,
owner_id int REFERENCES recipin.app_users (id) ownerid int REFERENCES recipin.appusers (id)
); );
` `
@@ -41,9 +41,9 @@ dotenv.config();
name varchar NOT NULL, name varchar NOT NULL,
description varchar, description varchar,
preptime varchar, preptime varchar,
date_created varchar, datecreated varchar,
date_modified varchar, datemodified varchar,
author_user_id int REFERENCES recipin.app_users (id) NOT NULL authoruserid int REFERENCES recipin.appusers (id) NOT NULL
); );
` `
@@ -52,17 +52,17 @@ dotenv.config();
recipe_ingredient_id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, recipe_ingredient_id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
quantity decimal, quantity decimal,
unit varchar, unit varchar,
ingredient_id int REFERENCES recipin.ingredient (id), ingredientid int REFERENCES recipin.ingredient (id),
recipe_id int REFERENCES recipin.recipe (id), recipeid int REFERENCES recipin.recipe (id),
collection_id int REFERENCES recipin.collection (id) collectionid int REFERENCES recipin.collection (id)
); );
` `
const userscollections = ` const userscollections = `
CREATE TABLE IF NOT EXISTS recipin.cmp_users_collections ( CREATE TABLE IF NOT EXISTS recipin.cmp_users_collections (
users_collections_id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, userscollectionsid INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
collection_id int REFERENCES recipin.collection (id), collectionid int REFERENCES recipin.collection (id),
user_member_id int REFERENCES recipin.app_users (id) usermemberid int REFERENCES recipin.appusers (id)
); );
`; `;
@@ -80,6 +80,7 @@ dotenv.config();
await client.end(); await client.end();
} catch(e: any) { } catch(e: any) {
await client.end();
throw new Error(e); throw new Error(e);
} }

View File

@@ -1,24 +1,33 @@
import { IUser } from "../schemas"; import { IUser } from "../schemas";
import { sql } from "@databases/pg";
import pgPromise from "pg-promise"; import pgPromise from "pg-promise";
import db from "../db"; import pool from '../db';
const pgp = pgPromise({ capSQL: true }); const pgp = pgPromise({ capSQL: true });
export class User { export class User {
async getAllUsers() { async getAllUsers() {
// behind auth // behind auth
try { try {
const statement = sql`SELECT * FROM recipe` const statement = `SELECT * FROM recipin.appusers`;
const result = await db.query(statement); const result = await pool.query(statement);
console.log(result);
await db.dispose();
return result; return result;
} catch (error: any) { } catch (error: any) {
throw new Error(error); throw new Error(error);
} }
} }
async post(data: IUser) {
const { firstname, lastname, handle, email, password } = data;
try {
const statement = `INSERT INTO recipin.appusers (firstname, lastname, handle, email, password) VALUES ($1, $2, $3, $4, $5)`;
const params = [firstname, lastname, handle, email, password];
const result = await pool.query(statement, params);
if (result.rows.length) return result.rows;
return null;
} catch (error: any) {
throw new Error(error);
}
}
// async getOneByID(id: string): Promise<Array<IUser | null>> { // async getOneByID(id: string): Promise<Array<IUser | null>> {
// try { // try {
// const statement = `SELECT * FROM users WHERE id = $1;`; // const statement = `SELECT * FROM users WHERE id = $1;`;

View File

@@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"@databases/pg": "^5.4.1", "@databases/pg": "^5.4.1",
"bcrypt": "^5.1.0", "bcrypt": "^5.1.0",
"body-parser": "^1.20.1",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",

View File

@@ -13,8 +13,8 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@databases/pg": "^5.4.1",
"bcrypt": "^5.1.0", "bcrypt": "^5.1.0",
"body-parser": "^1.20.1",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"express": "^4.18.2", "express": "^4.18.2",

View File

@@ -1,4 +1,4 @@
import db, { sql } from '../db'; import pool from '../db';
import { Express, Router } from 'express'; import { Express, Router } from 'express';
import UserCtl from '../controllers/UserCtl'; import UserCtl from '../controllers/UserCtl';
const router = Router(); const router = Router();
@@ -13,6 +13,12 @@ export const userRoute = (app: Express) => {
res.status(200).send(data); res.status(200).send(data);
}) })
router.post('/', async (req, res) => {
const data = req.body;
const response = userCtl.post(data);
res.status(200).send(response);
})
router.get('/hidden-thing', (req, res) => { router.get('/hidden-thing', (req, res) => {
res.send('does this route actually work?'); res.send('does this route actually work?');
}) })