F1Manager-Calc/pages/api/cloud/config.js

31 lines
872 B
JavaScript
Raw Normal View History

2023-04-18 03:41:36 +02:00
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2023-04-18 04:39:39 +02:00
import {databasePromise} from "../../../libs/mongodb";
2023-04-18 05:21:16 +02:00
import {getServerSession} from "next-auth"
import {authOptions} from "../auth/[...nextauth]";
2023-04-18 03:41:36 +02:00
export default async (req, res) => {
2023-04-18 05:21:16 +02:00
const session = await getServerSession(req, res, authOptions)
2023-04-18 04:39:39 +02:00
let db = await databasePromise;
2023-04-18 05:21:16 +02:00
const { method, body } = req;
2023-04-18 04:39:39 +02:00
2023-04-18 05:21:16 +02:00
switch (method) {
case 'GET':
await db.collection('cloud_configs').findOne({ userId: session.userId });
res.send(JSON.stringify(session, null, 2))
break;
case 'POST': // set
let { config } = body;
await db.collection('cloud_configs').updateOne(
{ userId: session.userId },
{
$set: { config },
},
{ upsert: true },
);
res.send(JSON.stringify(session, null, 2))
break;
2023-04-18 03:41:36 +02:00
}
2023-04-18 04:39:39 +02:00
2023-04-18 03:41:36 +02:00
};