feat: add endpoint to retrieve a public list of all users
All checks were successful
Build and Deploy Thoughts / build-and-deploy-local (push) Successful in 1m13s
All checks were successful
Build and Deploy Thoughts / build-and-deploy-local (push) Successful in 1m13s
This commit is contained in:
@@ -11,7 +11,9 @@ use serde_json::{json, Value};
|
||||
use app::persistence::{
|
||||
follow,
|
||||
thought::get_thoughts_by_user,
|
||||
user::{get_followers, get_following, get_user, search_users, update_user_profile},
|
||||
user::{
|
||||
get_all_users, get_followers, get_following, get_user, search_users, update_user_profile,
|
||||
},
|
||||
};
|
||||
use app::state::AppState;
|
||||
use app::{error::UserError, persistence::user::get_user_by_username};
|
||||
@@ -413,9 +415,25 @@ async fn get_user_followers(
|
||||
Ok(Json(UserListSchema::from(followers_list)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/all",
|
||||
responses(
|
||||
(status = 200, description = "A public list of all users", body = UserListSchema)
|
||||
),
|
||||
tag = "user"
|
||||
)]
|
||||
async fn get_all_users_public(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let users = get_all_users(&state.conn).await?;
|
||||
Ok(Json(UserListSchema::from(users)))
|
||||
}
|
||||
|
||||
pub fn create_user_router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", get(users_get))
|
||||
.route("/all", get(get_all_users_public))
|
||||
.route("/me", get(get_me).put(update_me))
|
||||
.nest("/me/api-keys", create_api_key_router())
|
||||
.route("/{param}", get(get_user_by_param))
|
||||
|
@@ -165,3 +165,10 @@ pub async fn get_followers(db: &DbConn, user_id: Uuid) -> Result<Vec<user::Model
|
||||
}
|
||||
get_users_by_ids(db, follower_ids).await
|
||||
}
|
||||
|
||||
pub async fn get_all_users(db: &DbConn) -> Result<Vec<user::Model>, DbErr> {
|
||||
user::Entity::find()
|
||||
.order_by_desc(user::Column::CreatedAt)
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
@@ -245,3 +245,25 @@ async fn test_update_me_css_and_images() {
|
||||
assert_eq!(v["headerUrl"], "https://example.com/new-header.jpg");
|
||||
assert_eq!(v["customCss"], "body { color: blue; }");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_get_all_users_public() {
|
||||
let app = setup().await;
|
||||
|
||||
create_user_with_password(&app.db, "userA", "password123", "a@example.com").await;
|
||||
create_user_with_password(&app.db, "userB", "password123", "b@example.com").await;
|
||||
create_user_with_password(&app.db, "userC", "password123", "c@example.com").await;
|
||||
|
||||
let response = make_get_request(app.router.clone(), "/users/all", None).await;
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: Value = serde_json::from_slice(&body).unwrap();
|
||||
let users_list = v["users"].as_array().unwrap();
|
||||
|
||||
assert_eq!(
|
||||
users_list.len(),
|
||||
3,
|
||||
"Should return a list of all 3 registered users"
|
||||
);
|
||||
}
|
||||
|
Reference in New Issue
Block a user