From 023a5e12ddcbc3cfedab5d3878b3432f7caaa41f Mon Sep 17 00:00:00 2001 From: Koronos Date: Mon, 16 Mar 2015 16:55:22 -0600 Subject: [PATCH] Moved all Appi to API folder --- API-Rest-Example.njsproj | 11 ++--- app.js | 10 ++--- video_games_api/appVideoGames.js | 22 ++++++++++ .../controllers/videoGamesController.js | 40 +++++++++---------- .../models/VideoGamesModel.js | 4 +- 5 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 video_games_api/appVideoGames.js rename {db => video_games_api}/controllers/videoGamesController.js (62%) rename {db => video_games_api}/models/VideoGamesModel.js (86%) diff --git a/API-Rest-Example.njsproj b/API-Rest-Example.njsproj index e7616c9..e37eebe 100644 --- a/API-Rest-Example.njsproj +++ b/API-Rest-Example.njsproj @@ -37,16 +37,17 @@ - - + + + - - - + + + diff --git a/app.js b/app.js index 877ed90..c4e4c8b 100644 --- a/app.js +++ b/app.js @@ -6,11 +6,8 @@ var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var mongoose = require("mongoose"); -// Connection to DB -mongoose.connect('mongodb://localhost/videogames', function (err, res) { - if (err) throw err; - console.log('Connected to Database'); -}); +//Routes +var videoGamesAPI = require("./video_games_api/appVideoGames.js"); var app = express(); @@ -26,6 +23,9 @@ app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); +//adding routes +app.use("/VideoGames", videoGamesAPI.videoGamesRoutes); + // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); diff --git a/video_games_api/appVideoGames.js b/video_games_api/appVideoGames.js new file mode 100644 index 0000000..24cb2cb --- /dev/null +++ b/video_games_api/appVideoGames.js @@ -0,0 +1,22 @@ +var mongoose = require("mongoose"); + +var videoGamesController = require("./controllers/videoGamesController.js"); + +var router = require("express").Router(); + +// Connection to DB +mongoose.connect('mongodb://localhost/videogames', function (err, res) { + if (err) throw err; + console.log('Connected to Database'); +}); + +router.route("/videoGames") +.get(videoGamesController.findAllVideoGames) +.post(videoGamesController.addVideoGame); + +router.route("/videoGames/:id") +.get(videoGamesController.findById) +.put(videoGamesController.updateVideoGame) +.delete(videoGamesController.deleteVideoGame); + +exports.videoGamesRoutes = router; \ No newline at end of file diff --git a/db/controllers/videoGamesController.js b/video_games_api/controllers/videoGamesController.js similarity index 62% rename from db/controllers/videoGamesController.js rename to video_games_api/controllers/videoGamesController.js index ea44762..a7031c9 100644 --- a/db/controllers/videoGamesController.js +++ b/video_games_api/controllers/videoGamesController.js @@ -1,10 +1,11 @@ var mongoose = require('mongoose'); -var videogame = mongoose.model('VideoGames'); +var videogame = require('./../models/VideoGamesModel.js'); //GET - Return all videogames in the DB -exports.findAllvideogames = function (req, res) { +exports.findAllVideoGames = function (req, res) { videogame.find(function (err, videogames) { - if (err) res.send(500, err.message); + if(err) + res.send(500, err.message); console.log('GET /VideoGames') res.status(200).jsonp(videogames); @@ -22,33 +23,29 @@ exports.findById = function (req, res) { }; //POST - Insert a new videogame in the DB -exports.addvideogame = function (req, res) { +exports.addVideoGame = function (req, res) { console.log('POST'); console.log(req.body); - - var videogame = new videogame({ + + var game = 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); + + game.save(function (err, game) { + if (err) + return res.status(500).body(err.message); + res.status(200).jsonp(game); }); }; //PUT - Update a register already exists -exports.updatevideogame = function (req, res) { +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; @@ -60,11 +57,10 @@ exports.updatevideogame = function (req, res) { }; //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); - }) +exports.deleteVideoGame = function (req, res) { + videogame.findByIdAndRemove(req.params.id, function (err) { + if (err) return res.send(500, err.message); + res.status(200); + res.end(); }); }; \ No newline at end of file diff --git a/db/models/VideoGamesModel.js b/video_games_api/models/VideoGamesModel.js similarity index 86% rename from db/models/VideoGamesModel.js rename to video_games_api/models/VideoGamesModel.js index 964ef01..1c4fda3 100644 --- a/db/models/VideoGamesModel.js +++ b/video_games_api/models/VideoGamesModel.js @@ -4,12 +4,10 @@ var Schema = mongoose.Schema; var videoGamesSchema = new Schema({ title: { type: String }, year: { type: Number }, - country: { type: String }, - poster: {type: String}, plataform: { type: String }, genre: { type: String, enum: - ["FPS", "Adventures", "RPG", "MOBA", "MORPG", "Sports", "Rancing"] + ["FPS", "Adventures", "RPG", "MOBA", "MORPG", "Sports", "Rancing","Estrategy"] }, summary: {type: String} });