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 { IUser } from '../schemas';
import { User } from "../models/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) {
// try {
// 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';
dotenv.config({ path: '../.env' });
dotenv.config();
const db = createConnectionPool(process.env.CONSTRING);
export { sql };
export default db;
const pool = new Pool({ connectionString: process.env.CONSTRING });
export default pool;

View File

@@ -9,10 +9,10 @@ dotenv.config();
`
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,
first_name varchar NOT NULL,
last_name varchar NOT NULL,
firstname varchar NOT NULL,
lastname varchar NOT NULL,
handle varchar NOT NULL,
email varchar NOT NULL,
password varchar NOT NULL
@@ -31,7 +31,7 @@ dotenv.config();
CREATE TABLE IF NOT EXISTS recipin.collection (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
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,
description varchar,
preptime varchar,
date_created varchar,
date_modified varchar,
author_user_id int REFERENCES recipin.app_users (id) NOT NULL
datecreated varchar,
datemodified varchar,
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,
quantity decimal,
unit varchar,
ingredient_id int REFERENCES recipin.ingredient (id),
recipe_id int REFERENCES recipin.recipe (id),
collection_id int REFERENCES recipin.collection (id)
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_users_collections (
users_collections_id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
collection_id int REFERENCES recipin.collection (id),
user_member_id int REFERENCES recipin.app_users (id)
userscollectionsid INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
collectionid int REFERENCES recipin.collection (id),
usermemberid int REFERENCES recipin.appusers (id)
);
`;
@@ -80,6 +80,7 @@ dotenv.config();
await client.end();
} catch(e: any) {
await client.end();
throw new Error(e);
}

View File

@@ -1,24 +1,33 @@
import { IUser } from "../schemas";
import { sql } from "@databases/pg";
import pgPromise from "pg-promise";
import db from "../db";
import pool from '../db';
const pgp = pgPromise({ capSQL: true });
export class User {
async getAllUsers() {
// behind auth
try {
const statement = sql`SELECT * FROM recipe`
const result = await db.query(statement);
console.log(result);
await db.dispose();
const statement = `SELECT * FROM recipin.appusers`;
const result = await pool.query(statement);
return result;
} catch (error: any) {
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>> {
// try {
// const statement = `SELECT * FROM users WHERE id = $1;`;

View File

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

View File

@@ -13,8 +13,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@databases/pg": "^5.4.1",
"bcrypt": "^5.1.0",
"body-parser": "^1.20.1",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"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 UserCtl from '../controllers/UserCtl';
const router = Router();
@@ -13,6 +13,12 @@ export const userRoute = (app: Express) => {
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) => {
res.send('does this route actually work?');
})