Skip to content

Commit

Permalink
Extra routing deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Koronos committed Mar 14, 2015
1 parent 827b571 commit 059bacb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions API-Rest-Example.njsproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="app.js" />
<Compile Include="db\controllers\videoGamesController.js" />
<Compile Include="db\models\VideoGamesModel.js" />
<Compile Include="routes\index.js" />
<Compile Include="routes\users.js" />
Expand All @@ -45,6 +46,7 @@
<Folder Include="bin\" />
<Folder Include="db\" />
<Folder Include="db\models\" />
<Folder Include="db\controllers\" />
<Folder Include="public\" />
<Folder Include="public\images\" />
<Folder Include="public\javascripts\" />
Expand Down
11 changes: 6 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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');
Expand Down
70 changes: 70 additions & 0 deletions db/controllers/videoGamesController.js
Original file line number Diff line number Diff line change
@@ -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);
})
});
};
1 change: 1 addition & 0 deletions db/models/VideoGamesModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 059bacb

Please sign in to comment.