From 3f29d420c32cbebee4235cfa17960370a730bd76 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 26 Feb 2023 23:33:08 +0530 Subject: [PATCH] feat: list forges REST API --- api_routes/src/lib.rs | 8 +++++- src/api.rs | 67 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/api_routes/src/lib.rs b/api_routes/src/lib.rs index 99afaab..63bae11 100644 --- a/api_routes/src/lib.rs +++ b/api_routes/src/lib.rs @@ -36,14 +36,20 @@ impl Search { #[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)] pub struct Api { pub get_latest: &'static str, + pub forges: &'static str, pub search: Search, } impl Api { const fn new() -> Api { let get_latest = "/api/v1/federated/latest"; + let forges = "/api/v1/forges/list"; let search = Search::new(); - Api { get_latest, search } + Api { + get_latest, + search, + forges, + } } } diff --git a/src/api.rs b/src/api.rs index c57e457..dae5dfe 100644 --- a/src/api.rs +++ b/src/api.rs @@ -21,9 +21,22 @@ use actix_web_codegen_const_routes::get; pub use api_routes::*; -use crate::errors::*; +use crate::pages::chart::home::{OptionalPage, Page}; use crate::search; use crate::WebFederate; +use crate::{errors::*, WebDB}; + +const LIMIT: u32 = 50; + +#[get(path = "ROUTES.forges")] +pub async fn forges(db: WebDB, q: web::Query) -> ServiceResult { + let q = q.into_inner(); + let q: Page = q.into(); + let offset = q.page * LIMIT; + let forges = db.get_all_forges(offset, LIMIT).await?; + + Ok(HttpResponse::Ok().json(forges)) +} #[get(path = "ROUTES.get_latest")] pub async fn lastest(federate: WebFederate) -> ServiceResult { @@ -33,5 +46,57 @@ pub async fn lastest(federate: WebFederate) -> ServiceResult { pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(lastest); + cfg.service(forges); search::services(cfg); } + +#[cfg(test)] +mod tests { + use super::*; + use crate::tests::*; + use crate::*; + + use actix_web::http::StatusCode; + use actix_web::test; + use db_core::prelude::*; + use url::Url; + + #[actix_rt::test] + async fn list_forges_works() { + const URL: &str = "https://list-forges-works-test.example.com"; + const HTML_PROFILE_URL: &str = "https://list-forges-works-test.example.com/user1"; + const USERNAME: &str = "user1"; + + const REPO_NAME: &str = "asdlkfjaldsfjaksdf"; + const HTML_REPO_URL: &str = + "https://list-forges-works-test.example.com/user1/asdlkfjaldsfjaksdf"; + const TAGS: [&str; 3] = ["test", "starchart", "spider"]; + + let (db, ctx, federate, _tmpdir) = sqlx_sqlite::get_ctx().await; + let app = get_app!(ctx, db, federate).await; + + let url = Url::parse(URL).unwrap(); + + let create_forge_msg = CreateForge { + url: url.clone(), + forge_type: ForgeImplementation::Gitea, + import: false, + }; + + let _ = db.delete_forge_instance(&create_forge_msg.url).await; + db.create_forge_instance(&create_forge_msg).await.unwrap(); + assert!( + db.forge_exists(&create_forge_msg.url).await.unwrap(), + "forge creation failed, forge existence check failure" + ); + + // test starts + let lisit_res_resp = get_request!(&app, ROUTES.forges); + assert_eq!(lisit_res_resp.status(), StatusCode::OK); + let forges_list: Vec = test::read_body_json(lisit_res_resp).await; + assert!(!forges_list.is_empty()); + assert!(forges_list + .iter() + .any(|f| f.url == create_forge_msg.url.to_string())); + } +}