Repository navigation
Expand file tree
/
Copy pathstudentserver.js
More file actions
402 lines (398 loc) · 14.6 KB
/
Copy pathstudentserver.js
File metadata and controls
402 lines (398 loc) · 14.6 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//studentserver.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static('./public'));
const mongodb = require('mongodb');
const MongoClient = require('mongodb').MongoClient;
var uri = "mongodb://127.0.0.1:27017/";
const dbconn = mongodb.MongoClient.connect(uri);
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerDefinition = {
openapi: '3.0.0',
info: {
title: 'Express API for StudentServer',
version: '1.0.0',
description:
'This is a REST API application made with Express. It retrieves data from StudentServer.',
contact: {
name: 'Jarold Sabillon',
},
},
servers: [
{
url: 'http://localhost:5678',
description: 'Student Server',
},
],
}; //setting up swagger express documentation
const options = {
swaggerDefinition,
// Paths to files containing OpenAPI definitions
apis: ['studentserver.js'],
};
const swaggerSpec = swaggerJSDoc(options);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
/**
* @swagger
* /students:
* post:
* summary: Creates a new student record
* description: Creates new student record and checks for duplicates.
* Information includes ID, student name, gpa, and enrollment status.
* requestBody:
* required: true
* content:
* application/x-www-form-urlencoded:
* schema:
* type: object
* properties:
* first_name: # <!--- form field name
* type: string
* last_name: # <!--- form field name
* type: string
* gpa: # <!--- form field name
* type: integer
* enrolled: # <!--- form field name
* type: bool
*
*
*
* responses:
* 200:
* description: Student information.
* content:
* application/json:
* schema:
* type: object
* properties:
* data:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* description: The student ID.
* example: 123456788
*
* status:
* type: string
* description: status update.
* example: Successfull/failed
*
*
*/
app.post('/students', function (req, res) { //Creates new student
var record_id = new Date().getTime();
var obj = {};
var rsp_obj = {};
obj._id = record_id;
obj.first_name = req.body.first_name;
obj.last_name = req.body.last_name;
obj.gpa = req.body.gpa;
obj.enrolled = req.body.enrolled; //Input parameters
obj.fullname = req.body.first_name + req.body.last_name;
var name = req.body.first_name + req.body.last_name;
const filter = {
first_name: obj.first_name,
last_name: obj.last_name
}
function post() {
MongoClient.connect(uri, { useUnifiedTopology: true }, function (err, client) {
var dbo = client.db("StudentServer");
dbo.collection("Students").find(filter).toArray(function (err, result) { //filters search by first and last name
if (result.length <= 0) {
dbconn.then(function (db) {
db.db("StudentServer").collection("Students").insertOne(obj); //Creates student record with parameters
})
rsp_obj.record_id = record_id;
rsp_obj.message = 'successfully created'; //Response message
str = JSON.stringify(rsp_obj, null, 2);
console.log("Created");
} else if (result.length > 0) {
rsp_obj.record_id = -1;
rsp_obj.message = 'error - Student Already Exists';
rsp_obj.name = name; //Response message
str = JSON.stringify(rsp_obj, null, 2);
console.log("Student Exists");
}
return res.status(201).send(str);
});
});
}
post();
}); //end post method
/**
* @swagger
* /students/{id}:
* get:
* summary: Retrieve student record.
* description: Retrieve student record by ID or name.
* Information includes ID, student name, gpa, and enrollment status.
* parameters:
* - in: path
* name: id
* required: true
* description: Numeric ID or name of the student to retrieve.
* schema:
* type: string
* responses:
* 200:
* description: Student information.
* content:
* application/json:
* schema:
* type: object
* properties:
* data:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* description: The student ID.
* example: 123456788
* first_name:
* type: string
* description: The students first name.
* example: Jarold
* last_name:
* type: integer
* description: The students last name.
* example: Sabillon
* gpa:
* type: string
* description: The students gpa.
* example: 4.00
* enrollment:
* type: boolean
* description: The students enrollment status.
* example: true
* full_name:
* type: string
* description: The students full name.
* example: JaroldSabillon
*
*
*/
app.get('/students/:record_id', function (req, res) {
var record_id = req.params.record_id;
var obj = {};
var rsp_obj = {}; //Input for parameters
obj._id = record_id;
var id = parseInt(req.params.record_id);
function get() {
const filter = {
$or: [{ first_name: record_id }, { last_name: record_id }, { _id: id }, { fullname: record_id }] //Filter search
}
MongoClient.connect(uri, { useUnifiedTopology: true }, function (err, client) {
var dbo = client.db("StudentServer");
if (record_id === "null") {
dbo.collection("Students").find().toArray(function (err, results) { //Finds all students
str = JSON.stringify(results, null, 2);
console.log("Retrieved All");
client.close(); //response message
return res.status(201).send(str);
});
}
else {
dbo.collection("Students").find(filter).toArray(function (err, result) { //Gets specific student if not null
console.log(result.length);
if (result.length > 0) {
rsp_obj.record_id = record_id;
rsp_obj.message = 'successfully created'; //message if found
console.log(result);
str = JSON.stringify(result, null, 2);
console.log(str);
console.log("found");
} else if (result.length <= 0) {
rsp_obj.record_id = -1;
rsp_obj.message = 'Student not found'; //Message if not found
str = JSON.stringify(rsp_obj, null, 2);
console.log("Not found");
}
return res.status(201).send(str);
});
}
});
}
get();
});
/**
* @swagger
* /students/{id}:
* put:
* summary: Update student record
* description: Update student record.
* Can change student first name, last name, gpa, and enrollment status
* parameters:
* - in: path
* name: id
* required: true
* description: ID of student to update
* schema:
* type: string
* requestBody:
* required: true
* content:
* application/x-www-form-urlencoded:
* schema:
* type: object
* properties:
* first_name: # <!--- form field name
* type: string
* last_name: # <!--- form field name
* type: string
* gpa: # <!--- form field name
* type: integer
* enrolled: # <!--- form field name
* type: bool
* responses:
* 200:
* description: Student information.
* content:
* application/json:
* schema:
* type: object
* properties:
* data:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* description: The student ID.
* example: 123456788
* Status:
* type: string
* description: Status update.
* example: Successful/Failed
*
*/
app.put('/students/:record_id', function (req, res) { //updates student record
var record_id = req.params.record_id;
var obj = {};
obj.record_id = record_id;
obj.first_name = req.body.first_name;
obj.last_name = req.body.last_name;
obj.gpa = req.body.gpa; //Input to update student
obj.enrolled = req.body.enrolled;
obj.fullname = req.body.first_name + req.body.last_name;
var id = parseInt(req.params.record_id);
console.log(id);
const filter = { "_id": id };
const value = { $set: { first_name: req.body.first_name, last_name: req.body.last_name, gpa: req.body.gpa, enrolled: req.body.enrolled, fullname: req.body.first_name + req.body.last_name } };
MongoClient.connect(uri, { useUnifiedTopology: true }, function (err, client) {
var dbo = client.db("StudentServer");
dbo.collection("Students").find(filter).toArray(function (err, result) {
if (result.length > 0) {
if (err) throw err;
var dbo = client.db("StudentServer");
dbo.collection("Students").updateOne(filter, value, function (err, result) { //Updates file if found
if (err) throw err;
var rsp_obj = {};
rsp_obj.record_id = record_id;
rsp_obj.message = "Succesfully Updated";
str = JSON.stringify(rsp_obj, null, 2);
client.close(); //message if found
console.log(result.modifiedCount);
return res.status(201).send(str);
});
} else if (result.length <= 0) {
var rsp_obj = {};
rsp_obj.record_id = -1;
rsp_obj.message = 'Student not found'; //error if student not found
str = JSON.stringify(rsp_obj, null, 2);
console.log("Student not found");
return res.status(201).send(str);
}
});
});
});
/**
* @swagger
* /students/{id}:
* delete:
* summary: Delete student record
* description: Deletes student file
* parameters:
* - in: path
* name: id
* required: true
* description: ID or name of student to update
* schema:
* type: string
*
* responses:
* 200:
* description: Student information.
* content:
* application/json:
* schema:
* type: object
* properties:
* data:
* type: array
* items:
* type: object
* properties:
* id:
* type: integer
* description: The ID of student that was deleted.
* example: 123456788
* Status:
* type: string
* description: Status update.
* example: Successful/Failed
*
*/
app.delete('/students/:record_id', function (req, res) { //deletes student record
var record_id = req.params.record_id;
var id = parseInt(req.params.record_id);
MongoClient.connect(uri, { useUnifiedTopology: true }, function (err, client) {
if (err) throw err;
var dbo = client.db("StudentServer");
const filter = {
$or: [{ first_name: record_id }, { last_name: record_id }, { _id: id }, { fullname: record_id }] //filter search
}
if(record_id === "all"){
dbo.collection("Students").drop(function(err, result) { //drops all records
client.close();
var rsp_obj = {};
rsp_obj.record_id = -1;
rsp_obj.message = 'All files deleted'; //response if all are deleted
str = JSON.stringify(rsp_obj, null, 2);
console.log("All deleted");
return res.status(201).send(str);
});
}else{
dbo.collection("Students").find(filter).toArray(function (err, result) { //deletes if record is found
if (result.length > 0) {
var rsp_obj = {};
dbo.collection("Students").deleteOne(filter);
rsp_obj.record_id = record_id;
rsp_obj.message = 'successfully Deleted'; //response if found
str = JSON.stringify(rsp_obj, null, 2);
console.log("Deleted");
return res.status(201).send(str);
} else if (result.length <= 0) {
var rsp_obj = {};
rsp_obj.record_id = -1;
rsp_obj.message = 'Student not found'; //response if not found
str = JSON.stringify(rsp_obj, null, 2);
console.log("Student not found");
return res.status(201).send(str);
}
});
}
});
});
//end delete method
app.listen(5678); //start the server
console.log('Server is running...');