Skip to content

Commit

Permalink
search users dziala ale bez pfp :(
Browse files Browse the repository at this point in the history
  • Loading branch information
malinowy5 committed May 18, 2024
1 parent 99a53eb commit 3e2bc9a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
56 changes: 54 additions & 2 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,58 @@ pub async fn get_posts_from_search(phrase: String, limit: i64, offset: i64) -> R
))
}

pub async fn get_users_from_search(phrase: String, limit: i64, offset: 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 phrase_cpy = "%".to_string() + &phrase + "%";
let query = format!(
"
SELECT users.user_id, users.user_name, users.display_name, users.description, images.image_file
FROM users
LEFT JOIN images ON images.image_id=users.pfp_id
WHERE users.user_name LIKE ?
AND users.user_id NOT IN
(SELECT user_id FROM bans WHERE is_active = 1 AND expires_on > {})
LIMIT ? OFFSET ?
", // tutaj tez ten left join do wywalenia
timestamp
);
let profile_list = connection
.call(move |conn| {
let mut statement = conn.prepare(&query).unwrap();
let mut rows = statement.query(params![phrase_cpy, limit, offset]).unwrap();
let mut profile_vec: Vec<Profile> = Vec::new();
while let Ok(Some(row)) = rows.next() {
let pfp = match row.get::<_, String>(4) {
Ok(val) => format!("pfp_{}", val),
Err(_) => "".to_string()
};
profile_vec.push(Profile {
user_id: row.get(0).unwrap(),
user_name: row.get(1).unwrap(),
display_name: row.get(2).unwrap(),
description: row.get(3).unwrap(),
pfp_image: pfp,
});
}
Ok(profile_vec)
})
.await
.unwrap();

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

pub async fn get_posts(limit: i64, offset: i64) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
Expand Down Expand Up @@ -436,9 +488,9 @@ pub async fn get_profile_by_id(user_id: i64) -> Result<impl warp::Reply, warp::R
users.display_name, users.description,
images.image_file
FROM users
JOIN images ON images.image_id=users.pfp_id
LEFT JOIN images ON images.image_id=users.pfp_id
WHERE users.user_id = ?
";
"; // na razie jest left join zeby zwracalo cokolwiek, do naprawienia

if !check_user_id(&connection, user_id).await {
let r = "User not found";
Expand Down
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![recursion_limit = "256"]
use warp::Filter;
pub mod auth;
pub mod api_calls;
Expand Down Expand Up @@ -64,9 +65,9 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(warp::path!("api" / "get" / "posts" / "from-search" / String / i64 / i64))
.and_then(get_posts_from_search);

// let get_users_from_search = warp::get()
// .and(warp::path("api" / "get" / "users" / "from-search" / String))
// .and_then(get_posts_from_search);
let get_users_from_search = warp::get()
.and(warp::path!("api" / "get" / "users" / "from-search" / String / i64 / i64))
.and_then(get_users_from_search);

let post = warp::post()
.and(warp::path!("api" / "post" / "add-post"))
Expand Down Expand Up @@ -198,7 +199,7 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.or(comment)
.or(get_comments_from_post)
.or(get_posts_from_search)
// .or(get_users_from_search)
.or(get_users_from_search)
}

#[tokio::main]
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct PostList {

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ProfileList {
pub post_list: Vec<Profile>
pub profile_list: Vec<Profile>
}

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

0 comments on commit 3e2bc9a

Please sign in to comment.