From 899ffa17991910f190744393e44479780a7d00d0 Mon Sep 17 00:00:00 2001 From: alok8bb Date: Fri, 8 Jul 2022 11:40:55 +0530 Subject: [PATCH] refactor: fix missing await and bad practices --- .env | 5 ----- .gitignore | 4 ++-- src/db/bin.ts | 7 +----- src/main.ts | 57 ++++++++++++++++++++++++++--------------------- src/routes/bin.ts | 7 ++++-- 5 files changed, 39 insertions(+), 41 deletions(-) delete mode 100644 .env diff --git a/.env b/.env deleted file mode 100644 index a9403d6..0000000 --- a/.env +++ /dev/null @@ -1,5 +0,0 @@ -PORT = 8080 -PGUSER=alok-pg -PGHOST=localhost -PGDATABASE=okiba -PGPORT=5432 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 5b9f813..a39c0ba 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ +.env node_modules/ public/ test/ -data/ -.env \ No newline at end of file +data/ \ No newline at end of file diff --git a/src/db/bin.ts b/src/db/bin.ts index 1f7a885..fb0f839 100644 --- a/src/db/bin.ts +++ b/src/db/bin.ts @@ -8,12 +8,7 @@ export const getAvailableWord = async (db: Pool): Promise => { .query(Query.getAvailableWord) .catch((err) => err); - if (data != undefined) { - let word = data.rows[0]; - return word; - } - - return undefined; + return data?.rows[0]; }; export const setWordTaken = async (db: Pool, id: number) => { diff --git a/src/main.ts b/src/main.ts index 867ce0e..96e3c0c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,34 +4,39 @@ import BinRouter from "./routes/bin"; import { Client, Pool } from "pg"; import { createDataDir, errorHandler, logError, logSuccess } from "./utils"; -dotenv.config(); -createDataDir(); +const main = async () => { + dotenv.config(); + createDataDir(); -// init db -const pool = new Pool(); -const client = new Client(); + // init db + const pool = await new Pool(); + const client = await new Client(); -client - .connect() - .then(() => { - logSuccess("Connected to database!"); - }) - .catch((err: Error) => { - logError(`Could not connect to database! \n\n${err.message}`); + client + .connect() + .then(() => { + logSuccess("Connected to database!"); + }) + .catch((err: Error) => { + logError(`Could not connect to database! \n\n${err.message}`); + throw err; + }); + + // start server + const app: Express = express(); + const port = process.env.PORT; + + app.use(express.text()); + + app.get("/", (req: Request, res: Response) => { + res.send("Hello, World!"); }); + app.use("/bin/", BinRouter(pool)); -// start server -const app: Express = express(); -const port = process.env.PORT; + app.use(errorHandler); + app.listen(port, () => { + logSuccess(`Server started on port ${port}`); + }); +}; -app.use(express.text()); - -app.get("/", (req: Request, res: Response) => { - res.send("Hello, World!"); -}); -app.use("/bin/", BinRouter(pool)); - -app.use(errorHandler); -app.listen(port, () => { - logSuccess(`Server started on port ${port}`); -}); +main(); diff --git a/src/routes/bin.ts b/src/routes/bin.ts index 89dad87..2ad8bab 100644 --- a/src/routes/bin.ts +++ b/src/routes/bin.ts @@ -11,6 +11,9 @@ export default function BinRouter(db: Pool) { router.post("/paste", async (req: Request, res: Response) => { let body: string = req.body; + if (body == undefined || body == "") { + return res.status(400).json({ message: "Invalid body!" }); + } const word: Word | undefined = await getAvailableWord(db); @@ -21,8 +24,8 @@ export default function BinRouter(db: Pool) { (err) => err ); - setWordTaken(db, word.id); - console.log(word.id); + await setWordTaken(db, word.id); + return res.status(200).json({ endpoint: word.val, message: "Code pasted successfully!",