/FairGrounds

News Rating App

Primary LanguageJavaScript

FairGrounds

News Rating App

Database setup

We are using a Postgres database that have three tables set up as follow:

Users:

userid integer NOT NULL, username character varying(20) NOT NULL, password character varying(20) NOT NULL, leaning integer NOT NULL

Articles:

CREATE TABLE public.articles ( articleid integer NOT NULL, topic character varying(1000), title character varying(1000) NOT NULL, url character varying(1000) NOT NULL, author character varying(1000), description character varying(1000), publishedat character varying(1000), source character varying(1000), urltoimage character varying(1000) );

Ratings:

ratingid serial, userid integer NOT NULL, articleid integer NOT NULL, written_fairly bit(1) NOT NULL

Restful API format

GET

Full tables

To retrieve JSON of entire database tables, make GET request at location:

localhost:3000/{insert_table}

Optional tables are users, articles, and ratings (listed in the database section)

An example return of GET request to localhost:3000/users :

[{"username":"rfpoulos","password":"newTestPassword","userid":6,"leaning":80,"email":"rfpoulos@outlook.com"},{"username":"rfpoulos2","password":"test","userid":7,"leaning":75,"email":"rfpoulos2@outlook.com"}]

Individual entries

For individual entries in each table, send GET request to:

localhost:3000/{insert_table}/{insert_id#}

For example localhost:3000/users/6 will return the entire column of the user with the serial id 6. Same for articles and ratings.

An example return of GET request to localhost:3000/users/6 :

[{"username":"rfpoulos","password":"newTestPassword","userid":6,"leaning":80,"email":"rfpoulos@outlook.com"}]

POST

POST requests can be done to each of the three tables in the following format

localhost:3000/{insert_table}

The sent object must have all of the NOT NULL values as listed in the datatables section. Otherwise, the value is optional.

Examples of acceptable POST user objects

{"username":"rfpoulos2","password":"test", "leaning":75,"email":"rfpoulos2@outlook.com"}

OR {"username":"rfpoulos2","password":"test", "email":"rfpoulos2@outlook.com"} because the "leaning" attribute is not required by the PSQL database.

PUT

A PUT request for any of the tables can be done to the following address:

localhost:3000/{insert_table}/{insert_id#}

The JSON package with it only needs the values that need to be updated. For example, lets say you want to update the following user object:

{"username":"rfpoulos","password":"newTestPassword","userid":6,"leaning":80,"email":"rfpoulos@outlook.com"}

You would send a PUT request to localhost:3000/users/6 with a JSON object that just contained the values you wanted to change. Lets say just the e-mail and password. You would send the following object:

{"email": "new-email@email.com", "password": "betterPassword"}

Now a GET request to localhost:3000/users/6 would reflect the following:

{"username":"rfpoulos","password":"betterPassword","userid":6,"leaning":80,"email":"rfpoulos@outlook.com"}

DELETE

To delete a column in any database, send a DELETE request to the following location:

localhost:3000/{insert_table}/{insert_id#}