-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (113 loc) · 3.76 KB
/
app.js
File metadata and controls
133 lines (113 loc) · 3.76 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
// import express
const express = require('express');
//import nedb
const Datastore = require('nedb');
// import body-parser
const bodyParser= require('body-parser');
// set up the app
const app = express();
// set up a new database
const db = new Datastore();
// logging constant
const log = (message) => console.log(message);
// add body-parsing functionality to the app
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// get app to serve static files from the public directory
app.use(express.static(__dirname + '/public'));
// global variable for id
let id = 1;
// Function to build an item
// takes in a name, description and price
let itemBuilder = (itemName, itemDescription, itemPrice, itemID) => {
// uses literal notation to create an item object
let item = {
name : itemName,
description : itemDescription,
price : itemPrice,
_id : itemID
}
// returns that item object
return item;
}
// CREATE (Post)
app.post('/create', (req,res) => {
// log that we are running the create operation
log(`\nCreate - POST`);
// create an item from the request body
let item = itemBuilder(req.body.name, req.body.description, req.body.price, parseInt(id));
// increment our id by one
id++;
// insert the item into our Database
db.insert(item, (err, item) => {
//if there is an error, send back the error
if (err) res.send(err);
// otherwise 201 - Created and the item
res.status(201).send(item);
//log that item to console
log(`Created item: ${JSON.stringify(item)}`);
});
});
// READ ALL (Get)
app.get('/read', (req,res) => {
// log that we are running the read operation
log(`\nRead - GET`);
// reading all items from database
db.find({}, (err, items) => {
//if there is an error, send back the error
if (err) res.send(err);
// otherwise 200 - OK
res.status(200).send(items);
//log the items to console
log(`Reading items: ${JSON.stringify(items)}`);
});
});
// READ ONE (Get)
app.get('/read/:id', (req,res) => {
// log that we are running the read operation
log(`\nRead - GET`);
// reading item from database by id
db.find({_id : parseInt(req.params.id)}, (err, item) => {
//if there is an error, send back the error
if (err) res.send(err);
// otherwise 200 - OK
res.status(200).send(item);
//log the item to console
log(`Reading item: ${JSON.stringify(item)}`);
});
});
// UPDATE (Put)
app.put('/update/:id', (req,res) => {
// log that we are running the read operation
log(`\nUpdate - PUT`);
// create a new item object
let updatedItem = itemBuilder(req.body.name, req.body.description, req.body.price, parseInt(req.params.id));
// find data in database BY using the id
// send the new item
db.update({_id : parseInt(req.params.id)}, updatedItem, (err, itemID) => {
//if there is an error, send back the error
if (err) res.send(err);
// otherwise 200 - OK
res.sendStatus(200);
// log the item ID being returned
log(`Updated item id: ${JSON.stringify(itemID)}`);
});
});
// DELETE (Delete)
app.delete('/delete/:id', (req,res) => {
// log that we are running the delete operation
log(`\nDelete - DELETE`);
// deleting item from database by id
db.remove({_id : parseInt(req.params.id)}, (err, itemID) => {
//if there is an error, send back the error
if (err) res.send(err);
// otherwise 200 - OK
res.sendStatus(200);
//log the item id to console
log(`Deleted item id: ${JSON.stringify(itemID)}`);
});
});
// export the app and the itemBuilder
module.exports = {app, itemBuilder};