Skip to content

Commit

Permalink
feat: [#658] New API endpoint for getting all the users
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Jan 8, 2025
1 parent e8bd16d commit 590b20b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running

let about_service = Arc::new(about::Service::new(authorization_service.clone()));

let listing_service = Arc::new(user::ListingService::new(
user_profile_repository.clone(),
authorization_service.clone(),
))
.clone();

// Build app container

let app_data = Arc::new(AppData::new(
Expand Down Expand Up @@ -194,6 +200,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
profile_service,
ban_service,
about_service,
listing_service,
));

// Start cronjob to import tracker torrent data and updating
Expand Down
3 changes: 3 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct AppData {
pub profile_service: Arc<user::ProfileService>,
pub ban_service: Arc<user::BanService>,
pub about_service: Arc<about::Service>,
pub listing_service: Arc<user::ListingService>,
}

impl AppData {
Expand Down Expand Up @@ -90,6 +91,7 @@ impl AppData {
profile_service: Arc<user::ProfileService>,
ban_service: Arc<user::BanService>,
about_service: Arc<about::Service>,
listing_service: Arc<user::ListingService>,
) -> AppData {
AppData {
cfg,
Expand Down Expand Up @@ -125,6 +127,7 @@ impl AppData {
profile_service,
ban_service,
about_service,
listing_service,
}
}
}
27 changes: 27 additions & 0 deletions src/web/api/server/v1/contexts/user/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,30 @@ fn api_base_url(host: &str) -> String {
// See https://github.com/torrust/torrust-index/issues/131
format!("http://{host}")
}

/// It handles the request to get all the users.
///
/// It returns:
///
/// - `200` response with a json containing a list with all the users and their profiles [`Vec<UserProfile>`](crate::models::torrent_tag::UserProfile).
/// - Other error status codes if there is a database error.
///
/// Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::user)
/// for more information about this endpoint.
///
/// # Errors
///
/// It returns an error if:
/// There is a database error
/// There is a problem authorizing the action.
/// The user is not authorized to perform the action
#[allow(clippy::unused_async)]
pub async fn get_all_handler(
State(app_data): State<Arc<AppData>>,
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
) -> Response {
match app_data.listing_service.get_all(maybe_user_id).await {
Ok(users) => Json(crate::web::api::server::v1::responses::OkResponseData { data: users }).into_response(),
Err(error) => error.into_response(),
}
}
9 changes: 7 additions & 2 deletions src/web/api/server/v1/contexts/user/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use axum::routing::{delete, get, post};
use axum::Router;

use super::handlers::{
ban_handler, change_password_handler, email_verification_handler, login_handler, registration_handler, renew_token_handler,
verify_token_handler,
ban_handler, change_password_handler, email_verification_handler, get_all_handler, login_handler, registration_handler,
renew_token_handler, verify_token_handler,
};
use crate::common::AppData;

Expand Down Expand Up @@ -38,3 +38,8 @@ pub fn router(app_data: Arc<AppData>) -> Router {
// code-review: should not this be a POST method? We add the user to the blacklist. We do not delete the user.
.route("/ban/:user", delete(ban_handler).with_state(app_data))
}

/// Routes for the [`user`](crate::web::api::server::v1::contexts::user) API context.
pub fn router_for_multiple_resources(app_data: Arc<AppData>) -> Router {
Router::new().route("/", get(get_all_handler).with_state(app_data))
}

0 comments on commit 590b20b

Please sign in to comment.