forked from sf-wdi-25/express_self_api
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathboardgamesController.js
More file actions
78 lines (67 loc) · 1.64 KB
/
boardgamesController.js
File metadata and controls
78 lines (67 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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);
}
res.json(newGame)
})
}
})
}
// Updates edited game
function update (req, res) {
db.Boardgame.findById(req.params.id, function(err, foundGame) {
foundGame.title = req.body.title,
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);
} 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,
}