We are having problems with one of our functions.
getIdFromUsername(username)
in file model/users-model.js
line 35. Should be returning an id number, but is returning an empty object.
It works if you type directly into psql so not sure why this is happening. Because of this, route /:user/books
isn't functioning.
A REST API that returns JSON data. Our database holds data about the books that FAC cohort members have read.
- Clone this repo
- Run
$ npm install
on your terminal to install Node dependencies - Run these commands in your terminal to set up user, app database and test database
$ psql -c "CREATE USER myuser WITH PASSWORD 'mypassword'"
$ psql -c "ALTER USER myuser WITH SUPERUSER"
$ psql -c "CREATE DATABASE readers_coders_db WITH OWNER myuser"
$ psql -c "CREATE DATABASE readers_coders_test_db WITH OWNER myuser"
- Create a
.env
file in the root folder according to.env-example
- Comment out the following line on your
connection.js
file which connects to the Heroku database
// connectionString: process.env.DATABASE_URL,
- Run
psql
to enter Postgres CLI - Connect to database
# \c readers_coders_dbb
- Initialise database
# \i database/init.sql
- Run
$ npm run dev
to run app on localhost - Run
psql
and connect to test database# \c readers_coders_test_db
- Run
$ npm test
to run tests
Readers and Coders
You can make requests to the Heroku App URL, e.g. GET
to https://readers-and-coders.herokuapp.com/books
CREATE TABLE users
(
id SERIAL PRIMARY KEY,
username VARCHAR(50) UNIQUE,
password VARCHAR(255),
cohort INTEGER
);
CREATE TABLE books
(
id SERIAL PRIMARY KEY,
title TEXT,
author TEXT,
fiction BOOLEAN
);
CREATE TABLE user_books
(
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
PRIMARY KEY (user_id, book_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (book_id) REFERENCES books(id) ON UPDATE CASCADE ON DELETE CASCADE
);
Querying a book that has spaces in it requires that you insert %20
for each space in cURL, we are not sure about Postman
Gets data on all books in database
Get all fiction books from database
Get all non-fiction books from database
Gets books by their database id, e.g./books/2
Gets a list of users
Route for signing up, creates your access token
{
"username": "yourusername",
"password": "yourpassword",
"cohort": 19
}
Route for logging in, you receive an access token
{
"username": "yourusername",
"password": "yourpassword",
}
DISCLAIMER: currently doesn't work
Gets books associated with a particular username, e.g./ivo/books/
Gets all users who have read a particular book, e.g.
/wallace-and-grommit/users
Adds a book to the database
{
"title": "The Adventures of Gregor Cookie",
"author": "Vatsal Kanoria",
"fiction": true
}
Edits info on a particular book, e.g.
{
"title": "The Adventures of Gregor Cookie",
"author": "Vatsal Kanoria",
"fiction": false
}
Deletes a particular book