2024-10-04 18:43:08 -04:00
|
|
|
const express = require('express');
|
|
|
|
const app = express.Router();
|
|
|
|
|
|
|
|
const sqlite = require('better-sqlite3');
|
2024-10-05 12:01:19 -04:00
|
|
|
const db = new sqlite('the_big_db.db', { verbose: console.log });
|
2024-10-04 18:43:08 -04:00
|
|
|
|
|
|
|
const { loginRequired } = require('../authStuff.js');
|
|
|
|
|
|
|
|
const showdown = require('showdown');
|
|
|
|
const converter = new showdown.Converter();
|
|
|
|
|
2024-10-21 11:56:48 -04:00
|
|
|
const hljs = require('highlight.js/lib/core');
|
|
|
|
hljs.registerLanguage('lua', require('highlight.js/lib/languages/lua'));
|
|
|
|
|
2024-10-05 17:38:48 -04:00
|
|
|
const pageListQuery = db.prepare(`
|
|
|
|
select p.number, p.title, p.time
|
|
|
|
from pages p
|
|
|
|
where time >= (
|
|
|
|
select max(s.time)
|
|
|
|
from pages s
|
|
|
|
where s.number = p.number
|
|
|
|
)
|
|
|
|
order by p.time desc
|
|
|
|
`);
|
2024-10-04 18:43:08 -04:00
|
|
|
app.get('/pages', (req, res) => {
|
|
|
|
try {
|
2024-10-05 17:38:48 -04:00
|
|
|
const pages = pageListQuery.all();
|
2024-10-04 18:43:08 -04:00
|
|
|
res.status(200).json(pages);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({"error": error});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/page/new', loginRequired, (req, res) => {
|
|
|
|
try {
|
2024-10-21 11:56:48 -04:00
|
|
|
let maxPage = db.prepare('select max(number) as maximum from pages').get()
|
|
|
|
let newPageNumber = maxPage.maximum + 1;
|
|
|
|
let newPage = db.prepare('insert into pages (number, title, description, author) values (?, ?, ?, ?) returning number')
|
2024-10-05 17:38:48 -04:00
|
|
|
.get(newPageNumber, "new page", "this page is new!", req.session.userId);
|
2024-10-21 11:56:48 -04:00
|
|
|
|
|
|
|
let newAttributes = db.prepare('insert into attributes (number, contents) values (?, ?)')
|
|
|
|
.run(newPageNumber, JSON.stringify({"parent": 3}));
|
2024-10-05 17:38:48 -04:00
|
|
|
res.status(200).json(newPageNumber);
|
2024-10-04 18:43:08 -04:00
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({"error": error});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-05 12:01:19 -04:00
|
|
|
app.get('/page/:number', (req, res) => {
|
2024-10-04 18:43:08 -04:00
|
|
|
try {
|
2024-10-05 12:01:19 -04:00
|
|
|
const page = db.prepare('select * from pages where number=:number order by time desc').get(req.params);
|
2024-10-04 18:43:08 -04:00
|
|
|
if (page === undefined) res.status(404).json({"error": "page not found"});
|
|
|
|
else res.status(200).json(page);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({"error": error});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-05 12:01:19 -04:00
|
|
|
app.post('/page/:number', loginRequired, (req, res) => {
|
2024-10-04 18:43:08 -04:00
|
|
|
try {
|
|
|
|
const html = converter.makeHtml(req.body.description);
|
2024-10-21 11:56:48 -04:00
|
|
|
const lua = hljs.highlight(req.body.description, {language: 'lua'}).value;
|
|
|
|
console.log(lua);
|
|
|
|
const changes = db.prepare('insert into pages (number, title, description, html, lua, author, type) values (?, ?, ?, ?, ?, ?, ?)')
|
|
|
|
.run(req.params.number, req.body.title, req.body.description, html, lua, req.session.userId, req.body.type ? 1 : 0);
|
2024-10-04 18:43:08 -04:00
|
|
|
res.status(200).json(changes);
|
|
|
|
} catch (error) {
|
2024-10-21 11:56:48 -04:00
|
|
|
console.log(error);
|
|
|
|
res.status(500).json({"error": error});
|
2024-10-04 18:43:08 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-06 16:01:13 -04:00
|
|
|
app.delete('/page/:number', loginRequired, (req, res) => {
|
2024-10-04 18:43:08 -04:00
|
|
|
try {
|
2024-10-21 11:56:48 -04:00
|
|
|
db.prepare('delete from pages where number = ?').run(req.params.number);
|
|
|
|
db.prepare('delete from attributes where number = ?').run(req.params.number);
|
2024-10-04 18:43:08 -04:00
|
|
|
res.status(200).json({id: req.params.id});
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({"error": error});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-05 12:01:19 -04:00
|
|
|
app.get('/page/:number/history', (req, res) => {
|
|
|
|
try {
|
2024-10-06 16:01:13 -04:00
|
|
|
const all = db.prepare('select * from pages where number=:number order by time desc').all(req.params);
|
2024-10-05 12:01:19 -04:00
|
|
|
res.status(200).json(all);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({"error": error});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
app.get('/page/:number/:id', (req, res) => {
|
|
|
|
try {
|
|
|
|
const page = db.prepare('select * from pages where number=:number and id=:id').get(req.params);
|
|
|
|
if (page === undefined) res.status(404).json({"error": "page not found"});
|
|
|
|
else res.status(200).json(page);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({"error": error});
|
|
|
|
}
|
|
|
|
});
|
2024-10-04 18:43:08 -04:00
|
|
|
|
|
|
|
module.exports = app;
|