From 6dcce42caba7b663c262d7b6cfab245708364fe8 Mon Sep 17 00:00:00 2001 From: b1ek Date: Sun, 10 Nov 2024 01:20:14 +1000 Subject: [PATCH] add selection api to back --- back/src/index.ts | 20 +++++++++++++++----- back/src/routes/selection.ts | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/back/src/index.ts b/back/src/index.ts index f487a18..751de9e 100644 --- a/back/src/index.ts +++ b/back/src/index.ts @@ -1,13 +1,13 @@ import express from 'express'; import session from 'express-session'; +import { TypeormStore } from 'connect-typeorm'; import 'reflect-metadata'; import routes from './routes.js'; import dataSourceRepo from './typeorm/data-source.repo.js'; +import { Session } from './typeorm/entity/Session.entity.js'; import './env.js'; -import { TypeORMError } from 'typeorm'; -import { TypeormStore } from 'connect-typeorm'; const listen_host = process.env.LISTEN_HOST ?? '0.0.0.0'; const listen_port = parseInt(process.env.LISTEN_PORT ?? '80'); @@ -17,6 +17,12 @@ await dataSourceRepo.runMigrations(); const app = express(); +declare module "express-session" { + interface SessionData { + selection: number[] | null + } +} + app.use(express.json()); app.use(session({ resave: false, @@ -24,9 +30,13 @@ app.use(session({ store: new TypeormStore({ cleanupLimit: 2, limitSubquery: false, - ttl: 3600 - }), - secret: 'keyboard cat' + ttl: 3600, + }).connect(dataSourceRepo.getRepository(Session)), + secret: 'keyboard cat', + cookie: { + httpOnly: true, + sameSite: true + } })); app.use('/api', routes()); diff --git a/back/src/routes/selection.ts b/back/src/routes/selection.ts index 3d8bfb9..be27caa 100644 --- a/back/src/routes/selection.ts +++ b/back/src/routes/selection.ts @@ -2,5 +2,21 @@ import { IRouter, Router } from "express"; export default function(): IRouter { const app = Router(); + + app.put('/', async (req, res) => { + const body = req.body as { selection?: number[] } | null; + if (!body) { res.send(400); return; } + if (!body.selection) { res.send(400); return; } + if (!(body.selection instanceof Array)) { res.send(400); return; } + + req.session.selection = body.selection; + res.send(req.session.selection); + return + }); + + app.get('/', async (req, res) => { + res.send(req.session.selection ?? []) + }) + return app; } \ No newline at end of file