net-art-uchicago / cta-file-sharing

a collaborative artware project by Media Art and Design students at the University of Chicago.
GNU General Public License v3.0
2 stars 15 forks source link

backend improvements #43

Closed nbriz closed 1 year ago

nbriz commented 1 year ago

hey @armiesingh

update the addPoem function

let's improve the addPoem() function so that it validates the data && returns an error (if/when there's an issue) https://github.com/net-art-uchicago/cta-file-sharing/blob/235b371bfe9986e425e9fa4f8b22c3a0e1abdcb6/backend/init_database.js#L12-L19

before u push the "poem" to the db && re-write the file, u should check that the poem object contains all the appropriate properties && that those properties have the correct types

    poem.datetime // number
    poem.location // an array w/two items in it (both numbers)
    poem.text // a string
    poem.author // a string,
    poem.route // a number

if something is wrong, then the function should return an object w/an error message like return { error: 'datetime should be a number' }

if the poem object passes all the validation then we can push it to the database && update the file, but rather than using the asynchronous version writeFile like the file is now, let's use the synchronous version writeFileSync like we do here: https://github.com/net-art-uchicago/cta-file-sharing/blob/235b371bfe9986e425e9fa4f8b22c3a0e1abdcb6/backend/init_database.js#L8

&& the return { message: 'success' } or something similar

update the add-poem REST API endpoint

https://github.com/net-art-uchicago/cta-file-sharing/blob/235b371bfe9986e425e9fa4f8b22c3a0e1abdcb6/backend/REST-API.js#L13-L22

remove the commented code && replace the last line with res.json(check) if u're function is properly returning an object with a success or error message, than this will pass it along to the front end.

clean up

let's delete the test scripts:

let's remove this comment https://github.com/net-art-uchicago/cta-file-sharing/blob/235b371bfe9986e425e9fa4f8b22c3a0e1abdcb6/backend/REST-API.js#L8