Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
**/node_modules

**.DS_Store
73 changes: 73 additions & 0 deletions controllers/boardgamesController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@


var db = require('../models');


// function for initial page render
function index (req, res) {
// console.log('Reached index controller');
db.Boardgame.find({}, function (err, allGames) {
if (err) {
console.log('error from controller index', err);
}
res.json(allGames);
})
}

// Creates new board game
function create (req, res) {
console.log('Reached create controller', req.body);
db.Boardgame.create(req.body, function(err, game) {
if (err) {
console.log('ERROR from controller create', err);
}
if (game.image) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could think about doing this before create, so that you only have a single database transaction:

if (!req.body.image) {
  req.body.image = 'images/generic-game-image.jpg';
}
db.Boardgame.create(...

res.json(game);
} else {
game.image = 'images/generic-game-image.jpg';
game.save(function(err, newGame) {
if (err) {
console.log("ERROR from update controller", err);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is clearly copy-pasted from update, with now-incorrect comments

}
res.json(newGame)
})
}
})
}


// Updates edited game
function update (req, res) {
console.log('reached update', req.body);
db.Boardgame.findById(req.params.id, function(err, foundGame) {
foundGame.title = req.body.title,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commas 😭 these should each be their own line of code, with a semicolon

foundGame.description = req.body.description,
foundGame.playtime = req.body.playtime,
foundGame.players = req.body.players,
foundGame.save(function(err, savedGame) {
if (err) {
console.log("ERROR from update controller", err);
}
res.json(savedGame)
})
})
}

function destroy (req, res) {
console.log("destroy controller reached");
db.Boardgame.findByIdAndRemove(req.params.id, function(err, deletedGame) {
if (err) {
console.log(err);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if there's an error, would be nice to send back some response

} else {
res.json(deletedGame);
}
})
}


module.exports = {
index: index,
create: create,
update: update,
destroy: destroy,
}
3 changes: 3 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
boardgames: require('./boardgamesController.js'),
}
14 changes: 14 additions & 0 deletions models/boardgame.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require('mongoose'),
Schema = mongoose.Schema;

const BoardgameSchema = new Schema({
title: String,
description: String,
playtime: String,
players: String,
image: String,
});

const Boardgame = mongoose.model('Boardgame', BoardgameSchema);

module.exports = Boardgame;
2 changes: 1 addition & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ var mongoose = require("mongoose");
mongoose.connect( process.env.MONGODB_URI || "mongodb://localhost/personal-api", {useMongoClient: true});
mongoose.Promise = global.Promise; // use native Promise

// module.exports.Campsite = require("./campsite.js.example");
module.exports.Boardgame = require("./boardgame.js");
Loading