Skip to content

Commit

Permalink
Merge pull request #83 from kochamaltki/post-sorts
Browse files Browse the repository at this point in the history
Post sorts
  • Loading branch information
Dromader2137 authored May 21, 2024
2 parents 6daf461 + ff3411d commit f555b09
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 8 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@
#### /api/get/posts/by-id/{id}/{limit}/{offset}
- Get: 200 (Post) / 404 ("Post not found")
- Note: Post with id {id}
#### /api/get/posts/all/{limit}/{offset}
- Get: 200 (PostList)
#### /api/get/posts/new/{limit}/{offset}
- Get: 200 (PostList) sorted by date
#### /api/get/posts/top/{limit}/{offset}/{from_date}
- Get: 200 (PostList) sorted by likes descending
#### /api/get/posts/bottom/{limit}/{offset}/{from_date}
- Get: 200 (PostList) sorted by likes ascending
#### /api/get/posts/trending/{limit}/{offset}/{from_date}
- Get: 200 (PostList) sorted by (likes / age in minutes)
```
Post {
post_id: i64
Expand Down
4 changes: 3 additions & 1 deletion scripts/test-posts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

ip=$1

token_1=$(./scripts/signup.sh $ip dr 1234 false)
token_1=$(./scripts/login.sh $ip admin admin false)
tok_1=$token_1
./scripts/create-post.sh $ip hello welcome yo $tok_1
./scripts/react.sh $ip 0 $tok_1
./scripts/create-post.sh $ip hello welcome yo $tok_1
./scripts/react.sh $ip 1 $tok_1
./scripts/create-post.sh $ip hello welcome yo $tok_1
./scripts/create-post.sh $ip hello welcome yo $tok_1
./scripts/create-post.sh $ip hello welcome yo $tok_1
Expand Down
154 changes: 150 additions & 4 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub async fn get_posts_by_user(user_id: i64, limit: i64, offset: i64) -> Result<
FROM posts
JOIN users ON users.user_id=posts.user_id
WHERE users.user_id = ?
ORDER BY posts.date DESC
LIMIT ? OFFSET ?";

if !check_user_id(&connection, user_id).await {
Expand Down Expand Up @@ -96,6 +97,7 @@ pub async fn get_posts_by_tag(tag: String, limit: i64, offset: i64) -> Result<im
WHERE posts_tags.tag_id = ?
AND posts.user_id NOT IN
(SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
ORDER BY posts.date DESC
LIMIT ? OFFSET ?
",
timestamp
Expand Down Expand Up @@ -241,10 +243,10 @@ pub async fn get_posts(limit: i64, offset: i64) -> Result<impl warp::Reply, warp
"
SELECT posts.*, users.user_name
FROM posts
JOIN users
ON posts.user_id = users.user_id
WHERE posts.user_id NOT IN
(SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
JOIN users ON posts.user_id = users.user_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
ORDER BY posts.date DESC
LIMIT ? OFFSET ?",
timestamp
);
Expand Down Expand Up @@ -276,6 +278,150 @@ pub async fn get_posts(limit: i64, offset: i64) -> Result<impl warp::Reply, warp
))
}

pub async fn get_posts_top(limit: i64, offset: i64, date_from: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
.unwrap();
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
FROM posts
JOIN users ON posts.user_id = users.user_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
ORDER BY posts.likes DESC
LIMIT ? OFFSET ?",
timestamp
);

let post_list = connection
.call(move |conn| {
let mut statement = conn.prepare(&query).unwrap();
let mut rows = statement.query(params![date_from, limit, offset]).unwrap();
let mut post_vec: Vec<Post> = Vec::new();
while let Ok(Some(row)) = rows.next() {
post_vec.push(Post {
post_id: row.get(0).unwrap(),
user_id: row.get(1).unwrap(),
date: row.get(2).unwrap(),
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
});
}
Ok(post_vec)
})
.await
.unwrap();

let post = PostList { post_list };
Ok(warp::reply::with_status(
warp::reply::json(&post),
warp::http::StatusCode::OK,
))
}

pub async fn get_posts_bottom(limit: i64, offset: i64, date_from: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
.unwrap();
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
FROM posts
JOIN users ON posts.user_id = users.user_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
ORDER BY posts.likes ASC
LIMIT ? OFFSET ?",
timestamp
);

let post_list = connection
.call(move |conn| {
let mut statement = conn.prepare(&query).unwrap();
let mut rows = statement.query(params![date_from, limit, offset]).unwrap();
let mut post_vec: Vec<Post> = Vec::new();
while let Ok(Some(row)) = rows.next() {
post_vec.push(Post {
post_id: row.get(0).unwrap(),
user_id: row.get(1).unwrap(),
date: row.get(2).unwrap(),
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
});
}
Ok(post_vec)
})
.await
.unwrap();

let post = PostList { post_list };
Ok(warp::reply::with_status(
warp::reply::json(&post),
warp::http::StatusCode::OK,
))
}

pub async fn get_posts_trending(limit: i64, offset: i64, date_from: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
.unwrap();
let timestamp = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
let query = format!(
"
SELECT posts.*, users.user_name
FROM posts
JOIN users ON posts.user_id = users.user_id
WHERE
posts.user_id NOT IN (SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
AND posts.date > ?
ORDER BY (posts.likes / (({} - posts.date) / 60)) ASC
LIMIT ? OFFSET ?",
timestamp, timestamp
);

let post_list = connection
.call(move |conn| {
let mut statement = conn.prepare(&query).unwrap();
let mut rows = statement.query(params![date_from, limit, offset]).unwrap();
let mut post_vec: Vec<Post> = Vec::new();
while let Ok(Some(row)) = rows.next() {
post_vec.push(Post {
post_id: row.get(0).unwrap(),
user_id: row.get(1).unwrap(),
date: row.get(2).unwrap(),
body: row.get(3).unwrap(),
likes: row.get(4).unwrap(),
user_name: row.get(5).unwrap(),
});
}
Ok(post_vec)
})
.await
.unwrap();

let post = PostList { post_list };
Ok(warp::reply::with_status(
warp::reply::json(&post),
warp::http::StatusCode::OK,
))
}

pub async fn get_comments_from_post(post_id: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db").await.unwrap();
let query = "
Expand Down
17 changes: 16 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and_then(get_post_by_id);

let get_posts = warp::get()
.and(warp::path!("api" / "get" / "posts" / "all" / i64 / i64))
.and(warp::path!("api" / "get" / "posts" / "new" / i64 / i64))
.and_then(get_posts);

let get_posts_top = warp::get()
.and(warp::path!("api" / "get" / "posts" / "top" / i64 / i64 / i64))
.and_then(get_posts_top);

let get_posts_bottom = warp::get()
.and(warp::path!("api" / "get" / "posts" / "bottom" / i64 / i64 / i64))
.and_then(get_posts_bottom);

let get_posts_trending = warp::get()
.and(warp::path!("api" / "get" / "posts" / "trending" / i64 / i64 / i64))
.and_then(get_posts_trending);

let get_tags_from_post = warp::get()
.and(warp::path!("api" / "get" / "tags" / "from-post" / i64))
Expand Down Expand Up @@ -171,6 +183,9 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
get_posts_by_user
.or(post)
.or(get_posts)
.or(get_posts_top)
.or(get_posts_bottom)
.or(get_posts_trending)
.or(login)
.or(signup)
.or(get_user_name)
Expand Down

0 comments on commit f555b09

Please sign in to comment.