-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
499 lines (479 loc) · 13.7 KB
/
index.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
require('dotenv').config()
const express = require('express')
const app = express()
const port = process.env.PORT
app.use(express.json())
app.listen(port, ()=>{
console.log(`I'm istening to port ${port}`)
})
let realEstates = [
{
id: 1,
name: 'five bedroom duplex',
count: 10,
location: 'ikoyi',
status: 'active',
isCofO: true,
isFenced: true,
price: 40000
},
{
id: 2,
name: 'abbey',
location: 'lekki',
count: 10,
status: 'active',
isCofO: true,
isFenced: true,
price: 20000
},
{
id: 3,
name: 'aqueduct',
location: 'ajah',
count: 10,
status: 'pending',
isCofO: true,
isFenced: true,
price: 25000
},
{
id: 4,
name: 'bowling alley',
location: 'egbeda',
count: 10,
status: 'pending',
isCofO: true,
isFenced: true,
price: 35000
}
]
//endpoint to create a property
app.post('/property', (request, response)=>{
for(let key in request.body){
request.body[key] = request.body[key].trim()
}
const {name, location, isCofO, isFenced, status} = request.body
try {
if(!name || !location || !isCofO || !isFenced){
throw new Error (`All fields are required`)
}
for(let i = 0; i< realEstates.length; i++){
if(realEstates[i].name == name && realEstates[i].location == location ){
throw new Error (`This property already exist`)
}
}
realEstates.push({
id: realEstates.length + 1,
name,
location,
count: 10,
status,
isCofO,
isFenced
})
response.status(201).json({
message:'success',
status: true,
data: realEstates
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint to update a property
app.patch('/property/:id', (request, response)=>{
const {id} = request.params
for(let key in request.body){
request.body[key] = request.body[key].trim()
}
const {name, status, isCofO, isFenced, location} = request.body
try {
if(!name || !status || !isCofO || !isFenced || !location){
throw new Error (`All fields are required`)
}
let property = realEstates.find( props => props.id == id)
for(let i = 0; i< realEstates.length; i++){
if(realEstates[i].name == name && realEstates[i].location == location ){
throw new Error (`This property already exist`)
}
}
if(property){
realEstates[id - 1] = {...property, ...request.body}
}else{
throw new Error (`The property you're searching for is not available`)
}
response.status(200).json({
message: 'Success',
status: true,
patched: property,
data: realEstates
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint to delete a property
app.delete('/property/:id', (request, response)=>{
const {id} = request.params
try {
let deleteProp = realEstates.find((props)=> props.id == id)
if(deleteProp){
realEstates.splice(id-1, 1)
}else{
throw new Error `Property isn't available`
}
response.status(200).json({
message: 'Successful',
status: true,
dataaa: realEstates
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint to get all properties or by different query parameters
app.get('/properties', (request, response)=>{
const {status, location, search} = request.query
try {
let properties = realEstates
if(status){
properties = realEstates.filter(props => props.status.toLowerCase() == status.toLowerCase())
}
if(location){
properties = realEstates.filter(props => props.location.toLowerCase() == location.toLowerCase())
}
if(search){
properties = []
for(let i = 0; i< realEstates.length; i++){
if(Object.values(realEstates[i]).join(' ').includes(search.toLowerCase())){
properties.push(realEstates[i])
}
}
}
if(properties.length == 0){
properties = `There's no property with the above specification`
}
response.status(200).json({
message: 'Successfull',
status: true,
data: properties
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint to get a single property
app.get('/property/:id', (request, response)=>{
const {id} = request.params
try {
const property = realEstates.find(props => props.id == id)
if(!property){
throw new Error (`Proper is not available`)
}
response.status(200).json({
message: 'Successful',
status: true,
data: property
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
const users = [
{
id: 0,
firstName: 'Abass',
lastName: 'Ibrahim',
email: '[email protected]',
group: 'admin',
username: null,
password: 'zeekybass',
status: 'active',
isVerified: true,
stateOfOrigin: null,
dob: null,
marital_status: null,
gender: null,
qualifications: null
},
{
id: 1,
firstName: 'Khawthar',
lastName: 'Abass',
email: '[email protected]',
group: 'admin',
username: null,
password: 'ibr2024',
status: 'active',
isVerified: true,
stateOfOrigin: null,
dob: null,
marital_status: null,
gender: null,
qualifications: null
},
{
id: 2,
firstName: 'Sherifat',
lastName: 'Samiat',
email: '[email protected]',
group: 'user',
username: null,
password: 'Samoo4life',
status: 'active',
isVerified: true,
stateOfOrigin: null,
dob: null,
marital_status: null,
gender: null,
qualifications: null
}
]
//endpoint to create a new user
app.post('/user/register', (request, response)=>{
for(let key in request.body){
request.body[key] = request.body[key].trim()
}
const {firstName, lastName,email, password, isVerified} = request.body
try {
if(!firstName || !lastName || !password || !isVerified || !email){
throw new Error (`All fields are required`)
}
for(let i = 0; i< users.length; i++){
if(users[i].email == email){
throw new Error (`Email already exists!! Login here`)
}
}
if(firstName.length <3){
throw new Error (`first name is too short`)
}
if(lastName.length <3){
throw new Error (`last name is too short`)
}
if(password.length <3){
throw new Error (`password is too short`)
}
users.push({
id: users.length - 1,
firstName,
lastName,
email,
group: 'user',
username: null,
password,
status: 'pending',
isVerified,
stateOfOrigin: null,
dob: null,
marital_status: null,
gender: null,
qualifications: null
})
response.status(200).json({
message: 'Successful',
status: true,
data: users
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint for user login
app.post('/user/login', (request, response)=>{
const {email, password} = request.body
try {
if(!email || !password){
throw new Error (`All fields are required!!`)
}
let user = users.find(user => user.email == email)
if(!user){
throw new Error (`This email does not exist!!`)
}
if(user.password != password){
throw new Error (`password is not correct`)
}
response.status(200).json({
message: 'successful',
status: true,
greetings: `You're welcome ${user.firstName}`,
data: user
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
let cart = []
//endpoint to add item to cart
app.post('/user/cart/add', (request, response)=>{
const {id, quantity} = request.body
try {
if(!id || !quantity){
throw new Error(`Please select an item`)
}
let item = realEstates.find(prop => prop.id == id)
if(!item){
throw new Error(`This item is not available in store`)
}
if(item.count == 0 ){
throw new Error (`This item is not available at the moment`)
}else if(item.count < quantity){
throw new Error (`The quantity you demanded is more than the quantites availble. You can only buy ${item.count} at the moment`)
}
for (let i =0; i < realEstates.length; i++){
if(realEstates[i].id == id){
realEstates[i].count -= parseInt(quantity)
break
}
}
let addToCart = cart.find(cart => cart.id == id)
if(!addToCart){
cart.push({
...item,
cartId: cart.length + 1,
count: item.count,
quantity,
total: `$${item.price * quantity}`
})
}else{
addToCart.count= item.count,
addToCart.quantity+=quantity
addToCart.total = `${(addToCart.price * parseInt(addToCart.quantity))}`
}
// parseInt(addToCart.total.slice(1)) +
response.status(200).json({
message: 'Successful',
status: true,
data: cart,
})
} catch (error) {
response.status(400).json({
message: error.message,
status: false
})
}
})
//endpoint to get all carts
app.get('/user/carts', (request, response)=>{
try {
const {location, search} = request.query
let properties = cart
if(location){
properties = cart.filter(props => props.location.toLowerCase() == location.toLowerCase())
if(properties.length == 0){
throw new Error (`There's no property with the above specification in your cart`)
}
}
if(search){
properties = []
for(let i = 0; i< cart.length; i++){
if(Object.values(cart[i]).join(' ').includes(search.toLowerCase())){
properties.push(cart[i])
}
}
if(properties.length == 0){
throw new Error (`There's no property with the above specification in your cart`)
}
}
response.status(200).json({
message: 'Successfull',
status: true,
data: properties
})
}catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint to get a single cart
app.get('/user/cart/:cartId', (request, response) =>{
const {cartId} = request.params
try {
const cartItem = cart.find(props => props.cartId == cartId)
if(!cartItem){
throw new Error (`Item is not available in cart`)
}
response.status(200).json({
message: 'Successful',
status: true,
data: cartItem
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint to update (add) to a cart
app.patch('/user/cart/:cartId', (request, response)=>{
const {cartId} = request.params
const {quantity} = request.body
try {
let property = cart.find(props => props.cartId == cartId)
if(!property){
throw new Error (`This property isn't available`)
}
property = {
...property,
count: property.count -=quantity,
quantity: property.quantity+=quantity,
total: property.price *property.quantity
}
for(let i = 0; i < cart.length; i++){
if(cart[i].cartId ==cartId){
cart[i] = property
}
}
response.status(200).json({
message: 'SUccessful',
status: true,
data: property
})
} catch (error) {
response.status(400).json({
message: `${error.message}`,
status: false,
})
}
})
//endpoint to delete a cart
app.delete('/user/cart/:cartId', (request, response)=>{
const {cartId} = request.params
try {
for(let i = 0; i < cart.length; i++){
if(cart[i].cartId ==cartId){
cart.splice(i, 1)
}
}
response.status(200).json({
message: "Successful",
status: true,
data: cart
})
} catch (error) {
}
})