Python Falcon API Method #2, Liking Posts, and Updating Data in MongoDB

In my last post, I discussed a back-end API method to store responses in my MongoDB submitted from a user text input box via a POST request. In this post, I will outline the code to pull responses from Mongo, display them in my app, track which response is clicked, and then post that response like back to Mongo.

To pull data from Mongo, I use the connection[‘databaseName’][‘collectionName’].find() method. I send a few variables in my client side get request, for example user id, level id, and level type. Then, I can use these in my find method like so:

cursor = connection[‘databaseName’][‘collectionName’].find({ “$AND” : [ { ‘user id’ : user_id_value }, { ‘level id’: level_id_value }, ‘level type’: level_type_value } ] } )

This query will give me a cursor object that I can iterate through using a for loop in my server side code. Then, I can stick all of the data I want in a dictionary, dump it to a JSON object (import json, json.dumps()), and then send it back to my client. On the app side, I make the network request and store the data in an object (see code below):

local function networkListener( event )

print(event.response)
if ( event.isError ) then

print( “Network error: “, event.response )

else

my_data = json.decode(event.response)

network.request(‘insert URL here’, ‘GET’, networkListener)

I now have all of my data stored in the my_data object. To stick this data in a table, I have to loop through my_data, appending the appropriate values to the list_options in my MUI table view (see code below):

finalResponsesDisplayTable = {}
for i,x in pairs(my_data) do

textResponse = x[“responseText”]
table.insert(finalResponsesDisplayTable,1,{ key = x[“responseID”], text = textResponse, value = x[“responseID”], isCategory = false })

end

finalResponsesDisplayTable is the object that I am going to pass to my MUI list parameter within the tableOptions values when I am creating a mui.newTableView. Now I have the responses in a table, displaying on the screen, and I have a way to identify these values. Notice in my code above that I put the responseID of each response as the key. This will let me access this value on a user touch event.

Once a user touches a row of the table, I can log that it has been touched, save the responseID, and change the color of the table row (code not shown). Once the correct responses have been selected, I can post those responseIDs back to my back-end to update the like count in the database. This is making a simple post network request after developing a back-end method to handle the request.

To update values, it is a little complicated because I am updating the value of a dictionary within an array within a document. I won’t go into the details of all data selection, insertion, and updating in MongoDB, but here is the code I used to update the liked value within each response (list of responses) in a document based on the ID (code below).

connection[‘databaseName’][‘collectionName’].update({“documentField1”: {“$elemMatch”: {“field_within_array”: uniqueID}}},{ “$inc”: { “documentField1.$.field2_within_array” : 1}})

The code above will find search through a document structure that looks like this:

{

documentField1:[{

field_within_array: uniqueID,

field2_within_array: 0,

},

{

field_within_array: uniqueID,

field2_within_array: 0,

}],

documentField2:””,

}

This will increment the field2_within_array by 1.

Well that’s it for this post. We successfully pulled data from MongoDB, displayed it visually in a table using MUI newTableView, added an event listener to a table touch that added a responseID to a table, then we posted the responseIDs to the server, where we then increased a field by one (viewed, liked, chosen, etc) using some complicated MongoDB syntax.

Leave a Comment

Your email address will not be published. Required fields are marked *

css.php