'use strict'; const express = require('express'); const bodyParser = require('body-parser'); const morgan = require('morgan'); const ObjectID = require('mongodb').ObjectID; const HttpCat = require('./http-cat.js') const Dao = require('./dao.js'); const config = require('./config.json'); const dao = new Dao(); const app = express(); app.use(HttpCat()); app.use(morgan('dev')); app.set('etag', false); app.use(bodyParser.json()); // before request handlers app.use(express.static('./')); app.get('/api/contacts', getContacts); app.get('/api/contacts/:id', getContact); app.post('/api/contacts', addContact); app.put('/api/contacts/:id', changeContact); app.post('/api/contacts/delete', deleteMultibleContacts); app.delete('/api/contacts/:id', deleteContact); app.use(errorHandler); // after request handlers dao.connect(config.mongoUrl) .then(() => { app.listen(3000, () => console.log('Server is running on port 3000')); }).catch(err => { console.log("MongoDB connection failed: "); console.log(err) process.exit(1); }) function errorHandler(error, request, response, next) { // there must be 4 arguments console.log(error) response.status(500).send('error: ' + error.toString()); } function getContacts(req, resp) { dao.findAll().then(data => resp.json(data)); } function getContact(req, resp) { var id = req.params.id; if (!ObjectID.isValid(id)) { resp.status(400).json("Invalid id"); return; } dao.findById(id).then(data => { resp.json(data); }).catch(err => { console.log(err); resp.status(404).json(err.toString()); }); } function addContact(req, resp, next) { var contact = req.body; if (contact.name == null ) { resp.status(400).json("Can't add a contact without a name"); return; } var sanitizedContact = { "name": contact.name, "phone": contact.phone } dao.insert(sanitizedContact).then((data) => { resp.status(201); resp.location("/api/contacts/"+data.insertedId); dao.findById(data.insertedId).then(insertedContact => { resp.json(insertedContact); }); }).catch(err => { console.log(err); next(err); }) } function changeContact(req, resp) { var id = req.params.id; var contact = req.body; if (!ObjectID.isValid(id)) { resp.status(400).json("Invalid id"); return; } if (contact.name == null) { resp.status(400).json("Contact must have a name"); return; } var sanitizedContact = { "name": contact.name, "phone": contact.phone } dao.update(id, sanitizedContact).then(data => { resp.status(200).end("Changed"); }).catch(err => { console.log(err); resp.status(404).end("Contact Not Found"); }); } function deleteContact(req, resp) { var id = req.params.id; if (!ObjectID.isValid(id)) { resp.status(400).json("Invalid id"); return; } dao.remove(id).then(data => { resp.status(200).json("Deleted"); }).catch(err => { console.log(err); resp.status(404).end("Contact Not Found"); }) } function deleteMultibleContacts(req, resp) { var contactIds = req.body; for (let id of contactIds) { if (!ObjectID.isValid(id)) { resp.status(400).json("Invalid ID in the list"); return; } } dao.removeMany(contactIds).then(data => { resp.status(200).json("Deleted"); }).catch(err => { console.log(err); resp.status(400).json("Failed to delete some or all of the contacts" + err.toString()); }) }