2024-10-22 08:45:52 +00:00
|
|
|
import { Database } from "bun:sqlite";
|
2024-10-22 04:35:21 +00:00
|
|
|
import {
|
|
|
|
addUser,
|
|
|
|
fetchCard,
|
2024-10-23 16:54:41 +00:00
|
|
|
fetchExpressionsByCard,
|
2024-10-22 04:35:21 +00:00
|
|
|
fetchLesson,
|
|
|
|
fetchLessons,
|
|
|
|
fetchResource,
|
2024-10-22 08:45:52 +00:00
|
|
|
} from "./db";
|
2024-10-20 12:51:30 +00:00
|
|
|
|
2024-10-22 08:45:52 +00:00
|
|
|
const db = new Database("../db/data.db", { strict: true });
|
|
|
|
db.exec("PRAGMA journal_mode = WAL;");
|
2024-10-20 12:51:30 +00:00
|
|
|
|
|
|
|
Bun.serve({
|
|
|
|
fetch(req) {
|
|
|
|
const url = new URL(req.url);
|
2024-10-22 08:45:52 +00:00
|
|
|
console.log(url.pathname, "url");
|
2024-10-22 04:35:21 +00:00
|
|
|
const user = parseUser(req);
|
2024-10-22 08:45:52 +00:00
|
|
|
if (req.method === "POST" && url.pathname === "/api")
|
2024-10-22 04:35:21 +00:00
|
|
|
return handlePost(req, user, url);
|
2024-10-22 08:45:52 +00:00
|
|
|
if (req.method === "GET" && url.pathname.startsWith("/api"))
|
2024-10-22 04:35:21 +00:00
|
|
|
return handleGet(req, user, url);
|
|
|
|
return serveStatic(url);
|
2024-10-20 12:51:30 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-10-22 04:35:21 +00:00
|
|
|
function parseUser(req: Request): number {
|
|
|
|
// console.log(req, 'request');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function serveStatic(url: URL) {
|
2024-10-22 08:45:52 +00:00
|
|
|
const filename = url.pathname === "/" ? "/index.html" : url.pathname;
|
|
|
|
const headers = { "Content-type": "text/html" };
|
2024-10-22 04:35:21 +00:00
|
|
|
const opts = { headers };
|
|
|
|
try {
|
|
|
|
const file = await Bun.file(`../ui/${filename}`).bytes();
|
|
|
|
return new Response(file, opts);
|
|
|
|
} catch (_) {
|
2024-10-22 08:45:52 +00:00
|
|
|
return new Response("404!");
|
2024-10-22 04:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleGet(_req: Request, user: number, url: URL) {
|
2024-10-22 08:45:52 +00:00
|
|
|
if (url.pathname === "/api/resource") return handleGetExpresion(user, url);
|
|
|
|
if (url.pathname === "/api/card") return handleGetCard(user, url);
|
|
|
|
if (url.pathname === "/api/lesson") return handleGetLesson(user, url);
|
|
|
|
if (url.pathname === "/api/lessons") return handleGetLessons(user, url);
|
|
|
|
else return new Response("huh");
|
2024-10-20 12:51:30 +00:00
|
|
|
}
|
2024-10-22 04:35:21 +00:00
|
|
|
function handleGetExpresion(user: number, url: URL) {
|
|
|
|
const params = new URLSearchParams(url.search);
|
2024-10-22 08:45:52 +00:00
|
|
|
const expression = params.get("exp");
|
2024-10-22 04:35:21 +00:00
|
|
|
const data = fetchResource(db, expression!);
|
|
|
|
return Response.json({ ok: data });
|
|
|
|
}
|
|
|
|
type LessonsType = Record<
|
|
|
|
number,
|
|
|
|
{
|
|
|
|
id: number;
|
|
|
|
text: string;
|
2024-10-23 16:54:41 +00:00
|
|
|
cards: Array<{
|
|
|
|
text: string;
|
|
|
|
note: string | null;
|
|
|
|
id: number;
|
|
|
|
words: Array<{ spelling: string; ipa: string; category: string }>;
|
|
|
|
}>;
|
2024-10-22 04:35:21 +00:00
|
|
|
}
|
|
|
|
>;
|
|
|
|
type LessonsDBType = {
|
|
|
|
id: number;
|
|
|
|
ltext: string;
|
|
|
|
ctext: string;
|
|
|
|
cnote: string | null;
|
|
|
|
cid: number;
|
|
|
|
};
|
|
|
|
function handleGetLesson(user: number, url: URL) {
|
2024-10-20 12:51:30 +00:00
|
|
|
const params = new URLSearchParams(url.search);
|
2024-10-22 08:45:52 +00:00
|
|
|
const lesson = params.get("lesson");
|
2024-10-22 04:35:21 +00:00
|
|
|
const data = fetchLesson(db, Number(lesson!));
|
|
|
|
return Response.json({ ok: data });
|
|
|
|
}
|
|
|
|
function handleGetCard(user: number, url: URL) {
|
|
|
|
const params = new URLSearchParams(url.search);
|
2024-10-22 08:45:52 +00:00
|
|
|
const card = params.get("card");
|
2024-10-22 04:35:21 +00:00
|
|
|
const data = fetchCard(db, Number(card), user);
|
|
|
|
return Response.json({ ok: data });
|
|
|
|
}
|
|
|
|
function handleGetLessons(user: number, url: URL) {
|
|
|
|
const params = new URLSearchParams(url.search);
|
2024-10-22 08:45:52 +00:00
|
|
|
const page = params.get("page") || "0";
|
2024-10-22 04:35:21 +00:00
|
|
|
const data: LessonsDBType[] = fetchLessons(db, 20, Number(page)) as any;
|
2024-10-22 08:45:52 +00:00
|
|
|
console.log(data, "fetchlessons");
|
2024-10-23 16:54:41 +00:00
|
|
|
console.log(data.length);
|
|
|
|
// const lessons = data.reduce((acc: LessonsType, item: LessonsDBType) => {
|
|
|
|
// let cur = acc[item.id] || { id: item.id, text: item.ltext, cards: [] };
|
|
|
|
// const words = fetchExpressionsByCard(db, item.cid) as any[];
|
|
|
|
// console.log(words, item.cid);
|
|
|
|
// const cards = [
|
|
|
|
// ...cur.cards,
|
|
|
|
// { text: item.ctext, note: item.cnote, id: item.cid, words },
|
|
|
|
// ];
|
|
|
|
// const def = { ...cur, cards };
|
|
|
|
// return { ...acc, [item.id]: def };
|
|
|
|
// }, {} as LessonsType);
|
|
|
|
// return Response.json({ ok: lessons });
|
|
|
|
return Response.json({ ok: data });
|
2024-10-20 12:51:30 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 04:35:21 +00:00
|
|
|
async function handlePost(req: Request, user: number, url: URL) {
|
2024-10-20 12:51:30 +00:00
|
|
|
const data = await req.json();
|
2024-10-22 08:45:52 +00:00
|
|
|
if (url.pathname === "/api/user") return handlePostUser(data);
|
|
|
|
else return new Response("huh");
|
2024-10-20 12:51:30 +00:00
|
|
|
}
|
|
|
|
// https://bun.sh/guides/http/server
|
|
|
|
type PostUser = { name: string; creds: string };
|
|
|
|
function handlePostUser(user: PostUser) {
|
2024-10-22 04:35:21 +00:00
|
|
|
addUser(db, user.name, user.creds);
|
2024-10-22 08:45:52 +00:00
|
|
|
return new Response("ok");
|
2024-10-20 12:51:30 +00:00
|
|
|
}
|