This file contains hidden or 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' | |
var mongoose = require('mongoose'); | |
var AlbumSchema = new mongoose.Schema({ | |
title: String, | |
band: String, | |
genre: String, | |
year: String, | |
}); |
This file contains hidden or 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
function deleteAlbum(req, res){ | |
var albumId = req.params.id; | |
Album.findById(albumId, function(err, album){ | |
if(err){ | |
res.status(500).send({message: 'Error 500 al devolver el album'}); | |
}else{ | |
if(album){ | |
album.remove(err => { | |
if(err){ |
This file contains hidden or 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
function updateAlbum(req, res){ | |
var params = req.body; | |
var albumId = req.params.id; | |
Album.findByIdAndUpdate(albumId, params, function(err, album){ | |
if(err){ | |
res.status(500).send({message: 'Error 500 al actualizar el album'}); | |
}else{ | |
res.status(200).send(album); | |
} |
This file contains hidden or 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
function saveAlbum(req, res){ | |
var album = new Album(); | |
var params = req.body; | |
album.title = params.title; | |
album.band = params.band; | |
album.genre = params.genre; | |
album.year = params.year; | |
album.save(function(err, albumStored){ |
This file contains hidden or 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
var Album = require('../models/album'); | |
function getAlbum(req, res){ | |
var albumId = req.params.id; | |
Album.findById(albumId, function(err, album) { | |
if(err) { | |
res.status(500).send({message: 'Error 500 al devolver el album'}); | |
}else{ | |
if(!album){ |
This file contains hidden or 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' | |
var mongoose = require('mongoose'); | |
var app = require('./app'); | |
var port = process.env.PORT || 3000; | |
// Puerto por defecto de mongodb | |
mongoose.connect('mongodb://localhost:27017/base-de-datos-de-prueba', function(err, res){ | |
if(err){ | |
throw err; |
This file contains hidden or 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' | |
var express = require('express'); | |
var AlbumController = require('../controllers/album'); | |
var api = express.Router(); | |
api.get('/album/:id', AlbumController.getAlbum); | |
api.get('/album', AlbumController.getAlbums); | |
api.post('/album', AlbumController.saveAlbum); | |
api.put('/album', AlbumController.updateAlbum); |
This file contains hidden or 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' | |
function getAlbum(req, res){ | |
var id = req.params.id; | |
res.status(200).send({ | |
data: id, | |
}); | |
} | |
function getAlbums(req, res){ |
This file contains hidden or 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' | |
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var app = express(); | |
var api = require('./routes/favorito'); | |
// Convierte una petición recibida (POST-GET...) a objeto JSON | |
app.use(bodyParser.urlencoded({extended:false})); |
This file contains hidden or 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' | |
var app = require('./app'); | |
var port = process.env.PORT || 3000; | |
app.listen(port, () => { | |
console.log(`Server running in http://localhost:${port}`); | |
console.log('Defined routes:'); | |
console.log(' [GET] http://localhost:3000/'); | |
}); |