-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
227 lines (197 loc) · 6.41 KB
/
server.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// require express and other modules
const express = require('express');
const app = express();
// parse incoming urlencoded form data
// and populate the req.body object
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
// allow cross origin requests (optional)
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
/************
* DATABASE *
************/
const db = require('./models');
/**********
* ROUTES *
**********/
// Serve static files from the `/public` directory:
// i.e. `/images`, `/scripts`, `/styles`
app.use(express.static('public'));
/*
* HTML Endpoints
*/
app.get('/', function homepage(req, res) {
res.sendFile(__dirname + '/views/index.html');
});
app.get('/add', function addPage(req, res) {
res.sendFile(__dirname + '/views/add.html');
});
app.get('/profile', function addPage(req, res) {
res.sendFile(__dirname + '/views/profile.html');
});
/*
* JSON API Endpoints
*/
app.get('/api', (req, res) => {
// all api endpoints are in the json object below
res.json({
appTitle: "HackerJedi!",
appContributors: "Chike, Siri, and Heggy",
message: "this documents all api endpoints!",
GitHubLink: "CHANGE THIS", // CHANGE ME
HerokuLink: "http://YOUR-APP-NAME.herokuapp.com", // CHANGE ME
endpoints: [
//2 pages for
{method: "GET", path: "/", description: "User lands and starts searching for apprenticeship."},
{method: "GET", path: "/add", description: "User adds new apprenticeship."},
//server side
{method: "GET", path: "/api", description: "Describes all available endpoints"},
{method: "GET", path: "/api/apprenticeships", description: "View all apprenticeships here." },
{method: "GET", path: "api/apprenticeships/:keyword", description: "Search keyword"},
{method: "POST", path: "/api/apprenticeships", description: "Create new apprenticeship/opportunity, post it to this api"},
{method: "PUT", path: "/api/apprenticeships/:id", description: "Edit an apprenticeship and update it"},
{method: "DELETE", path: "/api/apprenticeships/:id", description: "Delete an apprenticeship that's no longer available/relevant"},
//for later: search through opportunities
//maybe we can add algolia.com's search bar plugin
//or create our own search queries.
]
})
});
//THIS CODE REMOVES ALL USERS
app.delete('/user', function (req, res) {
console.log('deleted all users');
db.User.collection.remove()
})
//THIS CODE REMOVES ALL USERS
app.delete('/api/add', function (req, res) {
console.log('deleted all appr');
db.Apprenticeship.collection.remove()
})
app.get('/user', (req, res) => {
db.User.find({}, function (err, user) {
if (err) {
console.log("error: " + err);
}
res.json(user);
});
//want to display user info here after user logs in
})
app.delete('/user/:id', function (req, res) {
console.log('deleted user ID is ', req.params);
db.User.findOneAndDelete
( {_id: req.params.id},
(err, deletedUser) => {
if (err) {
console.log("the error is " + err);
}
res.json(deletedUser);
});
})
app.get('/api/add', (req, res) => {
db.Apprenticeship.find({}, function (err, apprenticeship) {
if (err) {
console.log("error: " + err);
}
res.json(apprenticeship);
});
})
app.post('/user', (req, res) => {
let newUser = req.body
db.User.create(newUser,(err,createdUser)=>{
if (err){
res.send(err)
}
res.json(createdUser)
})
});
app.get('/user/:email', (req, res) => {
let email = req.params.email
db.User.findOne({email: email})
.exec((err,foundUser)=>{
if (err){
res.send(err)
}
res.json(foundUser)
})
});
//Heggy's
// Creat new apprenticeship
app.post('/api/add', (req, res) => {
// create new apprenticeship with form data ('req.body')
console.log("body is ", req.body);
let newApprenticeship = new db.Apprenticeship({
company: req.body.company,
city: req.body.city,
url: req.body.url,
description: req.body.description,
email: req.body.email,
user_created: null,
});
// find the user from req.body
db.User.findOne({email: req.body.email}, (err, user) => {
if (err) {
return console.log(err);
}
// if that user doesn't exist yet, create a new one
if (user === null) {
db.User.create({name: req.body.user, email: req.body.email}, (err, newUser) => {
if (err) {
return console.log(err);
}
createApprenticeshipWithUser(newApprenticeship, newUser, res);
});
} else {
createApprenticeshipWithUser(newApprenticeship, user, res);
}
});
// save new Apprentice
});
// create Apprenticeship With new User function
function createApprenticeshipWithUser(apprenticeship, user, res) {
// add this user to the apprenticeship
apprenticeship.user_created = user;
// save new apprenticeship to database
apprenticeship.save(function(err, book) {
if (err) {
return console.log("save error: " + err)
}
console.log("saved ", apprenticeship.company);
// send back the apprenticeship
res.json(apprenticeship);
});
}
// learning tip: since we have :id (this variable we pass) in parameter
// we have to pass req.params instead of req.body
app.put('/api/add/:id', function(req,res){
// ^^^ get the id of the apprenticeship
//need to also get the user id from the frontend (login)
console.log('updated apprenticeships: ', req.params);
db.Apprenticeship.findOneAndUpdate({ _id: req.params.id},req.body,{new: true})
.populate('user_created')
.exec( (err, apprenticeship) => {
if (err) {
console.log("the error is " + err);
}
res.json(apprenticeship);
});
});
app.delete('/api/add/:id', function (req, res) {
console.log('deleted apprenticeship is ', req.params);
db.Apprenticeship.findOneAndDelete
( {_id: req.params.id},
(err, deletedApprenticeship) => {
if (err) {
console.log("the error is " + err);
}
res.json(deletedApprenticeship);
});
})
// process.env.PORT (this will be set, dynamically, by Heroku)
app.listen(process.env.PORT || 3000, ()=> {
console.log('listening on port 3000');
});