lots of layout; server not really running yet

This commit is contained in:
Mikayla Dobson
2022-11-17 18:08:31 -06:00
parent 798da777a9
commit 628d0dc04b
11 changed files with 2086 additions and 71 deletions

View File

@@ -0,0 +1,35 @@
import createError from 'http-errors';
import { User } from "../models/user";
const UserInstance = new User();
export default class UserCtl {
async getAll() {
try {
const users = await UserInstance.getAllUsers();
if (!users) throw createError(404, "No users found");
return users;
} catch (error) {
throw new Error(error);
}
}
// async getOne(id: string) {
// try {
// const user = await UserInstance.getOneByID(id);
// if (!user) throw createError(404, "User not found");
// return user;
// } catch (error) {
// throw new Error(error);
// }
// }
// async updateOne(id: string, data: IUser) {
// try {
// const result = await UserInstance.updateOneByID(id, data);
// if (!result) throw createError(400, "Bad request");
// return result;
// } catch (error) {
// throw new Error(error);
// }
// }
}

8
server/db/index.ts Normal file
View File

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

View File

@@ -1,5 +1,8 @@
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
dotenv.config({ path: './.env' });
import { loaders } from './loaders';
const port = 8080;
@@ -10,6 +13,6 @@ app.use(cors());
const app = express();
await loaders(app);
app.listen(port, () => {
console.log('listening on ' + port);
console.log('listening on port ' + port);
})
})();

View File

@@ -0,0 +1,5 @@
export class Recipe {
async getOneByID(id: string) {
}
}

View File

@@ -0,0 +1,46 @@
import { IUser } from "../schemas";
import { sql } from "@databases/pg";
import pgPromise from "pg-promise";
import db 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();
return result;
} catch (error) {
throw new Error(error);
}
}
// async getOneByID(id: string): Promise<Array<IUser | null>> {
// try {
// const statement = `SELECT * FROM users WHERE id = $1;`;
// const values = [id];
// const result = await db.query(statement, values);
// if (result.rows.length) return result.rows[0];
// return [];
// } catch (error) {
// throw new Error(error);
// }
// }
// async updateOneByID(id: string, data: IUser): Promise<IUser | null> {
// try {
// // does this formatting work?
// const condition = pgp.as.format('WHERE id = $1 RETURNING *', id);
// const statement = pgp.helpers.update(data, null, 'users') + condition;
// const result = await db.query(statement);
// if (result.rows.length) return result.rows[0];
// return null;
// } catch (error) {
// throw new Error(error);
// }
// }
}

1979
server/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -10,16 +10,30 @@
"author": "",
"license": "ISC",
"dependencies": {
"@databases/pg": "^5.4.1",
"bcrypt": "^5.1.0",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-session": "^1.17.3",
"helmet": "^6.0.0",
"http-errors": "^2.0.0",
"js-yaml": "^4.1.0",
"passport": "^0.6.0",
"passport-local": "^1.0.0",
"pg": "^8.8.0",
"pg-promise": "^10.15.0",
"swagger-ui-express": "^4.6.0"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.14",
"@types/http-errors": "^2.0.1",
"@types/js-yaml": "^4.0.5",
"@types/node": "^18.11.9",
"@types/pg": "^8.6.5",
"@types/pg-promise": "^5.4.3",
"@types/swagger-ui-express": "^4.1.3",
"nodemon": "^2.0.20",
"tslint": "^6.1.3",

View File

@@ -1,6 +1,11 @@
import { Express } from "express"
import { userRoute } from "./users";
export const routes = (app: Express, passport?: any) => {
console.log('routes called');
userRoute(app);
app.get('/hello', (req, res) => {
res.send({ message: "hello from the server!!" });
})

View File

@@ -0,0 +1,20 @@
import db, { sql } from '../db';
import { Express, Router } from 'express';
import UserCtl from '../controllers/UserCtl';
const router = Router();
const userCtl = new UserCtl();
export const userRoute = (app: Express) => {
app.use('/users', router);
// get all users
router.get('/', async (req, res, next) => {
await db.query(sql`SELECT * FROM users`).then(response => console.log(response));
// const data = userCtl.getAll();
// res.status(200).send(data);
})
router.get('/hidden-thing', (req, res) => {
res.send('does this route actually work?');
})
}

35
server/schemas/index.ts Normal file
View File

@@ -0,0 +1,35 @@
export interface IUser {
firstname: string
lastname: string
handle: string
email: string
password: string
}
export interface IRecipe {
name: string
description?: string
preptime: string
ingredients: IIngredient[]
removed: boolean
}
export interface IIngredient {
name: string
description?: string
active: boolean
}
export interface ICollection {
name: string
owner: IUser
active: boolean
default: boolean
}
export interface IGroceryList {
owner: IUser
listname: string
recipes?: IRecipe[]
active: boolean
}