diff --git a/module/module-pastebin/handler.js b/module/module-pastebin/handler.js new file mode 100644 index 00000000..a6c838ea --- /dev/null +++ b/module/module-pastebin/handler.js @@ -0,0 +1,42 @@ +const { Route, Handler } = global.Hydro.service.server; +const { PERM_LOGGEDIN } = global.Hydro.permission; +const { pastebin } = global.Hydro.model; +const { nav } = global.Hydro.lib; + +class PasteShowHandler extends Handler { + async get({ docId }) { + const doc = await pastebin.get(docId); + this.response.body = { doc }; + this.response.template = 'paste_show.html'; + } +} + +class PasteCreateHandler extends Handler { + async prepare() { + this.checkPerm(PERM_LOGGEDIN); + } + + async get() { + this.response.template = 'paste_create.html'; + } + + async post({ + language, expire, password, title, content, + }) { + const docId = await pastebin.add({ + language, expire, password, owner: this.user._id, title, content, + }); + this.response.body = { docId }; + this.response.redirect = `/paste/${docId}`; + } +} + +async function apply() { + Route('/paste/create', module.exports.PasteCreateHandler); + Route('/paste/:docId', module.exports.PasteShowHandler); + nav('/paste/create', 'pastebin', 'pastebin', PERM_LOGGEDIN); +} + +global.Hydro.handler.paste = module.exports = { + PasteCreateHandler, PasteShowHandler, apply, +}; diff --git a/module/module-pastebin/hydro.json b/module/module-pastebin/hydro.json new file mode 100644 index 00000000..f16925e6 --- /dev/null +++ b/module/module-pastebin/hydro.json @@ -0,0 +1,4 @@ +{ + "name": "module-pastebin", + "version": "1.0.0" +} \ No newline at end of file diff --git a/module/module-pastebin/model.js b/module/module-pastebin/model.js new file mode 100644 index 00000000..4738ad99 --- /dev/null +++ b/module/module-pastebin/model.js @@ -0,0 +1,26 @@ +const { DocumentNotFoundError } = global.Hydro.error; +const { db } = global.Hydro.service; + +const coll = db.collection('pastebin'); + +async function add({ + owner, language, expire, password, title, content, +}) { + const doc = { + owner, password, expire, language, title, content, + }; + const res = await coll.insertOne(doc); + return res.insertedId; +} + +async function get(_id) { + const doc = await coll.findOne({ _id }); + if (!doc) throw new DocumentNotFoundError(_id); + return doc; +} + +function del(_id) { + return coll.deleteOne({ _id }); +} + +global.Hydro.model.pastebin = module.exports = { add, get, del }; diff --git a/module/module-pastebin/template/paste_create.html b/module/module-pastebin/template/paste_create.html new file mode 100644 index 00000000..482d4df1 --- /dev/null +++ b/module/module-pastebin/template/paste_create.html @@ -0,0 +1,54 @@ +{% set page_name = "paste_create" %} +{% extends "layout/basic.html" %} +{% block content %} +
+
+
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+ +
+
+ + +
+
+
+
+
+
+ {% include 'components/md_hint.html' %} +
+
+{% endblock %} diff --git a/module/module-pastebin/template/paste_show.html b/module/module-pastebin/template/paste_show.html new file mode 100644 index 00000000..25d05fd0 --- /dev/null +++ b/module/module-pastebin/template/paste_show.html @@ -0,0 +1,48 @@ +{% set page_name = "paste_create" %} +{% extends "layout/basic.html" %} +{% block content %} +
+
+
+
+
+
+
+ +
+
+ +
+
+ +
+
+
+ +
+
+
+
+
+
+ {% include 'components/md_hint.html' %} +
+
+{% endblock %}