mirror of
https://github.com/okiba-org/backend.git
synced 2025-02-22 17:22:02 +00:00
feat: get paste route
This commit is contained in:
parent
f520d16f19
commit
656c95519d
@ -1,16 +1,29 @@
|
|||||||
import { Pool, QueryResult } from "pg";
|
import { Pool, QueryResult } from "pg";
|
||||||
import { Word } from "../utils/types";
|
import { Word } from "../utils/types";
|
||||||
import { Query } from "./queries";
|
|
||||||
|
|
||||||
// query wrappers
|
// query wrappers
|
||||||
export const getAvailableWord = async (db: Pool): Promise<Word | undefined> => {
|
export const getAvailableWord = async (db: Pool): Promise<Word | undefined> => {
|
||||||
|
let query =
|
||||||
|
"SELECT * FROM words WHERE taken = 'f' ORDER BY random() LIMIT 1;";
|
||||||
const data: void | QueryResult<Word> = await db
|
const data: void | QueryResult<Word> = await db
|
||||||
.query(Query.getAvailableWord)
|
.query(query)
|
||||||
.catch((err) => err);
|
.catch((err) => err);
|
||||||
|
|
||||||
return data?.rows[0];
|
return data?.rows[0];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const setWordTaken = async (db: Pool, id: number) => {
|
export const setWordTaken = async (db: Pool, id: number) => {
|
||||||
await db.query(Query.setWordTaken(id)).catch((err) => err);
|
let query = `UPDATE words SET taken = 't' WHERE id = ${id}`;
|
||||||
|
await db.query(query).catch((err) => err);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getWordFromVal = async (
|
||||||
|
db: Pool,
|
||||||
|
id: string
|
||||||
|
): Promise<Word | undefined> => {
|
||||||
|
let query = `SELECT * FROM words WHERE val = '${id}';`;
|
||||||
|
let results: QueryResult<Word> | void = await db
|
||||||
|
.query(query)
|
||||||
|
.catch((err) => err);
|
||||||
|
return results?.rows[0];
|
||||||
};
|
};
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
// query strings
|
|
||||||
export const Query = {
|
|
||||||
getAvailableWord:
|
|
||||||
"SELECT * FROM words WHERE taken = 'f' ORDER BY random() LIMIT 1;",
|
|
||||||
|
|
||||||
setWordTaken(id: number) {
|
|
||||||
return `UPDATE words SET taken = 't' WHERE id = ${id}`;
|
|
||||||
},
|
|
||||||
};
|
|
@ -1,11 +1,11 @@
|
|||||||
import { Router, Request, Response, NextFunction } from "express";
|
import { Router, Request, Response, NextFunction } from "express";
|
||||||
import { Pool, QueryResult } from "pg";
|
import { Pool, QueryResult } from "pg";
|
||||||
import fs from "fs";
|
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { Word } from "../utils/types";
|
import { Word } from "../utils/types";
|
||||||
import fsp from "fs/promises";
|
import fsp from "fs/promises";
|
||||||
import { projectRoot } from "../utils";
|
import { getPastePath, projectRoot } from "../utils";
|
||||||
import { getAvailableWord, setWordTaken } from "../db/bin";
|
import { getAvailableWord, getWordFromVal, setWordTaken } from "../db/bin";
|
||||||
|
import { getPackedSettings } from "http2";
|
||||||
|
|
||||||
export default function BinRouter(db: Pool) {
|
export default function BinRouter(db: Pool) {
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
@ -35,5 +35,31 @@ export default function BinRouter(db: Pool) {
|
|||||||
res.status(500).json({ message: "Something went wrong" });
|
res.status(500).json({ message: "Something went wrong" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get("/paste/:id", async (req: Request, res: Response) => {
|
||||||
|
const id: string = req.params.id;
|
||||||
|
|
||||||
|
if (id == undefined || id == "") {
|
||||||
|
return res.status(400).json({ message: "Invalid paste id!" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify word
|
||||||
|
let word = await getWordFromVal(db, id);
|
||||||
|
if (word == undefined || !word.taken) {
|
||||||
|
return res.status(404).json({ message: "Paste not found!" });
|
||||||
|
}
|
||||||
|
|
||||||
|
// send paste
|
||||||
|
const paste = await fsp
|
||||||
|
.readFile(getPastePath(word.val), {
|
||||||
|
encoding: "utf-8",
|
||||||
|
})
|
||||||
|
.catch((err: Error) => {
|
||||||
|
throw err;
|
||||||
|
});
|
||||||
|
|
||||||
|
res.set("Content-type", "text/plain");
|
||||||
|
return res.status(200).send(paste);
|
||||||
|
});
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,11 @@ export const logWarning = (msg: string) => console.log(chalk.bold.yellow(msg));
|
|||||||
|
|
||||||
export const projectRoot = path.join(__dirname, "..", "..");
|
export const projectRoot = path.join(__dirname, "..", "..");
|
||||||
|
|
||||||
|
// generate paste path string
|
||||||
|
export const getPastePath = (word: string): string => {
|
||||||
|
return path.join(projectRoot, "data", word + ".txt");
|
||||||
|
};
|
||||||
|
|
||||||
export const errorHandler: ErrorRequestHandler = (err, _, res, __) => {
|
export const errorHandler: ErrorRequestHandler = (err, _, res, __) => {
|
||||||
logError(err);
|
logError(err);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user