Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/exercises/12_advanced_apis/bookshop/bookshop.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
128 lines (113 sloc)
3.59 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const auth = require('./modules/authorisation') | |
const google = require('./modules/google') | |
const persistence = require('./modules/persistence') | |
// ------------------ ROUTE FUNCTIONS ------------------ | |
exports.search = (request, callback) => { | |
extractParam(request, 'q') | |
.then( query => google.searchByString(query)) | |
.then( data => this.cleanArray(request, data)) | |
.then( data => callback(null, data)) | |
.catch( err => callback(err)) | |
} | |
exports.addToCartOld = (request, callback) => { | |
extractBodyKey(request, 'id') | |
.then( id => google.getByID(id)) | |
.then( book => persistence.saveBook(book)) | |
.catch( err => callback(err)) | |
} | |
exports.addToCart = (request, callback) => { | |
auth.getHeaderCredentials(request).then( credentials => { | |
this.username = credentials.username | |
this.password = credentials.password | |
return auth.hashPassword(credentials) | |
}).then( credentials => { | |
return persistence.getCredentials(credentials) | |
}).then( account => { | |
const hash = account[0].password | |
return auth.checkPassword(this.password, hash) | |
}).then( () => { | |
return extractBodyKey(request, 'id') | |
}).then( id => { | |
this.id = id | |
return google.getByID(id) | |
}).then( (book) => { | |
this.book = book | |
return persistence.bookExists(this.username, this.id) | |
}).then( book => { | |
this.book.account = this.username | |
return persistence.saveBook(this.book) | |
}).then( book => { | |
delete book.account | |
callback(null, book) | |
}).catch( err => { | |
callback(err) | |
}) | |
} | |
exports.showCart = (request, callback) => { | |
auth.getHeaderCredentials(request).then( credentials => { | |
this.username = credentials.username | |
this.password = credentials.password | |
return auth.hashPassword(credentials) | |
}).then( credentials => { | |
return persistence.getCredentials(credentials) | |
}).then( account => { | |
const hash = account[0].password | |
return auth.checkPassword(this.password, hash) | |
}).then( () => { | |
return persistence.getBooksInCart(this.username) | |
}).then( books => { | |
return this.removeMongoFields(request, books) | |
}).then( books => { | |
callback(null, books) | |
}).catch( err => { | |
callback(err) | |
}) | |
} | |
exports.addUser = (request, callback) => { | |
let data | |
auth.getHeaderCredentials(request).then( credentials => { | |
return auth.hashPassword(credentials) | |
}).then( credentials => { | |
data = credentials | |
return persistence.accountExists(credentials) | |
}).then( () => { | |
return extractBodyKey(request, 'name') | |
}).then( name => { | |
data.name = name | |
return persistence.addAccount(data) | |
}).then( data => { | |
callback(null, data) | |
}).catch( err => { | |
callback(err) | |
}) | |
} | |
// ------------------ UTILITY FUNCTIONS ------------------ | |
const extractParam = (request, param) => new Promise( (resolve, reject) => { | |
if (request.params === undefined || request.params[param] === undefined) reject(new Error(`${param} parameter missing`)) | |
resolve(request.params[param]) | |
}) | |
const extractBodyKey = (request, key) => new Promise( (resolve, reject) => { | |
if (request.body === undefined || request.body[key] === undefined) reject(new Error(`missing key ${key} in request body`)) | |
resolve(request.body[key]) | |
}) | |
exports.cleanArray = (request, data) => new Promise((resolve) => { | |
const host = request.host || 'http://localhost' | |
const clean = data.items.map(element => { | |
return { | |
title: element.volumeInfo.title, | |
link: `${host}/books/${element.id}` | |
} | |
}) | |
resolve({books: clean}) | |
}) | |
exports.removeMongoFields = (request, data) => new Promise( (resolve, reject) => { | |
const host = request.host || 'http://localhost' | |
const clean = data.map(element => { | |
return { | |
title: element.title, | |
link: `${host}/books/${element.bookID}` | |
} | |
}) | |
resolve({books: clean}) | |
}) |