Total Complexity | 2 |
Complexity/F | 0 |
Lines of Code | 32 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import {Handler} from "@netlify/functions"; |
||
2 | import {MongoClient} from "mongodb"; |
||
3 | |||
4 | const uri = `mongodb+srv://${process.env.MONGODB_USER}:${process.env.MONGODB_PASSWORD}@cluster0.k6gbu.mongodb.net/test_database?retryWrites=true&w=majority`; |
||
5 | const dbName = "replayerDB"; |
||
6 | const collectionName = "media"; |
||
7 | |||
8 | const handler: Handler = async (event, context) => { |
||
9 | if (event.httpMethod !== "POST") { |
||
10 | return {statusCode: 405, body: "Method Not Allowed"}; |
||
11 | } |
||
12 | |||
13 | if (!event.body) { |
||
14 | return {statusCode: 400, body: "Bad Request"}; |
||
15 | } |
||
16 | |||
17 | // @ts-ignore |
||
18 | const client = new MongoClient(uri, {useNewUrlParser: true, useUnifiedTopology: true}); |
||
19 | await client.connect(); |
||
20 | const collection = client.db(dbName).collection(collectionName); |
||
21 | const body = JSON.parse(event.body); |
||
22 | const document = await collection.findOne({"resourceId": body.resourceId}); |
||
23 | await client.close(); |
||
24 | |||
25 | return { |
||
26 | statusCode: 200, |
||
27 | body: JSON.stringify({result: document || null}) |
||
28 | }; |
||
29 | }; |
||
30 | |||
31 | export {handler}; |
||
32 |