Criação de uma API Bio

// Import Bio Model
Bio = require ('./RegistoModel');
// For index
exports.index = function (req, res) {
Bio.get (function (err, bio) {
if (err)
res.json ({
status: "error",
message: err
});
res.json ({
status: "OK",
message: "Bios Successfully Obtained",
date: bio
});
});
};
// Create new BIO
exports.add = function (req, res) {
var bio = new Bio ();
bio.name = req.body.name? req.body.name: bio.name;
bio.email = req.body.email;
bio.telef = req.body.telef;
bio.morada = req.body.morada;
// Save and check for errors
bio.save (function (err) {
if (err)
res.json (err);
res.json ({
message: "New Bio Added!",
date: bio
});
});
};
// See Bio
exports.view = function (req, res) {
Bio.findById (req.params.bio_id, function (err, bio) {
if (err)
res.send (err);
res.json ({
message: 'Bio details',
date: bio
});
});
};
// Update Bio
exports.update = function (req, res) {
Bio.findById (req.params.bio_id, function (err, bio) {
if (err)
res.send (err);
bio.name = req.body.name? req.body.name: bio.name;
bio.email = req.body.email;
bio.telef = req.body.telef;
bio.morada = req.body.morada;
// Save and check for errors
bio.save (function (err) {
if (err)
res.json (err)
res.json ({
message: "Bio Updated Successfully",
date: bio
});
});
});
};
// Delete Bio
exports.delete = function (req, res) {
Bio.deleteOne ({
_id: req.params.bio_id
}, function (err, contact) {
if (err)
res.send (err)
res.json ({
status: "OK",
message: 'Bio Eliminated!'
});
});
};

Testar a API:

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
João Saraiva

João Saraiva

More from Medium

Knapp’s Sprint — A Retrospective

Why World Need a Network Based Conference Management Platform

APOCALYPSE GO: A Mobile Game

I’ve been taking some time out over the summer, creating memories with my boys before they start…