mirror of
https://github.com/okiba-org/backend.git
synced 2025-04-20 01:16:58 +00:00
refactor: fix missing await and bad practices
This commit is contained in:
parent
e1c79a1840
commit
899ffa1799
5
.env
5
.env
@ -1,5 +0,0 @@
|
||||
PORT = 8080
|
||||
PGUSER=alok-pg
|
||||
PGHOST=localhost
|
||||
PGDATABASE=okiba
|
||||
PGPORT=5432
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,5 +1,5 @@
|
||||
.env
|
||||
node_modules/
|
||||
public/
|
||||
test/
|
||||
data/
|
||||
.env
|
@ -8,12 +8,7 @@ export const getAvailableWord = async (db: Pool): Promise<Word | undefined> => {
|
||||
.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) => {
|
||||
|
37
src/main.ts
37
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
|
||||
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;
|
||||
// start server
|
||||
const app: Express = express();
|
||||
const port = process.env.PORT;
|
||||
|
||||
app.use(express.text());
|
||||
app.use(express.text());
|
||||
|
||||
app.get("/", (req: Request, res: Response) => {
|
||||
app.get("/", (req: Request, res: Response) => {
|
||||
res.send("Hello, World!");
|
||||
});
|
||||
app.use("/bin/", BinRouter(pool));
|
||||
});
|
||||
app.use("/bin/", BinRouter(pool));
|
||||
|
||||
app.use(errorHandler);
|
||||
app.listen(port, () => {
|
||||
app.use(errorHandler);
|
||||
app.listen(port, () => {
|
||||
logSuccess(`Server started on port ${port}`);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
main();
|
||||
|
@ -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!",
|
||||
|
Loading…
x
Reference in New Issue
Block a user