first commit

This commit is contained in:
didier amyot committed 2023-11-20 07:38:50 -05:00
commit 22489242c2
7 files changed
+326

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import {createBundle} from "./bundler.ts";
import {Context} from "https://deno.land/x/oak@v12.2.0/context.ts";
import {Team} from "./shared.ts";
const router = new Router();
const clientBundle = createBundle(
new URL("./client.ts", import.meta.url),
new URL("../import_map.json", import.meta.url),
false,
);
router.get("/", (context) => {
context.response.body = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<link href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css" rel="stylesheet">
</head>
<body>
<script type="module" src="client.js"></script>
</body>
</html>
`;
});
function getUserIndex(userId: string, ballotId: string, questionId: string){
return ["tatoo-vote",
"v1",
"results",
"user",
userId,
"ballot",
ballotId,
"question",
questionId]
}
function persistTeams(userId: any, ballots: Team[]) {
const kv = await Deno.openKv();
for(const ballot of ballots){
for(const question of Object.keys(ballot.scores)){
const k = getUserIndex(userId, ballot.id, question);
const v = {value: ballot.scores[question]};
await kv.set(k, v);
}
}
}
router.post("/api/v1/vote/:userid", async (ctx: Context)=> {
const userId = ctx?.params?.userid??"";
if(!ctx.request.hasBody){
ctx.throw(415);
}
const body = await ctx.request.body().value;
const teams = body as Team[];
await persistTeams(userId, teams);
console.log(body);
ctx.response.body = userId;
});
router.get("/client.js", async (context): Promise<void> => {
context.response.body = await createBundle(
new URL("./client.ts", import.meta.url),
new URL("../import_map.json", import.meta.url),
false,
);
context.response.type = "application/javascript";
});
const app = new Application();
app.use(router.routes());
app.use((ctx) => {
ctx.response.body = "Hello World!";
});
await app.listen({ port: 3333 });