homework/ng1/server.js

151 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-06-07 12:44:51 +03:00
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
2017-06-10 22:26:00 +03:00
const morgan = require('morgan');
2017-06-11 18:12:19 +03:00
const ObjectID = require('mongodb').ObjectID;
2017-06-07 12:44:51 +03:00
2017-06-11 18:12:19 +03:00
const HttpCat = require('./http-cat.js')
const Dao = require('./dao.js');
const config = require('./config.json');
const dao = new Dao();
2017-06-07 12:44:51 +03:00
const app = express();
2017-06-11 18:12:19 +03:00
app.use(HttpCat());
app.use(morgan('dev'));
2017-06-10 22:26:00 +03:00
app.set('etag', false);
2017-06-07 12:44:51 +03:00
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);
2017-06-11 18:12:19 +03:00
app.post('/api/contacts/delete', deleteMultibleContacts);
2017-06-07 12:44:51 +03:00
app.delete('/api/contacts/:id', deleteContact);
app.use(errorHandler); // after request handlers
2017-06-11 18:12:19 +03:00
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);
})
2017-06-07 12:44:51 +03:00
function errorHandler(error, request, response, next) { // there must be 4 arguments
2017-06-11 18:12:19 +03:00
console.log(error)
2017-06-07 12:44:51 +03:00
response.status(500).send('error: ' + error.toString());
}
function getContacts(req, resp) {
2017-06-11 18:12:19 +03:00
dao.findAll().then(data => resp.json(data));
2017-06-07 12:44:51 +03:00
}
function getContact(req, resp) {
var id = req.params.id;
2017-06-11 18:12:19 +03:00
if (!ObjectID.isValid(id)) {
resp.status(400).json("Invalid id");
return;
2017-06-07 12:44:51 +03:00
}
2017-06-11 18:12:19 +03:00
dao.findById(id).then(data => {
resp.json(data);
}).catch(err => {
console.log(err);
resp.status(404).json(err.toString());
});
2017-06-07 12:44:51 +03:00
}
2017-06-11 18:12:19 +03:00
function addContact(req, resp, next) {
2017-06-10 22:26:00 +03:00
var contact = req.body;
2017-06-11 18:12:19 +03:00
if (contact.name == null ) {
resp.status(400).json("Can't add a contact without a name");
return;
}
2017-06-11 00:24:59 +03:00
var sanitizedContact = {
2017-06-10 22:26:00 +03:00
"name": contact.name,
"phone": contact.phone
2017-06-11 00:24:59 +03:00
}
2017-06-10 22:26:00 +03:00
2017-06-11 18:12:19 +03:00
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);
})
2017-06-07 12:44:51 +03:00
}
function changeContact(req, resp) {
2017-06-10 22:26:00 +03:00
var id = req.params.id;
2017-06-11 18:12:19 +03:00
var contact = req.body;
2017-06-10 22:26:00 +03:00
2017-06-11 18:12:19 +03:00
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;
2017-06-10 22:26:00 +03:00
}
2017-06-11 18:12:19 +03:00
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");
});
2017-06-07 12:44:51 +03:00
}
function deleteContact(req, resp) {
2017-06-10 22:26:00 +03:00
var id = req.params.id;
2017-06-11 18:12:19 +03:00
if (!ObjectID.isValid(id)) {
resp.status(400).json("Invalid id");
return;
2017-06-10 22:26:00 +03:00
}
2017-06-11 18:12:19 +03:00
dao.remove(id).then(data => {
resp.status(200).json("Deleted");
}).catch(err => {
console.log(err);
resp.status(404).end("Contact Not Found");
})
2017-06-07 12:44:51 +03:00
}
2017-06-11 00:24:59 +03:00
2017-06-11 18:12:19 +03:00
function deleteMultibleContacts(req, resp) {
2017-06-11 00:24:59 +03:00
var contactIds = req.body;
2017-06-11 18:12:19 +03:00
for (let id of contactIds) {
if (!ObjectID.isValid(id)) {
resp.status(400).json("Invalid ID in the list");
return;
2017-06-11 00:24:59 +03:00
}
}
2017-06-11 18:12:19 +03:00
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());
})
2017-06-11 00:24:59 +03:00
}