an experiment in putting together a wiki and an object-oriented mud.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
the-forest/server/api.js

72 lines
2.3 KiB

const express = require('express');
const showdown = require('showdown');
const { query } = require('./dbHelper.js');
const { graphFromList } = require('./graphStuff.js');
const app = express.Router();
const converter = new showdown.Converter();
app.get('/pages', (req, res) => {
query('select id, title from pages', [])
.then((r) => r.rows)
.then((row) => row.map(([id, title]) => {return {"id": id, "title": title};}))
.then((pages) => res.status(200).json(pages))
.catch((error) => res.status(500).json({"error": error}));
});
app.post('/page/new', (req, res) => {
query('insert into pages (title, description) values (?, ?) returning id',
["new page", "this page is new!"])
.then((r) => r.rows[0])
.then((row) => res.status(200).json({id: row[0]}))
.catch((error) => res.status(500).json({"error": error}))
});
app.get('/page/:id', (req, res) => {
query('select * from pages where id=?', [req.params.id])
.then((r) => {
if (r.rows.length == 0) res.status(404).json({"error": "page not found in db"});
else return r.rows[0];
}).then((row) => res.status(200).json(row))
.catch((error) => res.status(500).json({"error": error}));
});
app.post('/page/:id', (req, res) => {
const html = converter.makeHtml(req.body.description);
query('replace into pages (id, title, description, html) values (?, ?, ?, ?)',
[req.params.id, req.body.title, req.body.description, html])
.then(() => res.status(200).json({}))
.catch((error) => res.status(500).json({"error": error}));
});
app.delete('/page/:id', (req, res) => {
query('delete from pages where id = ?', [req.params.id])
.then(() => res.status(200).json({id: req.params.id}))
.catch((error) => res.status(500).json({"error": error}))
})
app.get('/graph', (req, res) => {
query('select id, html from pages', [])
.then((r) => r.rows)
.then((stuff) => graphFromList(stuff))
.then((graph) => res.json(graph))
.catch((error) => res.status(500).json({"error": error}));
});
// auth stuff
app.post('/register', (req, res) => {
// parse the body, which miight be url-encoded?
// check if the nonce password is correctt
// hash the password
// add tthe user to the users database
});
app.post('/login', (req, res) => {
// hash the password
// check if the user/hashed pass matches
// if not, throw an error
// create a valid ttoken?
// return token
});
module.exports = app;