Skip to content

Commit b44ea9b

Browse files
committed
feat: [torrust#658] New API endpoint for getting all the users
1 parent e8bd16d commit b44ea9b

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/app.rs

+7
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
160160

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

163+
let listing_service = Arc::new(user::ListingService::new(
164+
user_profile_repository.clone(),
165+
authorization_service.clone(),
166+
))
167+
.clone();
168+
163169
// Build app container
164170

165171
let app_data = Arc::new(AppData::new(
@@ -194,6 +200,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
194200
profile_service,
195201
ban_service,
196202
about_service,
203+
listing_service,
197204
));
198205

199206
// Start cronjob to import tracker torrent data and updating

src/common.rs

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub struct AppData {
5252
pub profile_service: Arc<user::ProfileService>,
5353
pub ban_service: Arc<user::BanService>,
5454
pub about_service: Arc<about::Service>,
55+
pub listing_service: Arc<user::ListingService>,
5556
}
5657

5758
impl AppData {
@@ -90,6 +91,7 @@ impl AppData {
9091
profile_service: Arc<user::ProfileService>,
9192
ban_service: Arc<user::BanService>,
9293
about_service: Arc<about::Service>,
94+
listing_service: Arc<user::ListingService>,
9395
) -> AppData {
9496
AppData {
9597
cfg,
@@ -125,6 +127,7 @@ impl AppData {
125127
profile_service,
126128
ban_service,
127129
about_service,
130+
listing_service,
128131
}
129132
}
130133
}

src/web/api/server/v1/contexts/user/handlers.rs

+27
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,30 @@ fn api_base_url(host: &str) -> String {
181181
// See https://github.com/torrust/torrust-index/issues/131
182182
format!("http://{host}")
183183
}
184+
185+
/// It handles the request to get all the users.
186+
///
187+
/// It returns:
188+
///
189+
/// - `200` response with a json containing a list with all the users and their profiles [`Vec<UserProfile>`](crate::models::torrent_tag::UserProfile).
190+
/// - Other error status codes if there is a database error.
191+
///
192+
/// Refer to the [API endpoint documentation](crate::web::api::server::v1::contexts::user)
193+
/// for more information about this endpoint.
194+
///
195+
/// # Errors
196+
///
197+
/// It returns an error if:
198+
/// There is a database error
199+
/// There is a problem authorizing the action.
200+
/// The user is not authorized to perform the action
201+
#[allow(clippy::unused_async)]
202+
pub async fn get_all_handler(
203+
State(app_data): State<Arc<AppData>>,
204+
ExtractOptionalLoggedInUser(maybe_user_id): ExtractOptionalLoggedInUser,
205+
) -> Response {
206+
match app_data.listing_service.get_all(maybe_user_id).await {
207+
Ok(users) => Json(crate::web::api::server::v1::responses::OkResponseData { data: users }).into_response(),
208+
Err(error) => error.into_response(),
209+
}
210+
}

src/web/api/server/v1/contexts/user/routes.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use axum::routing::{delete, get, post};
77
use axum::Router;
88

99
use super::handlers::{
10-
ban_handler, change_password_handler, email_verification_handler, login_handler, registration_handler, renew_token_handler,
11-
verify_token_handler,
10+
ban_handler, change_password_handler, email_verification_handler, get_all_handler, login_handler, registration_handler,
11+
renew_token_handler, verify_token_handler,
1212
};
1313
use crate::common::AppData;
1414

@@ -38,3 +38,8 @@ pub fn router(app_data: Arc<AppData>) -> Router {
3838
// code-review: should not this be a POST method? We add the user to the blacklist. We do not delete the user.
3939
.route("/ban/:user", delete(ban_handler).with_state(app_data))
4040
}
41+
42+
/// Routes for the [`user`](crate::web::api::server::v1::contexts::user) API context.
43+
pub fn router_for_multiple_resources(app_data: Arc<AppData>) -> Router {
44+
Router::new().route("/", get(get_all_handler).with_state(app_data))
45+
}

0 commit comments

Comments
 (0)