aboutsummaryrefslogtreecommitdiff
path: root/routes/admin.js
blob: 260f34306773daefec40ffa2f33bb56f794a6d9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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");
});

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
  res.render("admin.html");
});

module.exports = router;