Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nisum-scorecard
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Venkaiah Naidu Singamchetty
nisum-scorecard
Commits
65d8dafa
Commit
65d8dafa
authored
Mar 13, 2024
by
Venkaiah Naidu Singamchetty
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'apiserver' into 'master'
apiPrimaryEdit See merge request
!1
parents
3dd71f3a
c94a2354
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
167 deletions
+6
-167
server.js
server.js
+6
-167
No files found.
server.js
View file @
65d8dafa
...
...
@@ -18,176 +18,15 @@ connectToDb((err) => {
}
})
app
.
get
(
'/employees'
,
(
req
,
res
)
=>
{
db
.
collection
(
'employees'
).
find
().
toArray
()
.
then
(
result
=>
{
res
.
send
(
result
)
})
.
catch
(
error
=>
res
.
status
(
401
).
send
(
error
))
})
// app.get('/products', (req, res) => {
// const pageIndex=parseInt(req.query.p || "0")
// let pageSize=5;
// db.collection('products').find().skip(pageIndex*pageSize).limit(pageSize).toArray()
// .then(result => {res.send(result)})
// .catch(error => res.status(500).send(error))
// })
app
.
get
(
'/products/:id'
,
(
req
,
res
)
=>
{
const
id
=
req
.
params
.
id
;
if
(
!
isNaN
(
id
))
{
const
numericId
=
Number
(
id
);
db
.
collection
(
'products'
).
findOne
({
id
:
numericId
})
.
then
(
result
=>
{
if
(
result
!=
null
)
{
res
.
status
(
200
).
json
(
result
);
}
else
{
res
.
status
(
404
).
json
({
error
:
'Product not found'
});
}
})
.
catch
(
error
=>
res
.
status
(
400
).
json
({
error
:
'Invalid ID'
}));
}
else
if
(
/^
[
a-zA-Z
]
+$/
.
test
(
id
))
{
res
.
status
(
404
).
json
({
error
:
'Invalid ID'
});
}
else
{
res
.
status
(
400
).
json
({
error
:
'Invalid ID'
});
}
});
// Middleware function to check if userId already exists
const
checkUserIdExists
=
(
req
,
res
,
next
)
=>
{
const
userId
=
req
.
body
.
userId
.
trim
();
db
.
collection
(
'users'
).
findOne
({
userId
:
userId
})
.
then
(
result
=>
{
if
(
result
)
{
res
.
status
(
400
).
json
({
error
:
"userId already exists"
});
}
else
{
next
();
// Proceed to register user if userId is not taken
}
})
.
catch
(
error
=>
res
.
status
(
500
).
json
({
error
:
"Internal server error"
}));
};
// Register User endpoint with middleware
app
.
post
(
'/registeruser'
,
checkUserIdExists
,
(
req
,
res
)
=>
{
const
user
=
req
.
body
;
const
userid
=
req
.
body
.
userId
;
db
.
collection
(
'users'
).
insertOne
(
user
)
.
then
(
result
=>
{
res
.
status
(
201
).
json
(
result
);
db
.
collection
(
'cartitems'
).
insertOne
({
userId
:
userid
,
cartItems
:
[]
})
})
.
catch
(
err
=>
res
.
status
(
500
).
json
({
error
:
"Could not create a new document"
}));
});
// Get Users endpoint
app
.
get
(
'/users'
,
(
req
,
res
)
=>
{
// db.collection('users').find({}, { projection: { _id: false, userId: true, password: true } }).toArray()
db
.
collection
(
'users'
).
find
({},
{
projection
:
{
_id
:
false
}
}).
toArray
()
.
then
(
result
=>
{
res
.
send
(
result
);
})
.
catch
(
error
=>
res
.
status
(
500
).
send
(
error
));
});
//login api
app
.
post
(
'/login'
,
async
(
req
,
res
)
=>
{
const
{
userId
,
password
}
=
req
.
body
;
try
{
const
user
=
await
db
.
collection
(
'users'
).
findOne
({
userId
:
userId
})
if
(
!
user
)
{
return
res
.
status
(
401
).
json
({
error
:
'Authentication failed'
,
message
:
'User not found'
});
}
if
(
password
===
user
.
password
&&
userId
===
user
.
userId
)
{
delete
user
.
password
;
delete
user
.
_id
;
res
.
json
({
message
:
'Login successful'
,
user
});
}
else
{
res
.
status
(
401
).
json
({
error
:
'Authentication failed'
,
message
:
'Email and password do not match'
});
}
}
catch
(
error
)
{
res
.
status
(
500
).
json
({
error
:
'Internal server error'
,
details
:
error
.
message
});
}
});
app
.
delete
(
'/deregister/:userid'
,
(
req
,
res
)
=>
{
const
userid
=
req
.
params
.
userid
if
(
isNaN
(
userid
))
{
db
.
collection
(
'users'
).
deleteOne
({
userId
:
userid
})
.
then
(
result
=>
{
res
.
send
(
result
)
db
.
collection
(
'cartitems'
).
deleteOne
({
userId
:
userid
})
})
.
catch
(
error
=>
res
.
status
(
500
).
send
(
error
))
}
else
{
res
.
status
(
500
).
json
({
error
:
'Invalid ID'
})
}
})
app
.
patch
(
'/updateuser/:id'
,
(
req
,
res
)
=>
{
const
Id
=
req
.
params
.
id
const
data
=
req
.
body
if
(
ObjectId
.
isValid
(
Id
))
{
db
.
collection
(
'users'
).
updateOne
({
_id
:
new
ObjectId
(
Id
)
},
{
$set
:
data
})
app
.
get
(
'/employee/:id'
,
(
req
,
res
)
=>
{
let
Id
=
parseInt
(
req
.
params
.
id
);
db
.
collection
(
'employees'
).
findOne
({
empId
:
Id
},{
projection
:{
_id
:
false
}})
.
then
(
result
=>
{
res
.
send
(
result
)
})
.
catch
(
error
=>
res
.
status
(
500
).
send
(
error
))
}
else
{
res
.
status
(
500
).
json
({
error
:
'Invalid ID'
})
}
})
app
.
get
(
'/cartItems/:userid'
,
(
req
,
res
)
=>
{
const
userid
=
req
.
params
.
userid
const
usernameRegex
=
/^
[
a-zA-Z0-9_
]{1,10}
$/
;
if
(
usernameRegex
.
test
(
userid
))
{
db
.
collection
(
'cartitems'
).
findOne
({
userId
:
userid
})
.
then
(
result
=>
{
if
(
result
!=
null
)
{
res
.
status
(
200
).
send
(
result
);
}
else
{
res
.
status
(
404
).
json
({
error
:
'UserCart not found'
});
}
})
.
catch
(
error
=>
res
.
status
(
500
).
send
(
error
))
}
else
{
res
.
status
(
400
).
json
({
error
:
'Invalid UserId'
})
}
.
catch
(
error
=>
res
.
status
(
401
).
send
(
error
))
})
\ No newline at end of file
app
.
patch
(
'/updateCartItems/:userid'
,
async
(
req
,
res
)
=>
{
const
userid
=
req
.
params
.
userid
;
const
newCartItem
=
req
.
body
;
// Check if userid is a number
if
(
!
isNaN
(
userid
))
{
return
res
.
status
(
400
).
json
({
error
:
'Invalid UserId'
});
}
try
{
// const cart = await db.collection('cartitems').findOne({ userId: userid });
// if (!cart) {
// // If cart doesn't exist, create a new one with the newCartItem
// await db.collection('cartitems').insertOne({ userId: userid, cartItems: [newCartItem] });
// return res.status(200).json({ message: 'Cart created with new item' });
// }
// // Check if the item already exists in the cart
// const existingItemIndex = cart.cartItems.findIndex(item => item.id === newCartItem.id);
// if (existingItemIndex !== -1) {
// // If the item already exists, increase its quantity by 1
// cart.cartItems[existingItemIndex].qty += 1;
// } else {
// // If the item doesn't exist, add it to the cart
// cart.cartItems.push(newCartItem);
// }
// // Update the cart with the modified cartItems
// await db.collection('cartitems').updateOne({ userId: userid }, { $set: { cartItems: cart.cartItems } });
await
db
.
collection
(
'cartitems'
).
updateOne
({
userId
:
userid
},
{
$set
:
{
cartItems
:
newCartItem
}
});
return
res
.
status
(
200
).
json
({
message
:
'Cart updated successfully'
});
}
catch
(
error
)
{
return
res
.
status
(
500
).
json
({
error
:
error
.
message
});
}
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment