Structuring and saving the database object

Once we have the request model and the function for generating a random ID, we will create a new object that we will store in the database. The code for that will look like this:

exports.createPoll = functions.https.onRequest { req, res ->    val reqBody = req.rawBody    val poll = JSON.parse<RequestPoll>(reqBody)    val newPoll = ResponsePoll(getRandomPollId().toString(), poll.pollQuestion, poll.pollOptions)}

The final onRequest function will look like this:

exports.createPoll = functions.https.onRequest { req, res ->    val reqBody = req.rawBody    val poll = JSON.parse<RequestPoll>(reqBody)    val newPoll = ResponsePoll(getRandomPollId().toString(), poll.pollQuestion, poll.pollOptions)    res.status(200).send(JSON.stringify(newPoll)) ...

Get Hands-On Serverless Applications with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.