Skip to content

Instantly share code, notes, and snippets.

@omirobarcelo
Created April 11, 2021 16:50
Show Gist options
  • Save omirobarcelo/08b5c955dc889ee3a60f0865c69c1601 to your computer and use it in GitHub Desktop.
Save omirobarcelo/08b5c955dc889ee3a60f0865c69c1601 to your computer and use it in GitHub Desktop.
WebHero - 5 - Create endpoint
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