Created
April 11, 2021 16:50
-
-
Save omirobarcelo/08b5c955dc889ee3a60f0865c69c1601 to your computer and use it in GitHub Desktop.
WebHero - 5 - Create endpoint
This file contains 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
export async function post(req: Request, res: Response, next: NextFunction) { | |
const { name, altNames, image, vote, comment } = req.body; | |
let game: IGameDocument; | |
try { | |
game = new models.Game({ | |
name, | |
altNames, | |
image, | |
positiveVotes: vote === 'positive' ? 1 : 0, | |
negativeVotes: vote === 'negative' ? 1 : 0, | |
comments: comment ? [{ kind: vote, text: comment, date: new Date() }] : [], | |
locked: false, | |
accepted: false | |
}); | |
game = await game.save(); | |
} catch (err) { | |
return res.status(500).send({ successful: false, errorMsg: 'Error while saving the game entry.' }); | |
} | |
return res.send(game); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment