This commit is contained in:
Flummi 2018-02-20 10:16:06 +01:00
parent 9dc10d486b
commit 2f1169d95b
4 changed files with 32 additions and 17 deletions

View File

@ -6,7 +6,8 @@ const queries = {
getlangs: "select * from `languages` order by `name` asc", getlangs: "select * from `languages` order by `name` asc",
add: "insert into `pastes` (`uuid`,`lang`,`desc`,`paste`,`hidden`,`reply`) " add: "insert into `pastes` (`uuid`,`lang`,`desc`,`paste`,`hidden`,`reply`) "
+ "values (LEFT(UUID(), 8), ?, ?, ?, ?, ?)", + "values (LEFT(UUID(), 8), ?, ?, ?, ?, ?)",
getuuid: "select `uuid` from `pastes` where `id` = ? limit 1" getuuid: "select `uuid` from `pastes` where `id` = ? limit 1",
getid: "select `id` from `pastes` where `uuid` = ? limit 1"
}; };
let languages = new Map(); let languages = new Map();
db.exec(queries.getlangs) db.exec(queries.getlangs)
@ -14,9 +15,9 @@ db.exec(queries.getlangs)
.catch(err => console.log(err)); .catch(err => console.log(err));
export default express.Router() export default express.Router()
.get("/", (req, res) => res.render("index", { languages: [...languages.values()] })) .get(["/", "/r/:uuid"], (req, res) => res.render("index", { languages: [...languages.values()], reply: req.params.uuid || false }))
.post("/", (req, res) => { .post("/", (req, res) => {
const data = { let data = {
desc: req.body.desc || "", desc: req.body.desc || "",
content: req.body.content || false, content: req.body.content || false,
lang: languages.getKey(req.body.lang) || 1, lang: languages.getKey(req.body.lang) || 1,
@ -25,6 +26,20 @@ export default express.Router()
}; };
if(!data.content) if(!data.content)
return res.render("index", { languages: [...languages.values()], notification: "nope" }); return res.render("index", { languages: [...languages.values()], notification: "nope" });
if(data.reply.length === 8) {
db.exec(queries.getid, [data.reply]).then(rows => {
data.reply = rows.length === 1 ? rows[0].id : 0;
savePaste(data, res);
}).catch(err => console.log(err));
}
else {
data.reply = 0;
savePaste(data, res);
}
});
const savePaste = (data, res) => {
db.exec(queries.add, [ db.exec(queries.add, [
data.lang, data.lang,
data.desc, data.desc,
@ -35,7 +50,7 @@ export default express.Router()
.then(rows => res.redirect(`/${rows[0].uuid}/`)) .then(rows => res.redirect(`/${rows[0].uuid}/`))
.catch(err => res.render("index", { languages: [...languages.values()], notification: "lol" })) .catch(err => res.render("index", { languages: [...languages.values()], notification: "lol" }))
).catch(err => res.render("index", { languages: [...languages.values()], notification: "didn't insert your paste lol" })); ).catch(err => res.render("index", { languages: [...languages.values()], notification: "didn't insert your paste lol" }));
}); };
Map.prototype.getKey = function(val) { Map.prototype.getKey = function(val) {
if(!val) if(!val)

View File

@ -1,4 +1,4 @@
<h1>New paste</h1> <h1>New paste{{#if reply}} (reply to <a href="/v/{{reply}}">{{reply}}</a>){{/if}}</h1>
<form method="post" action="/"> <form method="post" action="/">
<div class="settings"><p>Description:</p><input id="desc" type="text" name="desc" value=""></div> <div class="settings"><p>Description:</p><input id="desc" type="text" name="desc" value=""></div>
<textarea id="content" name="content"></textarea> <textarea id="content" name="content"></textarea>
@ -8,6 +8,6 @@
</select></li> </select></li>
<li><p>Hide from recent pastes:</p><input type="checkbox" id="hidden" name="hidden"></li> <li><p>Hide from recent pastes:</p><input type="checkbox" id="hidden" name="hidden"></li>
</ul> </ul>
<input id="email" type="text" name="email"> <input id="reply" type="hidden" name="reply" value="{{reply}}">
<input type="submit" value="Add paste"> <input type="submit" value="Add paste">
</form> </form>

View File

@ -2,7 +2,7 @@
<div class="pasteInfo"> <div class="pasteInfo">
<p class="timestamp"><time title="{{stamp}}"></time></p> <p class="timestamp"><time title="{{stamp}}"></time></p>
<p class="language"><a href="/v/lang/{{language}}">{{language}}</a></p> <p class="language"><a href="/v/lang/{{language}}">{{language}}</a></p>
<p>Paste: <a href="/{{uuid}}/">/{{uuid}}/</a><a href="/v/id/{{uuid}}">Show related</a></p> <p>Paste: <a href="/{{uuid}}/">/{{uuid}}/</a><a href="/r/{{uuid}}">Reply</a></p>
<p class="desc">{{{desc}}}</p> <p class="desc">{{{desc}}}</p>
</div> </div>
<div class="formatedCode"> <div class="formatedCode">

View File

@ -1,7 +1,7 @@
<div class="pasteInfo"> <div class="pasteInfo">
<p class="timestamp"><time title="{{paste.stamp}}"></time></p> <p class="timestamp"><time title="{{paste.stamp}}"></time></p>
<p class="language"><a href="/v/lang/{{paste.language}}">{{paste.language}}</a></p> <p class="language"><a href="/v/lang/{{paste.language}}">{{paste.language}}</a></p>
<p>Paste: <a href="/{{paste.uuid}}/">/{{paste.uuid}}/</a><a href="/v/id/{{paste.uuid}}">Show related</a></p> <p>Paste: <a href="/{{paste.uuid}}/">/{{paste.uuid}}/</a><a href="/r/{{paste.uuid}}">Reply</a></p>
<p class="desc">{{{paste.desc}}}</p> <p class="desc">{{{paste.desc}}}</p>
</div> </div>
<div class="formatedCode"> <div class="formatedCode">