use std::sync::Arc;
use crate::actix_web::HttpResponse;
#[cfg(feature = "authorization")]
use crate::biome::profile::rest_api::BIOME_PROFILE_READ_PERMISSION;
use crate::biome::profile::store::UserProfileStore;
use crate::futures::IntoFuture;
use crate::rest_api::{
ErrorResponse, Method, ProtocolVersionRangeGuard, Resource, SPLINTER_PROTOCOL_VERSION,
};
const BIOME_LIST_PROFILES_PROTOCOL_MIN: u32 = 1;
pub fn make_profiles_list_route(profile_store: Arc<dyn UserProfileStore>) -> Resource {
let resource = Resource::build("/biome/profiles").add_request_guard(
ProtocolVersionRangeGuard::new(BIOME_LIST_PROFILES_PROTOCOL_MIN, SPLINTER_PROTOCOL_VERSION),
);
#[cfg(feature = "authorization")]
{
resource.add_method(Method::Get, BIOME_PROFILE_READ_PERMISSION, move |_, _| {
let profile_store = profile_store.clone();
Box::new(match profile_store.list_profiles() {
Ok(profiles) => Box::new(HttpResponse::Ok().json(profiles).into_future()),
Err(err) => {
debug!("Failed to get profiles from the database {}", err);
Box::new(
HttpResponse::InternalServerError()
.json(ErrorResponse::internal_error())
.into_future(),
)
}
})
})
}
#[cfg(not(feature = "authorization"))]
{
resource.add_method(Method::Get, move |_, _| {
let profile_store = profile_store.clone();
Box::new(match profile_store.list_profiles() {
Ok(profiles) => HttpResponse::Ok().json(profiles).into_future(),
Err(err) => {
debug!("Failed to get profiles from the database {}", err);
HttpResponse::InternalServerError()
.json(ErrorResponse::internal_error())
.into_future()
}
})
})
}
}