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/09_api_basics/bookshop/async/handlers/favourites.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
81 lines (70 sloc)
1.53 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 favourites = require('../modules/favourites') | |
const errorResponse = require('../modules/utils').errorResponse | |
async function validate(req, res, next) { | |
try { | |
await favourites.validate(req.body) | |
} catch (err) { | |
return errorResponse(err, res) | |
} | |
return next() | |
} | |
async function add(req, res) { | |
const user = req.authorization.basic.username | |
const book = req.body | |
try { | |
await favourites.add(user, book) | |
} catch (err) { | |
return errorResponse(err, res) | |
} | |
return res.send(201, book) | |
} | |
async function list(req, res) { | |
const user = req.authorization.basic.username | |
let favs = null | |
try { | |
favs = await favourites.list(user) | |
} catch (err) { | |
return errorResponse(err, res) | |
} | |
return res.send(200, favs) | |
} | |
async function get(req, res) { | |
const user = req.authorization.basic.username | |
const id = req.params.id | |
let book = null | |
try { | |
book = await favourites.get(user, id) | |
} catch (err) { | |
return errorResponse(err, res) | |
} | |
return res.send(200, book) | |
} | |
async function update(req, res) { | |
const user = req.authorization.basic.username | |
const book = req.body | |
try { | |
await favourites.update(user, book) | |
} catch (err) { | |
return errorResponse(err, res) | |
} | |
return res.send(201, book) | |
} | |
async function delete_(req, res) { | |
const user = req.authorization.basic.username | |
const id = req.params.id | |
try { | |
await favourites.delete(user, id) | |
} catch (err) { | |
return errorResponse(err, res) | |
} | |
return res.send(204) | |
} | |
module.exports = { | |
validate: validate, | |
add: add, | |
list: list, | |
get: get, | |
update: update, | |
delete: delete_ | |
} |