T-954157790456099: Add endpoint for getting user profiles

This commit is contained in:
Okechi Onyeje 2018-12-29 21:14:29 -05:00
parent aece58471e
commit 1271b2dfe6
3 changed files with 22 additions and 26 deletions

View File

@ -21,7 +21,7 @@ function router(app) {
// Profile Endpoints
app.post('/profiles/:profileNumber', ProfileHandlers.updateUserProfile);
app.get('profiles/:profileNumber', ProfileHandlers.getUserProfile);
app.get('/profiles/:profileNumber', ProfileHandlers.getUserProfile);
}
module.exports = router;

View File

@ -25,25 +25,17 @@ function updateProfile(profileObj, profileNumber, tagferId) {
/**
* Gets a user profile
* @param {Number} profileNumber profile number that identifies which profile information to get for a user
* @param {string} tagferId SessionId obtained by extracting from authorization header
* @returns {Object} Profile object containg information for a specific user's profile
*/
async function getProfile(profileNumber) {
//get tagfer-id from loki
const sessions = loki.getCollection("sessions");
const primarySession = sessions.get(1);
const tagferId = primarySession.tagferId;
var profile = null;
await database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) {
if (snapshot.val()) {
profile = snapshot.val();
}
function getProfile(profileNumber, tagferId) {
return database.ref(`/profiles/${tagferId}/profile${profileNumber}`).once('value').then(function(snapshot) {
return snapshot.val();
});
return profile;
}
module.exports = {
updateProfile,
createInitialProfiles
createInitialProfiles,
getProfile
};

View File

@ -34,20 +34,24 @@ async function updateUserProfile(req, res) {
}
async function getUserProfile(req, res) {
const { profileNumber } = req.param;
var profile = dao.getProfile(profileNumber).then( (profile) => {
if (profile) {
res.json({ profile, status: http.OK})
} else {
res.json({error: errors.NO_PROFILE_FOUND_FOR_NUMBER , status: http.BAD_REQUEST})
}
}).catch( error => {
res.json({status: http.INTERNAL_SERVER_ERROR, error})
})
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
profileDao.getProfile(req.params.profileNumber,tagferId).then((profile) => {
if (profile) {
res.status(http.OK).json({profile})
} else {
res.status(http.BAD_REQUEST).json({ error: errors.NO_PROFILE_FOUND_FOR_NUMBER})
}
}).catch(error => {
res.status(http.INTERNAL_SERVER_ERROR).json({error})
})
} catch (error) {
res.status(http.BAD_REQUEST).json({ error })
}
}
module.exports = {
updateUserProfile
updateUserProfile,
getUserProfile
}