-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
187 lines (165 loc) · 6.47 KB
/
server.js
File metadata and controls
187 lines (165 loc) · 6.47 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
const express = require('express');
var session = require('express-session');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(bodyParser.json());
let sitesess;
const db = {
users: [
{
id:'1',
name:'Dev',
email: 'dev@web.com',
password:'1234',
created:new Date()
},
{
id:'2',
name:'Raj',
email: 'raj@web.com',
password:'12345',
created:new Date()
}
],
products: [
{
SKU:'ipd',
Name:'Super iPad',
Price:'$549.99'
},
{
SKU:'mbp',
Name:'MacBook Pro',
Price:'$1399.99'
},
{
SKU:'atv',
Name:'Apple TV',
Price:'$109.50'
},
{
SKU:'vga',
Name:'VGA adapter',
Price:'$30.00'
}
],
cartEntry: [
{
sku:'',
qty:''
}
],
total: [
{
totalsku:[],
totalVal:[0]
}
]
}
/*********************************************************************************************************************
GET Request
*********************************************************************************************************************/
app.get('/', (req,res) => {
res.send(db.total);
})
/********************************************************************************************************************
POST Request
********************************************************************************************************************/
app.post('/signin', (req, res) => {
db.users.filter(user => {
if(req.body.email == user.email && req.body.password == user.password) {
res.send(user.email+" is successfull signin.")
} else {
res.send("You have entered wrong credencials");
}
})
})
app.post('/register', (req, res) => {
const {email, password, name} = req.body;
db.users.push({
id:'3',
name,
email,
password,
created:new Date()
})
res.json(db.users[db.users.length-1]);
})
/*********************************************************************************************************************
PUT Request will be used to make update to existing resources
*********************************************************************************************************************/
app.put('/checkout', (req, res) => {
const {sku, qty} = req.body;
db.cartEntry.push({
sku:sku,
qty:qty
})
const lastNo = db.cartEntry.length-1;
const lastItem = db.cartEntry[lastNo];
for(let j=0; j<db.products.length; j++) {
if(lastItem.sku === db.products[j].SKU){
//console.log(db.products[j]);
let cartPrice = parseFloat(db.products[j].Price.slice(1)) * Number(lastItem.qty);
db.total[0].totalVal = (parseFloat(db.total[0].totalVal) + parseFloat(cartPrice));
var multiC = Number(lastItem.qty);
for(var mu=1; mu <=multiC; mu++){
db.total[0].totalsku.push(db.products[j].SKU)
}
}
}
const skuVal = db.total[0].totalsku;
let newArray = skuVal.reduce((b,c)=>((b[b.findIndex(d=>d.el===c)]||b[b.push({el:c,count:0})-1]).count++,b),[]);
const filterJam = newArray.map((cart, i) => {
/*******************************************************************************************************************************
**************************************************************************************************************
below code check if atv (Apple TV) quantity is equal to 3 then price will be charge for 2 units qty.
**********************************************************************************************************************
********************************************************************************************************************************/
if(cart.el === 'atv' && cart.count ===3 && sku === 'atv'){
db.total[0].totalVal = parseFloat(db.total[0].totalVal) - parseFloat(db.products[2].Price.slice(1));
}
/********************************************************************************************************************************
****************************************************************************************************************************
below code check if ipd (Super ipad) quantity is greater than 4 then whole price will be charge on the basis of $499.99
****************************************************************************************************************************
********************************************************************************************************************************/
if(cart.el === 'ipd' && cart.count > 4 && sku === 'ipd') {
const proprice = parseFloat(db.products[0].Price.slice(1));
let originalPrice = proprice * Number(cart.count);
let discountedCalc = 499.99 * Number(cart.count);
let finalval = originalPrice - discountedCalc;
db.total[0].totalVal = parseFloat(db.total[0].totalVal) - finalval;
}
/********************************************************************************************************************************
*****************************************************************************************************
below code check that with every mbp (MacBook Pro), vga (VGA Adapter) is free
*********************************************************************************************************
********************************************************************************************************************************/
if(cart.el === 'mbp' && sku === 'mbp'){
for(i=1; i <=qty; i++){
db.total[0].totalsku.push('vga');
}
}
})
/*******************************************************************************************************************************
********************************************** Final Output will be display here ***********************************************
*******************************************************************************************************************************/
scanned = () => {
console.log("SKU Scanned: "+db.total[0].totalsku);
console.log("Total expected: $"+db.total[0].totalVal.toFixed(2));
}
scanned();
})
app.listen(2019, () => {
console.log("Section 1 and 2 App is running on port 2019");
})
/*
The final output will be display like this: if i have selected 3 quantity of atv & submit it in put request using POSTMAN Then it
will display in console below result.
SKU Scanned: atv,atv,atv
Total expected: $219.00
these code will fulfill the requirement in the document file. I am not using any kind of frontend in this api.
it is simply work in get, post, put requests.
*/