Skip to content
Snippets Groups Projects

Add additional GET endpoints to user service

Merged Kirby, Matt J (UG - Comp Sci & Elec Eng) requested to merge user-enpoints into main
All threads resolved!
Files
3
@@ -70,7 +70,17 @@ export async function register(req,res){
// return save result as a response
user.save()
.then(result => res.status(201).send({ msg: "User Registered Successfully"}))
.then(async (result) => {
const user = await UserModel.findOne({username: username})
// create jwt token
const token = jwt.sign({
userId: user._id,
username : user.username
}, ENV.JWT_SECRET , { expiresIn : "24h"});
res.status(201).send({ msg: "User Registered Successfully", username: user.username, token })
})
.catch(error => res.status(500).send({error}))
}).catch(error => {
@@ -195,3 +205,49 @@ export async function updateUser(req,res){
}
}
/**
* POST /userlist
* This DOES NOT return emails and passwords
* @param {*} req
* @param {*} res
* @returns
*
* body: {userIdList: []}
*/
export const GetUserList = async (req,res) => {
try {
const {userIdList} = req.body;
const users = await UserModel.find({ '_id': { $in: userIdList } }, {password: 0, email: 0});
return res.status(201).send({ userList: users});
} catch(error){
return res.status(401).send({ error });
}
}
/**
* GET /search
* This endpoint OMITS the password and email fields
* @param {*} req
* @param {*} res
* @returns
*
* query: {query: string}
*/
export const Search = async (req,res) => {
try {
const {searchQuery} = req.query;
console.log(searchQuery)
if(searchQuery === undefined || searchQuery.length === 0){
throw new Error("Please provide a valid query!")
}
const usersWithMatchingUsername = await UserModel.find({"username": {"$regex": `^${searchQuery}`}}, {password: 0, email: 0})
const usersWithMatchingName = await UserModel.find({$or: [{"firstName": {"$regex": `^${searchQuery}`,"$options": 'i'}}, {"lastName": {"$regex": `^${searchQuery}`,"$options": 'i'}}]}, {password: 0, email: 0})
return res.status(201).send({ usersByUsername: usersWithMatchingUsername, usersByName: usersWithMatchingName});
} catch(error){
return res.status(401).send({ error: error.message });
}
}
Loading