api side of edit histories mostly done

This commit is contained in:
Shoofle 2024-10-05 12:01:19 -04:00
parent 5a7021b674
commit 6355e86777
11 changed files with 93 additions and 466 deletions

View File

@ -35,10 +35,7 @@ export async function postNewPage() {
}
export async function fetchPageList() {
return fetch(`${apiUrl}/pages`, {
method: 'GET',
...defaults
}).then((res) => res.json())
return shoofetch(`${apiUrl}/pages`, {method: 'GET'});
}
export async function fetchPage(id) {

View File

@ -14,9 +14,9 @@ export default function PageList({pages, ...props}) {
else if (error) outputContents = `Error encountered: ${error}`;
else outputContents = (
<ol {...props}>
{ data.map(({id, title}) =>
<li key={id} >
<a href={`/${id}`} {...noLoad} >{title}</a>
{ data.map(({number, title}) =>
<li key={number} >
<a href={`/${number}`} {...noLoad} >{title}</a>
</li>
)}
</ol>

View File

@ -16,9 +16,9 @@ function Profile() {
retry: 1
});
//useEffect(() => { if (!loggedIn) navigate('/'); }, [loggedIn]);
useEffect(() => { if (!loggedIn) navigate('/'); }, [loggedIn]);
const { name, favoriteColor, leastFavoriteColor } = (data || {});
const { id, name, favoriteColor, leastFavoriteColor } = (data || {});
return (
<div className="main-column">
@ -27,6 +27,10 @@ function Profile() {
<p>there {isError ? "is" : "isn't"} an error</p>
<p>error is {error?.message}</p>
<form onSubmit={handleSubmit(() => {})}>
<label>
Id:
<input {...register("id")} value={id} disabled/>
</label>
<label>
User name:
<input {...register("name")} value={name}/>

View File

@ -1,58 +0,0 @@
const libsql = require("@libsql/client");
const showdown = require("showdown");
const createPages = `
create table if not exists pages (
id integer primary key,
title varchar(255),
description text,
html text
)
`;
const createUsers = `
create table if not exists users (
name varchar(64) primary key,
password varchar(128)
)
`;
const createTokens = `
create table if not exists tokens (
)
`;
async function initDb() {
const config = {
url: "http://127.0.0.1:8000"
};
const db = libsql.createClient(config);
const converter = new showdown.Converter();
console.log("creating page table")
console.log(await db.execute(createPages));
console.log("finding pages that havent been rendered");
const rows = (await db.execute(`select * from pages where html is null`)).rows;
console.log(rows);
rows.forEach(async ({id, title, description, html}) => {
console.log(`rendering page number ${id}`)
console.log(`${id}. ${title}: ${description} ( ${html} )`);
const renderedPage = converter.makeHtml(description);
await db.execute({
sql: `replace into pages (id, title, description, html) values (?, ?, ?, ?)`,
args: [id, title, description, renderedPage]
});
});
console.log("creating user table");
console.log(await db.execute(createUsers));
}
initDb();

348
db/package-lock.json generated
View File

@ -1,348 +0,0 @@
{
"name": "forest-db",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "forest-db",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"@libsql/client": "^0.12.0",
"showdown": "^2.1.0"
}
},
"node_modules/@libsql/client": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/@libsql/client/-/client-0.12.0.tgz",
"integrity": "sha512-Jt9ijfzlmswZzxRRksEv8Igbw7ER9IX/RSrwzEIRyzwwQLcpeZGGrnDQP9S7UPCJ90uFDiaICFiyhvwO+60DmA==",
"dependencies": {
"@libsql/core": "^0.12.0",
"@libsql/hrana-client": "^0.7.0",
"js-base64": "^3.7.5",
"libsql": "^0.4.4",
"promise-limit": "^2.7.0"
}
},
"node_modules/@libsql/core": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/@libsql/core/-/core-0.12.0.tgz",
"integrity": "sha512-PZJF4qvPbDWZfXk0Tr+O5xaMhFoetA1gIVPgYLH6Bc6gAeyYxhGw268p+95SkADZDjB5CYPcVLvfQEjSVeR3Zw==",
"dependencies": {
"js-base64": "^3.7.5"
}
},
"node_modules/@libsql/darwin-arm64": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/@libsql/darwin-arm64/-/darwin-arm64-0.4.5.tgz",
"integrity": "sha512-xLdnn0NrgSk6OMi716FFs/27Hs33jtSd2fkKi/72Ey/qBtPWcB1BMurDQekzi0yAcfQTjGqIz7tpOibyjiEPyQ==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"darwin"
]
},
"node_modules/@libsql/darwin-x64": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/@libsql/darwin-x64/-/darwin-x64-0.4.5.tgz",
"integrity": "sha512-rZsEWj0H7oCqd5Y2pe0RzKmuQXC2OB1RbnFy4CvjeAjT6MP6mFp+Vx9mTCAUuJMhuoSVMsFPUJRpAQznl9E3Tg==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"darwin"
]
},
"node_modules/@libsql/hrana-client": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/@libsql/hrana-client/-/hrana-client-0.7.0.tgz",
"integrity": "sha512-OF8fFQSkbL7vJY9rfuegK1R7sPgQ6kFMkDamiEccNUvieQ+3urzfDFI616oPl8V7T9zRmnTkSjMOImYCAVRVuw==",
"dependencies": {
"@libsql/isomorphic-fetch": "^0.3.1",
"@libsql/isomorphic-ws": "^0.1.5",
"js-base64": "^3.7.5",
"node-fetch": "^3.3.2"
}
},
"node_modules/@libsql/isomorphic-fetch": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/@libsql/isomorphic-fetch/-/isomorphic-fetch-0.3.1.tgz",
"integrity": "sha512-6kK3SUK5Uu56zPq/Las620n5aS9xJq+jMBcNSOmjhNf/MUvdyji4vrMTqD7ptY7/4/CAVEAYDeotUz60LNQHtw==",
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@libsql/isomorphic-ws": {
"version": "0.1.5",
"resolved": "https://registry.npmjs.org/@libsql/isomorphic-ws/-/isomorphic-ws-0.1.5.tgz",
"integrity": "sha512-DtLWIH29onUYR00i0GlQ3UdcTRC6EP4u9w/h9LxpUZJWRMARk6dQwZ6Jkd+QdwVpuAOrdxt18v0K2uIYR3fwFg==",
"dependencies": {
"@types/ws": "^8.5.4",
"ws": "^8.13.0"
}
},
"node_modules/@libsql/linux-arm64-gnu": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/@libsql/linux-arm64-gnu/-/linux-arm64-gnu-0.4.5.tgz",
"integrity": "sha512-VR09iu6KWGJ6fauCn59u/jJ9OA+/A2yQ0dr2HDN2zkRueLC6D2oGYt4gPfLZPFKf+WJpVMtIhNfd+Ru9MMaFkA==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"linux"
]
},
"node_modules/@libsql/linux-arm64-musl": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/@libsql/linux-arm64-musl/-/linux-arm64-musl-0.4.5.tgz",
"integrity": "sha512-74hvD5ej4rBshhxFGNYU16a3m8B/NjIPvhlZ/flG1Oeydfo6AuUXSSNFi+H5+zi9/uWuzyz5TLVeQcraoUV10A==",
"cpu": [
"arm64"
],
"optional": true,
"os": [
"linux"
]
},
"node_modules/@libsql/linux-x64-gnu": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/@libsql/linux-x64-gnu/-/linux-x64-gnu-0.4.5.tgz",
"integrity": "sha512-gb5WObGO3+rbuG8h9font1N02iF+zgYAgY0wNa8BNiZ5A9UolZKFxiqGFS7eHaAYfemHJKKTT+aAt3X2p5TibA==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"linux"
]
},
"node_modules/@libsql/linux-x64-musl": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/@libsql/linux-x64-musl/-/linux-x64-musl-0.4.5.tgz",
"integrity": "sha512-JfyE6OVC5X4Nr4cFF77VhB1o+hBRxAqYT9YdeqnWdAQSYc/ASi5HnRALLAQEsGacFPZZ32pixfraQmPE3iJFfw==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"linux"
]
},
"node_modules/@libsql/win32-x64-msvc": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/@libsql/win32-x64-msvc/-/win32-x64-msvc-0.4.5.tgz",
"integrity": "sha512-57GGurNJhOhq3XIopLdGnCoQ4kQAcmbmzzFoC4tpvDE/KSbwZ/13zqJWhQA41nMGk/PKM1XKfKmbIybKx1+eqA==",
"cpu": [
"x64"
],
"optional": true,
"os": [
"win32"
]
},
"node_modules/@neon-rs/load": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/@neon-rs/load/-/load-0.0.4.tgz",
"integrity": "sha512-kTPhdZyTQxB+2wpiRcFWrDcejc4JI6tkPuS7UZCG4l6Zvc5kU/gGQ/ozvHTh1XR5tS+UlfAfGuPajjzQjCiHCw=="
},
"node_modules/@types/node": {
"version": "22.5.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.5.5.tgz",
"integrity": "sha512-Xjs4y5UPO/CLdzpgR6GirZJx36yScjh73+2NlLlkFRSoQN8B0DpfXPdZGnvVmLRLOsqDpOfTNv7D9trgGhmOIA==",
"dependencies": {
"undici-types": "~6.19.2"
}
},
"node_modules/@types/ws": {
"version": "8.5.12",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.12.tgz",
"integrity": "sha512-3tPRkv1EtkDpzlgyKyI8pGsGZAGPEaXeu0DOj5DI25Ja91bdAYddYHbADRYVrZMRbfW+1l5YwXVDKohDJNQxkQ==",
"dependencies": {
"@types/node": "*"
}
},
"node_modules/commander": {
"version": "9.5.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-9.5.0.tgz",
"integrity": "sha512-KRs7WVDKg86PWiuAqhDrAQnTXZKraVcCc6vFdL14qrZ/DcWwuRo7VoiYXalXO7S5GKpqYiVEwCbgFDfxNHKJBQ==",
"engines": {
"node": "^12.20.0 || >=14"
}
},
"node_modules/data-uri-to-buffer": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
"integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
"engines": {
"node": ">= 12"
}
},
"node_modules/detect-libc": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz",
"integrity": "sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==",
"engines": {
"node": ">=8"
}
},
"node_modules/fetch-blob": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
"integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/jimmywarting"
},
{
"type": "paypal",
"url": "https://paypal.me/jimmywarting"
}
],
"dependencies": {
"node-domexception": "^1.0.0",
"web-streams-polyfill": "^3.0.3"
},
"engines": {
"node": "^12.20 || >= 14.13"
}
},
"node_modules/formdata-polyfill": {
"version": "4.0.10",
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
"integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
"dependencies": {
"fetch-blob": "^3.1.2"
},
"engines": {
"node": ">=12.20.0"
}
},
"node_modules/js-base64": {
"version": "3.7.7",
"resolved": "https://registry.npmjs.org/js-base64/-/js-base64-3.7.7.tgz",
"integrity": "sha512-7rCnleh0z2CkXhH67J8K1Ytz0b2Y+yxTPL+/KOJoa20hfnVQ/3/T6W/KflYI4bRHRagNeXeU2bkNGI3v1oS/lw=="
},
"node_modules/libsql": {
"version": "0.4.5",
"resolved": "https://registry.npmjs.org/libsql/-/libsql-0.4.5.tgz",
"integrity": "sha512-sorTJV6PNt94Wap27Sai5gtVLIea4Otb2LUiAUyr3p6BPOScGMKGt5F1b5X/XgkNtcsDKeX5qfeBDj+PdShclQ==",
"cpu": [
"x64",
"arm64",
"wasm32"
],
"os": [
"darwin",
"linux",
"win32"
],
"dependencies": {
"@neon-rs/load": "^0.0.4",
"detect-libc": "2.0.2"
},
"optionalDependencies": {
"@libsql/darwin-arm64": "0.4.5",
"@libsql/darwin-x64": "0.4.5",
"@libsql/linux-arm64-gnu": "0.4.5",
"@libsql/linux-arm64-musl": "0.4.5",
"@libsql/linux-x64-gnu": "0.4.5",
"@libsql/linux-x64-musl": "0.4.5",
"@libsql/win32-x64-msvc": "0.4.5"
}
},
"node_modules/node-domexception": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
"integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/jimmywarting"
},
{
"type": "github",
"url": "https://paypal.me/jimmywarting"
}
],
"engines": {
"node": ">=10.5.0"
}
},
"node_modules/node-fetch": {
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
"integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
"dependencies": {
"data-uri-to-buffer": "^4.0.0",
"fetch-blob": "^3.1.4",
"formdata-polyfill": "^4.0.10"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/node-fetch"
}
},
"node_modules/promise-limit": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/promise-limit/-/promise-limit-2.7.0.tgz",
"integrity": "sha512-7nJ6v5lnJsXwGprnGXga4wx6d1POjvi5Qmf1ivTRxTjH4Z/9Czja/UCMLVmB9N93GeWOU93XaFaEt6jbuoagNw=="
},
"node_modules/showdown": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/showdown/-/showdown-2.1.0.tgz",
"integrity": "sha512-/6NVYu4U819R2pUIk79n67SYgJHWCce0a5xTP979WbNp0FL9MN1I1QK662IDU1b6JzKTvmhgI7T7JYIxBi3kMQ==",
"dependencies": {
"commander": "^9.0.0"
},
"bin": {
"showdown": "bin/showdown.js"
},
"funding": {
"type": "individual",
"url": "https://www.paypal.me/tiviesantos"
}
},
"node_modules/undici-types": {
"version": "6.19.8",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
"integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw=="
},
"node_modules/web-streams-polyfill": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
"integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
"engines": {
"node": ">= 8"
}
},
"node_modules/ws": {
"version": "8.18.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",
"integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}

View File

@ -1,20 +0,0 @@
{
"name": "forest-db",
"version": "1.0.0",
"description": "database tools for the forest",
"main": "db.js",
"scripts": {
"start": "sqld -d the_big_db.db --http-listen-addr=127.0.0.1:8000",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://git.shoofle.net/shoofle/the-forest"
},
"author": "shoofle",
"license": "ISC",
"dependencies": {
"@libsql/client": "^0.12.0",
"showdown": "^2.1.0"
}
}

View File

@ -1,48 +1,79 @@
// this is a horrible script i use and edit for db revisions and migrations. i hate it.
// i should have a db migration plan but this is not, and probably never will be, big
// enough of a project to really warrant it.
const sqlite = require("better-sqlite3");
const showdown = require("showdown");
const db = new sqlite('./the_big_db.db', { verbose: console.log });
const db = new sqlite('the_big_db.db', { verbose: console.log });
const converter = new showdown.Converter();
const createPages = db.prepare(`
create table if not exists pages (
id integer primary key,
number integer autoincrement,
title varchar(255),
description text,
html text
html text,
time timestamp default current_timestamp,
author integer
)
`);
const createUsers = db.prepare(`
create table if not exists users (
name varchar(64) primary key,
id integer primary key,
name varchar(64) unique,
password varchar(128)
)
`);
function initDb() {
console.log("creating page table")
function migratePages() {
console.log("moving old table to a temporary");
console.log(db.prepare(`alter table pages rename to old_pages`).run())
console.log("creating new page table")
console.log(createPages.run());
console.log("finding pages that havent been rendered");
rows = db.prepare(`select * from pages where html is null`).all();
console.log(rows);
console.log('iterating over old pages');
rows = db.prepare(`select * from old_pages`).all();
const insertPage = db.prepare(`replace into pages (id, title, description, html) values (:id, :title, :description, :html)`);
const insertPage = db.prepare(`insert into pages (number, title, description, html) values (:id, :title, :description, :html)`);
rows.forEach((pageData) => {
const {id, title, description, html} = pageData;
console.log(`rendering page number ${id}`)
console.log(`${id}. ${title}: ${description} ( ${html} )`);
const renderedPage = converter.makeHtml(description);
insertPage.run({...pageData, html: renderedPage});
});
console.log("creating user table");
console.log(createUsers.run());
console.log("getting rid of old table");
console.log(db.prepare("drop table old_pages").run());
}
initDb();
db.transaction(migratePages)();
function migrateUsers() {
console.log("moving old users");
console.log(db.prepare("alter table users rename to old_users").run());
console.log("creating new users table");
console.log(createUsers.run());
const insertUser = db.prepare("insert into users (name, password) values (:name, :password)")
db.prepare("select * from old_users").all().forEach((user) => {
console.log(insertUser.run(user));
});
console.log("clearing old table");
console.log(db.prepare("drop table old_users").run());
}
db.transaction(migrateUsers)();

View File

@ -4,17 +4,17 @@ const { circular } = require('graphology-layout');
function graphFromList(allTheStuff) {
const graph = new graphology.Graph();
for (const {id, html} of allTheStuff) {
for (const {number, html} of allTheStuff) {
graph.addNode(id);
graph.addNode(number);
}
for (const {id, html} of allTheStuff) {
for (const {number, html} of allTheStuff) {
const { document } = (new JSDOM(html)).window;
const links = document.querySelectorAll('a');
links.forEach((link) => {
const referent = link.href.replace("/","");
graph.mergeEdge(id, referent);
graph.mergeEdge(number, referent);
});
}

View File

@ -14,7 +14,7 @@ app.use(user_routes);
app.get('/graph', (req, res) => {
try {
const rows = db.prepare('select id, html from pages').all();
const rows = db.prepare('select number, html from pages').all();
const graph = graphFromList(rows);
res.status(200).json(graph);
} catch (error) {

View File

@ -2,7 +2,7 @@ const express = require('express');
const app = express.Router();
const sqlite = require('better-sqlite3');
const db = new sqlite('../the_big_db.db', { verbose: console.log });
const db = new sqlite('the_big_db.db', { verbose: console.log });
const { loginRequired } = require('../authStuff.js');
@ -11,7 +11,7 @@ const converter = new showdown.Converter();
app.get('/pages', (req, res) => {
try {
const pages = db.prepare('select id, title from pages').all();
const pages = db.prepare('select number, title from pages').all();
res.status(200).json(pages);
} catch (error) {
res.status(500).json({"error": error});
@ -20,17 +20,19 @@ app.get('/pages', (req, res) => {
app.post('/page/new', loginRequired, (req, res) => {
try {
const newPage = db.prepare('insert into pages (title, description) values (?, ?) returning id')
.get("new page", "this page is new!");
const maxPageNumber = db.prepare('select max(number) from pages').get()
console.log('maxPageNumber is ', maxPageNumber);
const newPage = db.prepare('insert into pages (title, description, author) values (?, ?, ?) returning number')
.get("new page", "this page is new!", req.session.userId);
res.status(200).json(newPage);
} catch (error) {
res.status(500).json({"error": error});
}
});
app.get('/page/:id', (req, res) => {
app.get('/page/:number', (req, res) => {
try {
const page = db.prepare('select * from pages where id=:id').get(req.params);
const page = db.prepare('select * from pages where number=:number order by time desc').get(req.params);
if (page === undefined) res.status(404).json({"error": "page not found"});
else res.status(200).json(page);
} catch (error) {
@ -38,11 +40,11 @@ app.get('/page/:id', (req, res) => {
}
});
app.post('/page/:id', loginRequired, (req, res) => {
app.post('/page/:number', loginRequired, (req, res) => {
try {
const html = converter.makeHtml(req.body.description);
const changes = db.prepare('replace into pages (id, title, description, html) values (?, ?, ?, ?)')
.run(req.params.id, req.body.title, req.body.description, html);
const changes = db.prepare('insert into pages (number, title, description, html, author) values (?, ?, ?, ?, ?)')
.run(req.params.number, req.body.title, req.body.description, html, req.session.userId);
res.status(200).json(changes);
} catch (error) {
res.status(500).json({"error": error});
@ -51,12 +53,29 @@ app.post('/page/:id', loginRequired, (req, res) => {
app.delete('/page/:id', loginRequired, (req, res) => {
try {
const changes = db.prepare('delete from pages where id = ?').run(req.params.id);
const changes = db.prepare('delete from pages where number = ?').run(req.params.id);
res.status(200).json({id: req.params.id});
} catch (error) {
res.status(500).json({"error": error});
}
});
app.get('/page/:number/history', (req, res) => {
try {
const all = db.prepare('select number, id, time, author from pages where number=:number order by time desc').all(req.params);
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});
}
});
module.exports = app;

View File

@ -2,7 +2,7 @@ const express = require('express');
const app = express.Router();
const sqlite = require('better-sqlite3');
const db = new sqlite('../the_big_db.db', { verbose: console.log });
const db = new sqlite('the_big_db.db', { verbose: console.log });
const argon2 = require('argon2');
@ -36,7 +36,7 @@ app.post('/login', async (req, res) => {
const {name, password} = req.body;
// fetch username and passswords from the db
const storedUser = db.prepare('select name, password from users where name = ?').get(name);
const storedUser = db.prepare('select * from users where name = ?').get(name);
if (!storedUser) {
return res.status(401).json({"error": "password/username combo not found in database"});
}
@ -48,8 +48,9 @@ app.post('/login', async (req, res) => {
// set the session cookie and rreturn 200!
req.session.name = name;
req.session.userId = storedUser.id;
console.log('setting req.session.name! : ', req.session);
return res.status(200).json({message: "successfully logged in!", name: name});
return res.status(200).json({message: "successfully logged in!", id: storedUser.id, name: name});
});
app.post('/logout', (req, res) => {
@ -59,6 +60,7 @@ app.post('/logout', (req, res) => {
app.get('/user', loginRequired, (req, res) => {
res.status(200).json({
"id": req.session.userId,
"name": req.session.name,
"favoriteColor": "red",
"leastFavoriteColor": "also red"