Skip to content

Commit

Permalink
comment count
Browse files Browse the repository at this point in the history
  • Loading branch information
Dromader2137 committed Jun 4, 2024
1 parent aa55af2 commit 3dad4bb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
60 changes: 45 additions & 15 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ pub async fn get_posts_by_user(user_id: i64, limit: i64, offset: i64) -> Result<
.await
.unwrap();
let query = "
SELECT posts.*, users.user_name, users.display_name, images.image_file
SELECT posts.*, users.user_name, users.display_name, images.image_file,
COUNT(comments.comment_id)
FROM posts
JOIN users ON users.user_id=posts.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
LEFT JOIN comments ON comments.post_id=posts.post_id
WHERE users.user_id = ?
GROUP BY posts.post_id
ORDER BY posts.date DESC
LIMIT ? OFFSET ?";

Expand Down Expand Up @@ -63,7 +66,8 @@ pub async fn get_posts_by_user(user_id: i64, limit: i64, offset: i64) -> Result<
Ok(val) => val,
Err(_) => "".to_string()
}
}
},
like_count: row.get(8).unwrap()
});
}
Ok(post_vec)
Expand Down Expand Up @@ -93,15 +97,18 @@ pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64, date
let phrase_cpy = "%".to_string() + &decoded_phrase + "%";
let query = format!(
"
SELECT posts.*, users.user_name, users.display_name, images.image_file
SELECT posts.*, users.user_name, users.display_name, images.image_file,
COUNT(comments.comment_id)
FROM posts
JOIN users
ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
LEFT JOIN comments ON comments.post_id=posts.post_id
WHERE posts.body LIKE ?
AND posts.user_id NOT IN
(SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
GROUP BY posts.post_id
ORDER BY posts.likes DESC
LIMIT ? OFFSET ?
",
Expand All @@ -126,7 +133,8 @@ pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64, date
Ok(val) => val,
Err(_) => "".to_string()
}
}
},
like_count: row.get(8).unwrap()
});
}
Ok(post_vec)
Expand Down Expand Up @@ -207,12 +215,15 @@ pub async fn get_posts(limit: i64, offset: i64) -> Result<impl warp::Reply, warp
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name, users.display_name, images.image_file
SELECT posts.*, users.user_name, users.display_name, images.image_file,
COUNT(comments.comment_id)
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
LEFT JOIN comments ON comments.post_id=posts.post_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
GROUP BY posts.post_id
ORDER BY posts.date DESC
LIMIT ? OFFSET ?",
timestamp
Expand All @@ -237,7 +248,9 @@ pub async fn get_posts(limit: i64, offset: i64) -> Result<impl warp::Reply, warp
Ok(val) => val,
Err(_) => "".to_string()
}
}
},
like_count: row.get(8).unwrap()

});
}
Ok(post_vec)
Expand All @@ -262,13 +275,16 @@ pub async fn get_posts_top(limit: i64, offset: i64, date_from: i64) -> Result<im
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name, users.display_name, images.image_file
SELECT posts.*, users.user_name, users.display_name, images.image_file,
COUNT(comments.comment_id)
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
LEFT JOIN comments ON comments.post_id=posts.post_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
GROUP BY posts.post_id
ORDER BY posts.likes DESC
LIMIT ? OFFSET ?",
timestamp
Expand All @@ -293,7 +309,8 @@ pub async fn get_posts_top(limit: i64, offset: i64, date_from: i64) -> Result<im
Ok(val) => val,
Err(_) => "".to_string()
}
}
},
like_count: row.get(8).unwrap()
});
}
Ok(post_vec)
Expand All @@ -318,13 +335,16 @@ pub async fn get_posts_bottom(limit: i64, offset: i64, date_from: i64) -> Result
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name, users.display_name, images.image_file
SELECT posts.*, users.user_name, users.display_name, images.image_file,
COUNT(comments.comment_id)
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
LEFT JOIN comments ON comments.post_id=posts.post_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
GROUP BY posts.post_id
ORDER BY posts.likes ASC
LIMIT ? OFFSET ?",
timestamp
Expand All @@ -349,7 +369,8 @@ pub async fn get_posts_bottom(limit: i64, offset: i64, date_from: i64) -> Result
Ok(val) => val,
Err(_) => "".to_string()
}
}
},
like_count: row.get(8).unwrap()
});
}
Ok(post_vec)
Expand All @@ -374,13 +395,16 @@ pub async fn get_posts_trending(limit: i64, offset: i64, date_from: i64) -> Resu
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name, users.display_name, images.image_file
SELECT posts.*, users.user_name, users.display_name, images.image_file,
COUNT(comments.comment_id)
FROM posts
JOIN users ON posts.user_id = users.user_id
LEFT JOIN images ON users.pfp_id=images.image_id
LEFT JOIN comments ON comments.post_id=posts.post_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
GROUP BY posts.post_id
ORDER BY (posts.likes / (({} - posts.date + 3600) / 3600)) DESC
LIMIT ? OFFSET ?",
timestamp, timestamp
Expand All @@ -405,7 +429,8 @@ pub async fn get_posts_trending(limit: i64, offset: i64, date_from: i64) -> Resu
Ok(val) => val,
Err(_) => "".to_string()
}
}
},
like_count: row.get(8).unwrap()
});
}
Ok(post_vec)
Expand Down Expand Up @@ -568,10 +593,12 @@ pub async fn get_post_by_id(post_id: i64) -> Result<impl warp::Reply, warp::Reje
.await
.unwrap();
let query = "
SELECT posts.*, users.user_name, users.display_name, images.image_file
SELECT posts.*, users.user_name, users.display_name, images.image_file,
COUNT(comments.comment_id)
FROM posts
JOIN users
ON posts.user_id = users.user_id
LEFT JOIN comments ON comments.post_id=posts.post_id
LEFT JOIN images ON users.pfp_id=images.image_id
WHERE posts.post_id = ?";

Expand All @@ -594,7 +621,8 @@ pub async fn get_post_by_id(post_id: i64) -> Result<impl warp::Reply, warp::Reje
Ok(val) => val,
Err(_) => "".to_string()
}
}
},
like_count: row.get(8).unwrap()
};
} else {
post = Post {
Expand All @@ -606,6 +634,7 @@ pub async fn get_post_by_id(post_id: i64) -> Result<impl warp::Reply, warp::Reje
user_name: "".to_string(),
display_name: "".to_string(),
pfp_image: "".to_string(),
like_count: 0
};
}
Ok(post)
Expand Down Expand Up @@ -900,7 +929,8 @@ pub async fn post(
likes: 0,
user_name: "".to_string(),
display_name: "".to_string(),
pfp_image: "".to_string()
pfp_image: "".to_string(),
like_count: 0
},
request.tags,
)
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Post {
pub user_name: String,
pub display_name: String,
pub pfp_image: String,
pub like_count: i64
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down

0 comments on commit 3dad4bb

Please sign in to comment.