Web App: Cloud
API Challenge Solution

Notes

This will represent a note!

This will represent a hint!

This will represent a warning!

The solutions to the Web API challenges are below. We've hidden them just to add that one extra button click before you see the solution. Try to implement them on your own first! Remember that you can test each route by deploying to Heroku and using Postman to make the request. Make sure that you send the body of the request as raw JSON, like in the image (testing the edit route for stats).

Settings: GET

exports.getSettings = function(req, res) {
db.Settings.findOne({})
.then( function(settings) {
res.json(settings);
})
.catch( function(err) {
res.send(err);
});
}

Settings: PUT

exports.editSettings = function(req, res) {
db.Settings.findOneAndUpdate({}, req.body, {'new': true, upsert: true})
.then( function(editedSettings) {
res.json(editedSettings);
})
.catch( function(err) {
res.send(err);
});
}

Stats: GET

exports.getStats = function(req, res) {
db.Stats.findOne({})
.then( function(stats) {
res.json(stats);
})
.catch( function(err) {
res.send(err);
});
}

Stats: PUT

exports.editStats = function(req, res) {
db.Stats.findOneAndUpdate({}, req.body, {'new': true, upsert: true})
.then( function(editedStats) {
res.json(editedStats);
})
.catch( function(err) {
res.send(err);
});
}

helpers/settings.js

var db	= require('../models');

exports.getSettings = function(req, res) {
db.Settings.findOne({})
.then( function(settings) {
res.json(settings);
})
.catch( function(err) {
res.send(err);
});
}

exports.editSettings = function(req, res) {
db.Settings.findOneAndUpdate({}, req.body, {'new': true, upsert: true})
.then( function(editedSettings) {
res.json(editedSettings);
})
.catch( function(err) {
res.send(err);
});
}
								

helpers/stats.js

var db	= require('../models');

exports.getStats = function(req, res) {
db.Stats.findOne({})
.then( function(stats) {
res.json(stats);
})
.catch( function(err) {
res.send(err);
});
}

exports.editStats = function(req, res) {
db.Stats.findOneAndUpdate({}, req.body, {'new': true, upsert: true})
.then( function(editedStats) {
res.json(editedStats);
})
.catch( function(err) {
res.send(err);
});
}

exports.resetStats = function(req, res) {
var blank = {
'avgTemperature': 0,
'avgHumidity': 0,
'avgBrightness': 0,
'timeInHot': 0,
'timeInCold': 0,
'timeInDry': 8,
'timeInHumid': 0,
'timeOn': 0,
'timeTotal': 0
}

db.Stats.findOneAndUpdate({}, blank, {'new': true, upsert: true})
.then (function(wipedStats) {
res.json(wipedStats);
})
.catch( function(err) {
res.send(erR);
});
}

helpers/data.js

var db	= require('../models');


exports.getData = function(req, res) {
db.Data.find()
.then( function(data) {
res.json(data);
})
.catch( function(err) {
res.send(err);
})
}

exports.createData = function(req, res) {
db.Data.create(req.body)
.then (function(newData) {
res.json(newData);
})
.catch ( function(err) {
res.send(err);
});
}

exports.deleteData = function(req, res) {
db.Data.remove({})
.then (function() {
res.json({message: 'Data wiped' });
})
.catch (function(err) {
res.send(err);
});
}

exports.getOneData = function(req, res) {
db.Data.findById(req.params.id)
.then (function(foundData) {
res.json(foundData);
})
.catch( function(err) {
res.send(err);
});
}

exports.editData = function(req, res) {
db.Data.findOneAndUpdate({ _id: req.params.id}, req.body, {'new': true, upsert: true})
.then (function(editedData) {
res.json(editedData);
})
.catch( function(err) {
res.send(err);
});
}

exports.deleteOneData = function(req, res) {
db.Data.remove( {_id: req.params.id})
.then( function() {
res.json( {message: 'Succesfully deleted'});
})
.catch( function(err) {
res.send(err);
});
}

routes/api.js

var express		= require('express'),
router		= express.Router(),
db			= require('../models'),
dataHelpers		= require('../helpers/data'),
statsHelpers	= require('../helpers/stats'),
settingsHelpers	= require('../helpers/settings');


/*****
* C - CREATE
* R - READ
* U - UPDATE
* D - DELETE
*****/

router.route('/data')
.get(dataHelpers.getData)
.post(dataHelpers.createData)
.delete(dataHelpers.deleteData);

router.route('/data/:id')
.get(dataHelpers.getOneData)
.put(dataHelpers.editData)
.delete(dataHelpers.deleteOneData);

router.route('/settings')
.get(settingsHelpers.getSettings)
.put(settingsHelpers.editSettings);

router.route('/statistics')
.get(statsHelpers.getStats)
.put(statsHelpers.editStats)
.delete(statsHelpers.resetStats);

module.exports = router;