-
Notifications
You must be signed in to change notification settings - Fork 54
Connor's Personal API #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1267fb2
543f783
e9a66c1
979352c
52f3c11
fa1c772
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| **/node_modules | ||
|
|
||
| **.DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
|
|
||
|
|
||
| var db = require('../models'); | ||
|
|
||
|
|
||
| // function for initial page render | ||
| function index (req, res) { | ||
| 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) { | ||
| db.Boardgame.create(req.body, function(err, game) { | ||
| if (err) { | ||
| console.log('ERROR from controller create', err); | ||
| } | ||
| if (game.image) { | ||
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
| db.Boardgame.findById(req.params.id, function(err, foundGame) { | ||
| foundGame.title = req.body.title, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| // Deletes specified game | ||
| function destroy (req, res) { | ||
| db.Boardgame.findByIdAndRemove(req.params.id, function(err, deletedGame) { | ||
| if (err) { | ||
| console.log(err); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Shows a single game in JSON | ||
| function show (req, res) { | ||
| db.Boardgame.findById(req.params.id, function (err, game) { | ||
| res.json(game); | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| module.exports = { | ||
| index: index, | ||
| create: create, | ||
| update: update, | ||
| destroy: destroy, | ||
| show: show, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module.exports = { | ||
| boardgames: require('./boardgamesController.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; |
This file was deleted.
There was a problem hiding this comment.
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: