From 059bacb34ad6b0f6331ce71cd6147c80d1813b9e Mon Sep 17 00:00:00 2001 From: Koronos Date: Sat, 14 Mar 2015 08:06:20 -0600 Subject: [PATCH] Extra routing deleted --- API-Rest-Example.njsproj | 2 + app.js | 11 ++-- db/controllers/videoGamesController.js | 70 ++++++++++++++++++++++++++ db/models/VideoGamesModel.js | 1 + 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 db/controllers/videoGamesController.js diff --git a/API-Rest-Example.njsproj b/API-Rest-Example.njsproj index 58cc30a..e7616c9 100644 --- a/API-Rest-Example.njsproj +++ b/API-Rest-Example.njsproj @@ -37,6 +37,7 @@ + @@ -45,6 +46,7 @@ + diff --git a/app.js b/app.js index 01ad749..9dc9f41 100644 --- a/app.js +++ b/app.js @@ -4,9 +4,13 @@ var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); +var mongoose = require("mongoose"); -var routes = require('./routes/index'); -var users = require('./routes/users'); +// Connection to DB +mongoose.connect('mongodb://localhost/tvshows', function (err, res) { + if (err) throw err; + console.log('Connected to Database'); +}); var app = express(); @@ -22,9 +26,6 @@ app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); -app.use('/', routes); -app.use('/users', users); - // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); diff --git a/db/controllers/videoGamesController.js b/db/controllers/videoGamesController.js new file mode 100644 index 0000000..ea44762 --- /dev/null +++ b/db/controllers/videoGamesController.js @@ -0,0 +1,70 @@ +var mongoose = require('mongoose'); +var videogame = mongoose.model('VideoGames'); + +//GET - Return all videogames in the DB +exports.findAllvideogames = function (req, res) { + videogame.find(function (err, videogames) { + if (err) res.send(500, err.message); + + console.log('GET /VideoGames') + res.status(200).jsonp(videogames); + }); +}; + +//GET - Return a videogame with specified ID +exports.findById = function (req, res) { + videogame.findById(req.params.id, function (err, videogame) { + if (err) return res.send(500, err.message); + + console.log('GET /videogame/' + req.params.id); + res.status(200).jsonp(videogame); + }); +}; + +//POST - Insert a new videogame in the DB +exports.addvideogame = function (req, res) { + console.log('POST'); + console.log(req.body); + + var videogame = new videogame({ + title: req.body.title, + year: req.body.year, + country: req.body.country, + poster: req.body.poster, + seasons: req.body.seasons, + genre: req.body.genre, + summary: req.body.summary + }); + + videogame.save(function (err, videogame) { + if (err) return res.send(500, err.message); + res.status(200).jsonp(videogame); + }); +}; + +//PUT - Update a register already exists +exports.updatevideogame = function (req, res) { + videogame.findById(req.params.id, function (err, videogame) { + videogame.title = req.body.petId; + videogame.year = req.body.year; + videogame.country = req.body.country; + videogame.poster = req.body.poster; + videogame.genre = req.body.genre; + videogame.summary = req.body.summary; + + videogame.save(function (err) { + if (err) return res.send(500, err.message); + res.status(200).jsonp(videogame); + }); + }); +}; + +//DELETE - Delete a videogame with specified ID +exports.deletevideogame = function (req, res) { + videogame.findById(req.params.id, function (err, videogame) { + videogame.remove(function (err) { + if (err) return res.send(500, err.message); + res.status(200); + }) + }); +}; \ No newline at end of file diff --git a/db/models/VideoGamesModel.js b/db/models/VideoGamesModel.js index 8dea705..964ef01 100644 --- a/db/models/VideoGamesModel.js +++ b/db/models/VideoGamesModel.js @@ -5,6 +5,7 @@ var videoGamesSchema = new Schema({ title: { type: String }, year: { type: Number }, country: { type: String }, + poster: {type: String}, plataform: { type: String }, genre: { type: String, enum: