views and misc
This commit is contained in:
@ -1,32 +1,48 @@
|
||||
import express from "express";
|
||||
const router = express.Router();
|
||||
|
||||
import highlight from "highlight.js";
|
||||
import db from "../../lib/sql";
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
const query = "select p.uuid, p.stamp, p.title, p.paste, l.name as language, r.uuid as reply from `pastes` as p "
|
||||
+ "left join `languages` as l on l.id = p.lang "
|
||||
+ "left join `pastes` as r on r.id = p.reply "
|
||||
+ "order by p.id desc "
|
||||
+ "limit 5";
|
||||
db.exec(query)
|
||||
.then(rows => {
|
||||
const pastes = rows.map(r => {
|
||||
return {
|
||||
title: r.title,
|
||||
uuid: r.uuid,
|
||||
paste: highlight.highlightAuto(r.paste).value,
|
||||
language: r.language,
|
||||
desc: r.reply ? `Reply to <a href="/${r.reply}/" class="descID">/${r.reply}/</a>` : "No description.",
|
||||
rows: r.paste.split(/\r?\n/).length
|
||||
};
|
||||
});
|
||||
res.render("view", { pastes: pastes });
|
||||
})
|
||||
.catch(err => {
|
||||
res.render("view");
|
||||
});
|
||||
});
|
||||
const queries = {
|
||||
all: "select p.id, p.uuid, p.stamp, p.desc, p.paste, l.name as language, r.uuid as reply from `pastes` as p "
|
||||
+ "left join `languages` as l on l.id = p.lang "
|
||||
+ "left join `pastes` as r on r.id = p.reply "
|
||||
+ "order by p.id desc limit 5",
|
||||
single: "select p.id, p.uuid, p.stamp, p.desc, p.paste, l.name as language, r.uuid as reply from `pastes` as p "
|
||||
+ "left join `languages` as l on l.id = p.lang "
|
||||
+ "left join `pastes` as r on r.id = p.reply "
|
||||
+ "where p.uuid = ? limit 1"
|
||||
};
|
||||
|
||||
export default router;
|
||||
export default express.Router()
|
||||
.get("/", (req, res) => {
|
||||
db.exec(queries.all)
|
||||
.then(rows => {
|
||||
res.render("view", { pastes: rows.map(r => {
|
||||
const paste = new Buffer(r.paste, "base64").toString("ascii");
|
||||
return {
|
||||
uuid: r.uuid,
|
||||
paste: highlight.highlightAuto(paste).value,
|
||||
language: r.language,
|
||||
desc: r.reply ? `Reply to <a href="/${r.reply}/" class="descID">/${r.reply}/</a>` : "No description.",
|
||||
rows: paste.split(/\r?\n/).length
|
||||
};
|
||||
})});
|
||||
}).catch(err => res.render("view"));
|
||||
})
|
||||
.get("/:uuid", (req, res) => {
|
||||
if(req.params.uuid.length !== 8)
|
||||
return res.send("error");
|
||||
db.exec(queries.single, [req.params.uuid])
|
||||
.then(rows => {
|
||||
const r = rows[0];
|
||||
const paste = new Buffer(r.paste, "base64").toString("ascii");
|
||||
res.render("viewsingle", { paste: {
|
||||
uuid: r.uuid,
|
||||
paste: highlight.highlightAuto(paste).value,
|
||||
language: r.lang,
|
||||
desc: r.desc,
|
||||
rows: paste.split(/\r?\n/).length
|
||||
}});
|
||||
}).catch(err => res.send("paste not found"));
|
||||
});
|
Reference in New Issue
Block a user