Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
287 changes: 171 additions & 116 deletions pg-promise-exercises/exercises.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ const postgresConfig = {
host: 'localhost',
port: 5432,
database: 'pg-promise-exercises',
user: '<change-this-to-your-username>', // replace this with your username
user: 'waynebanks', // replace this with your username
password: '' // replace this if you have set a password for your username (this is unlikely)
};


const db = pg(postgresConfig);

/* -----------------------------------------
Exercise 1
-----------------------------------------
// -----------------------------------------
// Exercise 1
// -----------------------------------------

This is an example function that finds all the books from the `books` table
@function: `allBooks`
@input params: None
@output: [{id, title, author_id, subject_id}]
// This is an example function that finds all the books from the `books` table
// @function: `allBooks`
// @input params: None
// @output: [{id, title, author_id, subject_id}]

// The assertion fails, and you have to make it pass.

The assertion fails, and you have to make it pass.

*/



const allBooks = db.any('select * from books')
/* This is calling the `then` function on the `allBooks` promise, and checks if
we get back 15 rows. This assertion will fail. Make it PASS!*/
allBooks.then(books => {
assert.deepEqual(books.length, 20)
assert.deepEqual(books.length, 15)
}).catch(error => {
console.log('Dang, my assertion failed.', error);
});
Expand All @@ -42,20 +42,20 @@ allBooks.then(books => {



/* -----------------------------------------
Exercise 2
-----------------------------------------
// -----------------------------------------
// Exercise 2
// -----------------------------------------

// Implement the function `firstTenBooks` which returns just the names of the
// books, and make the assertion pass.
// @function: `firstTenBooks`
// @input params: None
// @output: [{id, title, author_id, subject_id}]

Implement the function `firstTenBooks` which returns just the names of the
books, and make the assertion pass.
@function: `firstTenBooks`
@input params: None
@output: [{id, title, author_id, subject_id}]


*/

let firstTenBooks; // = .... IMPLEMENT THIS FUNCTION
let firstTenBooks = db.any('SELECT * FROM books LIMIT 10'); // = .... IMPLEMENT THIS FUNCTION
firstTenBooks.then(books => {
assert(books.length, 10)
}).catch(error => {
Expand All @@ -67,153 +67,208 @@ firstTenBooks.then(books => {



/* -----------------------------------------
Exercise 3
-----------------------------------------
// -----------------------------------------
// Exercise 3
// -----------------------------------------

// Implement the function `findAuthorsOrderedByLastName` which returns all the
// authors from the the `authors` table, and the rows are ordered by the
// `last_name`.

Implement the function `findAuthorsOrderedByLastName` which returns all the
authors from the the `authors` table, and the rows are ordered by the
`last_name`.

// @function: `findAuthorsOrderedByLastName`
// @input params: None
// @output: [{id, first_name, last_name}]

@function: `findAuthorsOrderedByLastName`
@input params: None
@output: [{id, first_name, last_name}]


*/

let findAuthorsOrderedByLastName; // = .... IMPLEMENT THIS FUNCTION
let findAuthorsOrderedByLastName = db.any('SELECT * FROM authors ORDER BY last_name'); // = .... IMPLEMENT THIS FUNCTION
findAuthorsOrderedByLastName.then(authors => {
assert.deepEqual(authors.length, 19)
assert.deepEqual(authors[0].last_name, 'Alcott')
assert.deepEqual(authors[18].last_name, 'Worsley')
}).catch(error => {
console.log('Whoops, my function doesnt behave as expected.', error);
});

//
/* --------End of Exercise 3---------------- */



/* -----------------------------------------
Exercise 4
-----------------------------------------

Implement the function `findBookAuthors` which returns the `first_name` and
`last_name` from the `authors` table, and the `title` of the
books(from the `books` table) that the authors have written.

@function: `findBookAuthors`
@input params: None
@output: [{first_name, last_name, title}]

In this exercise you will ALSO have to write the assertions. For inspiration,
look at the assertions in Exercises 1 - 3.

Expected Result:
[{first_name: 'John', last_name: 'Worsley', title: 'Practical PostgreSQL'}
{first_name: 'Paulette', last_name: 'Bourgeois', title: 'Franklin in the Dark'}
{first_name: 'Margery Williams', last_name: 'Bianco', title: 'The Velveteen Rabbit'}
{first_name: 'Louisa May', last_name: 'Alcott', title: 'Little Women'}
{first_name: 'Stephen', last_name: 'King', title: 'The Shining'}
{first_name: 'Frank', last_name: 'Herbert', title: 'Dune'}
{first_name: 'Burne', last_name: 'Hogarth', title: 'Dynamic Anatomy'}
{first_name: 'Margaret Wise', last_name: 'Brown', title: 'Goodnight Moon'}
{first_name: 'Edgar Allen', last_name: 'Poe', title: 'The Tell-Tale Heart'}
{first_name: 'Mark', last_name: 'Lutz', title: 'Learning Python'}
{first_name: 'Mark', last_name: 'Lutz', title: 'Programming Python'}
{first_name: 'Tom', last_name: 'Christiansen', title: 'Perl Cookbook'}
{first_name: 'Arthur C.', last_name: 'Clarke', title: '2001: A Space Odyssey'}
{first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'Bartholomew and the Oobleck'}
{first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'The Cat in the Hat'}]
*/
let findBookAuthors; // IMPLEMENT THIS FUNCTION

// -----------------------------------------
// Exercise 4
// -----------------------------------------

// Implement the function `findBookAuthors` which returns the `first_name` and
// `last_name` from the `authors` table, and the `title` of the
// books(from the `books` table) that the authors have written.

// @function: `findBookAuthors`
// @input params: None
// @output: [{first_name, last_name, title}]

// In this exercise you will ALSO have to write the assertions. For inspiration,
// look at the assertions in Exercises 1 - 3.

// Expected Result:
// [{first_name: 'John', last_name: 'Worsley', title: 'Practical PostgreSQL'}
// {first_name: 'Paulette', last_name: 'Bourgeois', title: 'Franklin in the Dark'}
// {first_name: 'Margery Williams', last_name: 'Bianco', title: 'The Velveteen Rabbit'}
// {first_name: 'Louisa May', last_name: 'Alcott', title: 'Little Women'}
// {first_name: 'Stephen', last_name: 'King', title: 'The Shining'}
// {first_name: 'Frank', last_name: 'Herbert', title: 'Dune'}
// {first_name: 'Burne', last_name: 'Hogarth', title: 'Dynamic Anatomy'}
// {first_name: 'Margaret Wise', last_name: 'Brown', title: 'Goodnight Moon'}
// {first_name: 'Edgar Allen', last_name: 'Poe', title: 'The Tell-Tale Heart'}
// {first_name: 'Mark', last_name: 'Lutz', title: 'Learning Python'}
// {first_name: 'Mark', last_name: 'Lutz', title: 'Programming Python'}
// {first_name: 'Tom', last_name: 'Christiansen', title: 'Perl Cookbook'}
// {first_name: 'Arthur C.', last_name: 'Clarke', title: '2001: A Space Odyssey'}
// {first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'Bartholomew and the Oobleck'}
// {first_name: 'Theodor Seuss', last_name: 'Geisel', title: 'The Cat in the Hat'}]


let findBookAuthors = db.any(
SELECT *
FROM authors a
JOIN books b
ON a.id = b.id
);

findBookAuthors.then(function bookAuthors(){
assert.deepEqual(books.authors.length, 15)
assert.deepEqual(authors[0].last_name, 'Alcott')
assert.deepEqual(authors[1].first_name, 'Paulette')
}).catch(error => {
console.log('Whoops, my joint is bugging out, b', error);
});
/* --------End of Exercise 4---------------- */





/* -----------------------------------------
Exercise 5
-----------------------------------------
// -----------------------------------------
// Exercise 5
// -----------------------------------------

// Implement the function `authorIdWithTwoBooks` which returns the
// `author_id` of authors who have 2 books. (HINT: you have to use a SUBQUERY)

Implement the function `authorIdWithTwoBooks` which returns the
`author_id` of authors who have 2 books. (HINT: you have to use a SUBQUERY)
// @function: `authorIdWithTwoBooks`
// @input params: None
// @output: [{first_name, last_name, title}]

@function: `authorIdWithTwoBooks`
@input params: None
@output: [{first_name, last_name, title}]
// In this exercise you will ALSO have to write the assertions. For inspiration,
// look at the assertions in Exercises 1 - 3.

In this exercise you will ALSO have to write the assertions. For inspiration,
look at the assertions in Exercises 1 - 3.
// Expected Result:
// [{author_id: 1809},
// {author_id: 7805}]

Expected Result:
[{author_id: 1809},
{author_id: 7805}]


*/
let authorIdWithTwoBooks; // IMPLEMENT THIS FUNCTION
let authorIdWithTwoBooks= db.any(SELECT twoBooks.author_id FROM
(SELECT author_id, count(author_id) AS total FROM authors
JOIN books
ON authors.id = books.author_id
GROUP BY author_id) twoBooks WHERE total = 2);

authorIdWithTwoBooks.then(function twoBookId(){
assert.deepEqual(twoBookId.length, 2)
assert.deepEqual(twoBookId[0].authors.id, 1809)
assert.deepEqual(twoBookId[1].authors.id, 7805)
}).catch(error => {
console.log('Whoops, my joint is bugging out, b', error);
});

/* --------End of Exercise 5---------------- */





/* -----------------------------------------
Exercise 6
-----------------------------------------
// -----------------------------------------
// Exercise 6
// -----------------------------------------

// Implement the function `bookTitlesWithMultipleEditions` which returns the
// `title` of books which have more than one entry in the editions table (do not use the "edition" field). (HINT: you have to use a join)

// @function: `bookTitlesWithMultipleEditions`
// @input params: None
// @output: [{title}]

Implement the function `bookTitlesWithMultipleEditions` which returns the
`title` of books which have more than one entry in the editions table (do not use the "edition" field). (HINT: you have to use a join)
// In this exercise you will ALSO have to write the assertions. For inspiration,
// look at the assertions in Exercises 1 - 3.

@function: `bookTitlesWithMultipleEditions`
@input params: None
@output: [{title}]
// Expected Result:
// [{title: 'The Shining'},
// {title: 'The Cat in the Hat'},
// {title: 'Dune'}
// {title: '2001: A Space Odyssey'}
// {title: 'The Tell-Tale Heart'}]

In this exercise you will ALSO have to write the assertions. For inspiration,
look at the assertions in Exercises 1 - 3.

Expected Result:
[{title: 'The Shining'},
{title: 'The Cat in the Hat'},
{title: 'Dune'}
{title: '2001: A Space Odyssey'}
{title: 'The Tell-Tale Heart'}]
let bookTitlesWithMultipleEditions = db.any(SELECT author_name
FROM books
GROUP BY author_name
HAVING COUNT(distinct page_count) > 1
);

*/
let bookTitlesWithMultipleEditions; // IMPLEMENT THIS FUNCTION
bookTitlesWithMultipleEditions.then( function multipleEditions(){
assert.deepEqual(multipleEditions.length, 5)
assert.deepEqual(multipleEditions[0].title, 'The Shining')
assert.deepEqual(multipleEditions[1].title, 'The Cat in the Hat')

})

/* --------End of Exercise 6---------------- */




/* -----------------------------------------
Exercise 7
-----------------------------------------
// -----------------------------------------
// Exercise 7
// -----------------------------------------

// Implement the function `findStockedBooks` which returns the `title` & the
// author's `first_name` & `last_name` of all books which are stocked as
// represented in the `daily_inventory` table.

// @function: `findStockedBooks`
// @input params: None
// @output: [{first_name, last_name, title}]

// In this exercise you will ALSO have to write the assertions. For inspiration,
// look at the assertions in Exercises 1 - 3.

Implement the function `findStockedBooks` which returns the `title` & the
author's `first_name` & `last_name` of all books which are stocked as
represented in the `daily_inventory` table.
// Expected Result:
// [ {first_name: 'Frank', title: 'Dune', last_name: 'Herbert'},
// {title: 'The Cat in the Hat', first_name: 'Theodor Seuss', last_name: 'Geisel'}]

@function: `findStockedBooks`
@input params: None
@output: [{first_name, last_name, title}]

In this exercise you will ALSO have to write the assertions. For inspiration,
look at the assertions in Exercises 1 - 3.
let findStockedBooks = db.any(SELECT title, first_name, last_name, is_stocked
FROM daily_inventory d
JOIN editions e
ON d.isbn = e.isbn
JOIN books b
ON b.id = e.book_id
JOIN authors a
ON a.id = b.author_id

WHERE is_stocked = TRUE

);

Expected Result:
[ {first_name: 'Frank', title: 'Dune', last_name: 'Herbert'},
{title: 'The Cat in the Hat', first_name: 'Theodor Seuss', last_name: 'Geisel'}]
findStockedBooks.then(function(){
assert.deepEqual(multipleEditions.length, 3)
assert.deepEqual(multipleEditions[0].title, 'Dune')
assert.deepEqual(multipleEditions[0].last_name, 'Herbert')
assert.deepEqual(multipleEditions[2].title, 'The Cat in the Hat')

*/
let findStockedBooks; // IMPLEMENT THIS FUNCTION
})

/* --------End of Exercise 7---------------- */

Expand Down
Loading