Skip to content

Commit

Permalink
Merge pull request #640 from bounswe/return-usernames
Browse files Browse the repository at this point in the history
Improved Reponse and error handling fix for followed and follower endpoints
  • Loading branch information
Sefik-Palazoglu authored Dec 25, 2023
2 parents 4b9158b + 2bbfb2e commit af99ad0
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions prediction-polls/backend/src/repositories/ProfileDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,17 @@ async function updatePoints(userId, additional_points) {
}

async function followProfile(follower_id, followed_id) {

const { error: error_follower } = await getProfileWithUserId(follower_id);
const { error: error_followed } = await getProfileWithUserId(followed_id);
const { updateDecision, error: validityError } = await verifyFollow(follower_id, followed_id);
if (error_follower || error_followed) {
throw { error: errorCodes.PROFILE_NOT_FOUND };
}
if (validityError) {
throw { error: errorCodes.FOLLOWERSHIP_ALREADY_EXISTS };
}
try {
const { error: error_follower } = await getProfileWithUserId(follower_id);
const { error: error_followed } = await getProfileWithUserId(followed_id);
const { updateDecision, error: validityError } = await verifyFollow(follower_id, followed_id);
if (error_follower || error_followed) {
throw { error: errorCodes.PROFILE_NOT_FOUND };
}
if (validityError) {
throw { error: errorCodes.FOLLOWERSHIP_ALREADY_EXISTS };
}
if (updateDecision == true) {
const query_follow = "UPDATE user_follow SET follow_status = ? WHERE follower_id = ? AND followed_id = ?";
const values = [true, follower_id, followed_id];
Expand All @@ -219,16 +220,16 @@ async function followProfile(follower_id, followed_id) {
}
async function unfollowProfile(follower_id, followed_id) {
const query_follow = "UPDATE user_follow SET follow_status = ? WHERE follower_id = ? AND followed_id = ?";
const { error: error_follower } = await getProfileWithUserId(follower_id);
const { error: error_followed } = await getProfileWithUserId(followed_id);
const { error: error_follow } = await verifyUnFollow(follower_id, followed_id);
if (error_follower || error_followed) {
throw { error: errorCodes.PROFILE_NOT_FOUND };
}
if (error_follow) {
throw { error: errorCodes.NO_FOLLOWERSHIP_FOUND };
}
try {
const { error: error_follower } = await getProfileWithUserId(follower_id);
const { error: error_followed } = await getProfileWithUserId(followed_id);
const { error: error_follow } = await verifyUnFollow(follower_id, followed_id);
if (error_follower || error_followed) {
throw { error: errorCodes.PROFILE_NOT_FOUND };
}
if (error_follow) {
throw { error: errorCodes.NO_FOLLOWERSHIP_FOUND };
}
const values = [false, follower_id, followed_id]
const [resultSetHeader] = await pool.query(query_follow, values);
return { status: "success" };
Expand All @@ -243,13 +244,13 @@ async function followedProfiles(userId) {
throw { error: errorCodes.PROFILE_NOT_FOUND };
}
try {
const follow_query = "SELECT followed_id FROM user_follow WHERE follower_id = ? AND follow_status = ? "
const follow_query = "SELECT users.username FROM user_follow , users WHERE user_follow.followed_id = users.id AND user_follow.follower_id = ? AND user_follow.follow_status = ? "
const values = [userId, true];
const [rows] = await pool.query(follow_query, values);
const followed = rows.map(
(followership) => { return followership.followed_id }
(followership) => { return followership.username }
);

return { followed_list: followed };
} catch (error) {
return { error: errorCodes.DATABASE_ERROR };
Expand All @@ -262,42 +263,42 @@ async function followerProfiles(userId) {
throw { error: errorCodes.PROFILE_NOT_FOUND };
}
try {
const follow_query = "SELECT follower_id FROM user_follow WHERE followed_id = ? AND follow_status = ? "
const follow_query = "SELECT users.username FROM user_follow, users WHERE user_follow.follower_id = users.id AND user_follow.followed_id = ? AND follow_status = ? "
const values = [userId, true];
const [rows] = await pool.query(follow_query, values);
const follower = rows.map(
(followership) => { return followership.follower_id }
(followership) => { return followership.username }
);
return { follower_list: follower };
}
catch (error) {
return { error: errorCodes.DATABASE_ERROR };
}
}
async function getRankPerTag(topic){
async function getRankPerTag(topic) {
try {
const query = 'SELECT users.id , users.username, has_domain_point.amount from users, has_domain_point where users.id = has_domain_point.userId AND has_domain_point.topic = ? Order By has_domain_point.amount Desc LIMIT 100';
const [result] = await pool.query(query, [topic]);
return {result: result};
return { result: result };
} catch (error) {
return { error: errorCodes.DATABASE_ERROR };
}
}

async function updateBadges(userId){
async function updateBadges(userId) {
try {
const query = 'Select * from (SELECT userId, topic, RANK() OVER (PARTITION BY topic ORDER BY amount DESC) as userRank FROM has_domain_point) as temp where temp.userRank > 4';
const [result] = await pool.query(query, [userId]);
const deleteQuery = 'DELETE FROM badges';
const [_] = await pool.query(deleteQuery, []);
const insertQuery = 'INSERT INTO badges (userRank, topic, userId) VALUES (?, ?, ?)'
result.map(async (row)=>{
const [result] = await pool.query(insertQuery, [row.userRank,row.topic,row.userId]);
result.map(async (row) => {
const [result] = await pool.query(insertQuery, [row.userRank, row.topic, row.userId]);
})
return {result: "Success"};
return { result: "Success" };
} catch (error) {
return { error: errorCodes.DATABASE_ERROR };
}
}

module.exports = { getProfileWithProfileId, getProfileWithUserId, addProfile, updateProfile, getBadges, updateBadge, updatePoints, followProfile, unfollowProfile, followerProfiles, followedProfiles,getRankPerTag,updateBadges }
module.exports = { getProfileWithProfileId, getProfileWithUserId, addProfile, updateProfile, getBadges, updateBadge, updatePoints, followProfile, unfollowProfile, followerProfiles, followedProfiles, getRankPerTag, updateBadges }

0 comments on commit af99ad0

Please sign in to comment.