Last active
October 3, 2022 15:14
-
-
Save polluterofminds/bd6103153e352940a6fd7cb65f4ead65 to your computer and use it in GitHub Desktop.
SoundBasket artist.js POST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MongoClient } from "mongodb"; | |
const uri = process.env.MONGO_URI; | |
const client = new MongoClient(uri); | |
export default async function handler(req, res) { | |
if (req.method === "POST") { | |
try { | |
const database = client.db("pinata"); | |
const artists = database.collection("artists"); | |
const body = JSON.parse(req.body); | |
const payload = { | |
...body, | |
pinata_user: user, | |
}; | |
const found = await artists.findOne({ username: body.username }); | |
if (found) { | |
const success = await artists.updateOne( | |
{ username: body.username }, | |
{ $set: payload }, | |
{ upsert: true } | |
); | |
return res.json(success); | |
} | |
const result = await artists.insertOne(payload); | |
res.json(result); | |
} catch (error) { | |
console.log(error); | |
res.status(500).send("Server error"); | |
} | |
} else { | |
res.send("OK"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment