feat: list forges REST API

This commit is contained in:
Aravinth Manivannan 2023-02-26 23:33:08 +05:30
parent 5ed1ebacef
commit 3f29d420c3
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
2 changed files with 73 additions and 2 deletions

View file

@ -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,
}
}
}

View file

@ -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<OptionalPage>) -> ServiceResult<impl Responder> {
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<impl Responder> {
@ -33,5 +46,57 @@ pub async fn lastest(federate: WebFederate) -> ServiceResult<impl Responder> {
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<Forge> = 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()));
}
}