-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (137 loc) · 3.7 KB
/
index.js
File metadata and controls
157 lines (137 loc) · 3.7 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require('dotenv').config();
const express = require('express');
const helmet = require('helmet');
const morgan = require('morgan');
const server = express();
const project = require('./data/models/project-model');
const action = require('./data/models/action-model');
server.use(helmet());
server.use(morgan('dev'));
server.use(express.json());
server.get('/api/projects', (req, res) => {
project
.get()
.then(projects => res.status(200).json({ success: true, projects }))
.catch(err =>
res.status(500).json({
success: false,
message: 'Unable to retrieve projects. Please try again.'
})
);
});
server.get('/api/project/:id', (req, res) => {
const id = req.params.id;
project
.getById(id)
.then(project => res.status(200).json({ success: true, project }))
.catch(err =>
res.status(500).json({
success: false,
message: 'Unable to retrieve project. Please try again.'
})
);
});
server.post('/api/project', (req, res) => {
const newProject = req.body;
const { name, description } = req.body;
if (!name || !description) {
res
.status(500)
.json({ success: false, message: 'Name and description required.' });
}
project
.add(newProject)
.then(project => res.status(200).json({ success: true, project }))
.catch(err =>
res.status(500).json({
success: false,
message: 'Unable to add project. Please try again.'
})
);
});
server.get('/api/actions', (req, res) => {
action
.get()
.then(actions => res.status(200).json({ success: true, actions }))
.catch(err =>
res.status(500).json({
success: false,
message: 'Unable to retrieve actions. Please try again.'
})
);
});
server.get('/api/actions/page/:pageNumber', (req, res) => {
const pageNumber = req.params.pageNumber;
action
.getByPage(pageNumber)
.then(actions => res.status(200).json({ success: true, actions }))
.catch(err =>
res.status(500).json({
success: false,
message: 'Unable to retrieve actions. Please try again.'
})
);
});
server.get('/api/action/:id', (req, res) => {
const id = req.params.id;
action
.getById(id)
.then(action => {
if (!action) {
res
.status(404)
.json({ success: false, message: 'No action found with that id' });
} else {
res.status(200).json({ success: true, action });
}
})
.catch(err =>
res.status(500).json({
success: false,
message: 'Unable to retrieve action. Please try again.'
})
);
});
server.post('/api/action', (req, res) => {
const newAction = req.body;
const { description, fk } = req.body;
if (!description || !fk) {
res
.status(500)
.json({ success: false, message: 'Name and description required.' });
}
action
.add(newAction)
.then(action => res.status(200).json({ success: true, action }))
.catch(err =>
res.status(500).json({
success: false,
message: 'Unable to add action. Please try again.'
})
);
});
server.delete('/api/project/:id', (req, res) => {
const id = req.params.id;
project
.remove(id)
.then(project => {
if (!project) {
res
.status(404)
.json({ success: false, message: 'No project found with that id' });
} else {
res.status(200).json({ success: true });
}
})
.catch(
res.status(500).json({
success: false,
message:
'Unable to delete project. This project may have associated actions.'
})
);
});
const port = process.env.PORT || 4000;
server.listen(port, () => {
console.log('*** Server running on 4000 ***');
});