diff options
Diffstat (limited to 'routes')
| -rw-r--r-- | routes/admin.js | 31 | ||||
| -rw-r--r-- | routes/blog.js | 49 | ||||
| -rw-r--r-- | routes/index.js | 3 | ||||
| -rw-r--r-- | routes/posts.js | 1 |
4 files changed, 74 insertions, 10 deletions
diff --git a/routes/admin.js b/routes/admin.js index b6afab7..260f343 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -1,5 +1,8 @@ const express = require("express"); const router = express.Router(); +const cheerio = require("cheerio"); +const fs = require("fs"); +const firebase = require("../firebase"); router.get("/dashboard", function (req, res) { res.render("dashboard.html"); @@ -7,7 +10,33 @@ router.get("/dashboard", function (req, res) { router.get("/dashboard/new", function (req, res) { res.render("createPost.html"); -}) +}); + +router.get("/dashboard/edit/:slug", function (req, res) { + var html = fs.readFileSync( + __dirname + "/../public/views/editPost.html", + "utf8" + ); + var $ = cheerio.load(html); + const store = firebase.firestore(); + let query = store.collection("posts"); + query = query.where("slug", "==", req.params.slug); + query + .get() + .then(function (querySnapshot) { + querySnapshot.forEach(function (doc) { + $("#title").val(doc.data().title); + $("#content").val(Buffer.from(doc.data().content, "base64").toString()); + $("#tags").val(doc.data().tags); + $("#publishDate").val(doc.data().publishDate); + }); + }) + .then(() => { + const publishScript = `<script src="/static/assets/js/pages/publish.js"></script>`; + $("body").append(publishScript); + res.send($.html()); + }); +}); router.get("/", (req, res) => { // Send admin.html from public folder diff --git a/routes/blog.js b/routes/blog.js index fbdc1f2..8a8cb19 100644 --- a/routes/blog.js +++ b/routes/blog.js @@ -7,13 +7,50 @@ router.get("/posts", (req, res) => { const posts = []; let query = store.collection("posts"); query = query.select("slug", "tags", "title", "shortText", "publishDate"); - query.get().then(function (querySnapshot) { - querySnapshot.forEach(function (doc) { - posts.push(doc.data()); + query + .get() + .then(function (querySnapshot) { + querySnapshot.forEach(function (doc) { + posts.push(doc.data()); + }); + }) + .then(() => { + res.json(posts); + }); +}); + +router.put("/update/:slug", (req, res) => { + const store = firebase.firestore(); + const { title, content, tags, publishDate, shortText, slug } = req.body; + const base64 = Buffer.from(content).toString("base64"); + const post = { + title, + content: base64, + tags: String(tags).split(",").length > 0 ? String(tags).split(",") : [], + publishDate, + shortText, + slug, + }; + let query = store.collection("posts"); + query = query.where("slug", "==", slug); + query + .get() + .then(function (querySnapshot) { + querySnapshot.forEach(function (doc) { + doc.ref.update({ + title: post.title, + content: post.content, + tags: post.tags, + publishDate: post.publishDate, + }); + }); + }) + .then(() => { + res.json({ success: true }); + }) + .catch((err) => { + res.json({ success: false, err }); }); - }).then(() => { - res.json(posts); - }); }); router.post("/new", (req, res) => { diff --git a/routes/index.js b/routes/index.js index 659d50d..605f6ad 100644 --- a/routes/index.js +++ b/routes/index.js @@ -13,8 +13,8 @@ const home = require("./home"); router.use("/admin", admin); router.use("/api/blog", blog); router.use("/", repositories); -router.use("/", posts); router.use("/", home); +router.use("/", posts); // Create the routes @@ -22,6 +22,5 @@ router.get("/about", (req, res) => { res.render("about.html"); }); - // Export the routes module.exports = router; diff --git a/routes/posts.js b/routes/posts.js index bc22670..00b092f 100644 --- a/routes/posts.js +++ b/routes/posts.js @@ -6,7 +6,6 @@ const router = express.Router(); const marked = require("marked"); const hljs = require("highlight.js"); - router.get("/posts/:id", function (req, res) { const id = req.params.id; // get single document where slug === id |
