-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperations.js
More file actions
115 lines (103 loc) · 2.52 KB
/
Copy pathoperations.js
File metadata and controls
115 lines (103 loc) · 2.52 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var mongoose = require('mongoose')
, async = require('async')
, Operation = mongoose.model('Operation')
, _ = require('underscore')
mongoose.set('debug', true)
// Find operation by id
exports.operation = function(req, res, next, id){
Operation.load(id, function (err, operation) {
if (err) return next(err)
if (!operation) return next(new Error('Failed to load operation ' + id))
req.operation = operation
next()
})
}
// GET /ops
exports.index = function(req, res) {
res.locals.path = req.path
var page = req.param('page') > 0 ? req.param('page') : 0
var perPage = 10
var options = {
perPage: perPage,
page: page
}
Operation.list(options, function(err, operations) {
if (err) return res.render('500')
Operation.count().exec(function (err, count) {
res.render('operations/index', {
title: 'Operations Manifest',
operations: operations,
page: page,
pages: count / perPage
})
})
})
}
// GET /ops/:id
exports.show = function(req, res){
res.locals.path = req.path
res.render('operations/show', {
title: 'Operation',
operation: req.operation
})
}
// GET /ops/new
exports.new = function(req, res){
res.locals.path = req.path
res.render('operations/new', {
title: 'Register an Operation',
operation: new Operation({})
})
}
// POST /ops
exports.create = function (req, res) {
res.locals.path = req.path
var operation = new Operation(req.body)
operation.save(function(err) {
if (err) {
res.render('operations/new', {
title: 'Register an Operation',
operation: operation,
errors: err.errors,
req: req.body
})
}
else {
res.redirect('/ops/' + operation._id)
}
})
}
// GET /ops/:id/edit
exports.edit = function (req, res) {
res.locals.path = req.path
res.render('operations/edit', {
title: 'Edit Operation',
operation: req.operation
})
}
// PUT /ops/:id
exports.update = function(req, res) {
res.locals.path = req.path
var operation = req.operation
operation = _.extend(operation, req.body)
operation.save(function (err) {
if (err) {
res.render('operations/edit', {
title: 'Edit Operation',
operation: operation,
errors: err.errors
})
}
else {
res.redirect('/ops/' + operation._id)
}
})
}
// DELETE /ops/:id
exports.destroy = function(req, res){
var operation = req.operation
operation.remove(function(err){
// req.flash('notice', 'Deleted successfully')
res.redirect('/ops')
})
}