use super::ApiError; use crate::database; use crate::models::projects::{Project, ProjectStatus}; use crate::util::auth::check_is_moderator_from_headers; use actix_web::{get, web, HttpRequest, HttpResponse}; use serde::Deserialize; use sqlx::PgPool; #[derive(Deserialize)] pub struct ResultCount { #[serde(default = "default_count")] pub count: i16, } fn default_count() -> i16 { 100 } #[get("projects")] pub async fn get_projects( req: HttpRequest, pool: web::Data, count: web::Query, ) -> Result { check_is_moderator_from_headers(req.headers(), &**pool).await?; use futures::stream::TryStreamExt; let project_ids = sqlx::query!( " SELECT id FROM mods WHERE status = ( SELECT id FROM statuses WHERE status = $1 ) ORDER BY updated ASC LIMIT $2; ", ProjectStatus::Processing.as_str(), count.count as i64 ) .fetch_many(&**pool) .try_filter_map(|e| async { Ok(e.right().map(|m| database::models::ProjectId(m.id))) }) .try_collect::>() .await?; let projects: Vec = database::Project::get_many_full(project_ids, &**pool) .await? .into_iter() .map(super::projects::convert_project) .collect(); Ok(HttpResponse::Ok().json(projects)) }