F1Manager-Calc/pages/api/cloud/config.js
2023-04-18 13:40:11 +08:00

31 lines
900 B
JavaScript

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import {databasePromise} from "../../../libs/cloud/mongodb";
import {getServerSession} from "next-auth"
import {authOptions} from "../auth/[...nextauth]";
export default async (req, res) => {
const session = await getServerSession(req, res, authOptions)
let db = await databasePromise;
const { method, body } = req;
switch (method) {
case 'GET':
let cloudConfig = await db.collection('cloud_configs').findOne({ userId: session.userId });
res.send(JSON.stringify(cloudConfig, 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;
}
};