From ceb92b364b3b68109d31b232593907c5d96042e5 Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Tue, 9 Jan 2024 17:57:14 -0300 Subject: [PATCH 1/3] switch back to openapi 3.1.0 --- .github/workflows/deploy-api-docs.yml | 1 + spec.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy-api-docs.yml b/.github/workflows/deploy-api-docs.yml index 6459c5c..93b2fb7 100644 --- a/.github/workflows/deploy-api-docs.yml +++ b/.github/workflows/deploy-api-docs.yml @@ -14,6 +14,7 @@ jobs: - name: Generate Swagger UI uses: Legion2/swagger-ui-action@v1 with: + version: "5.11.0" output: swagger-ui spec-file: ./spec.yml diff --git a/spec.yml b/spec.yml index 6161c87..bdd2215 100644 --- a/spec.yml +++ b/spec.yml @@ -1,4 +1,4 @@ -openapi: 3.0.0 +openapi: 3.1.0 info: title: Geode Index From aeeff4361f00d130962afabf8bca00fa9e0a132c Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Wed, 10 Jan 2024 14:51:43 -0300 Subject: [PATCH 2/3] fix warnings and dberror when no mods --- src/endpoints/mods.rs | 2 +- src/main.rs | 4 ++-- src/types/models/mod_entity.rs | 23 ++++++++++++----------- src/types/models/mod_version.rs | 8 ++++++-- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/endpoints/mods.rs b/src/endpoints/mods.rs index b10d52b..584cc58 100644 --- a/src/endpoints/mods.rs +++ b/src/endpoints/mods.rs @@ -8,7 +8,7 @@ use crate::{AppData, Error}; use crate::types::models::mod_entity::Mod; #[derive(Deserialize)] -struct IndexQueryParams { +pub struct IndexQueryParams { page: Option, per_page: Option, query: Option diff --git a/src/main.rs b/src/main.rs index d8eefcf..606448c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,12 +6,12 @@ use env_logger::Env; mod endpoints; mod types; -struct AppData { +pub struct AppData { db: sqlx::postgres::PgPool, } #[derive(Debug)] -enum Error { +pub enum Error { FsError, DbAcquireError, DbError, diff --git a/src/types/models/mod_entity.rs b/src/types/models/mod_entity.rs index 52cbf24..acb8062 100644 --- a/src/types/models/mod_entity.rs +++ b/src/types/models/mod_entity.rs @@ -13,28 +13,29 @@ pub struct Mod { impl Mod { pub async fn get_index(pool: &mut PgConnection, page: i64, per_page: i64, query: String) -> Result, Error> { - #[derive(Debug)] - struct ModRecord { - id: String, - repository: String, - latest_version: String, - validated: bool, - } + #[derive(Debug)] + struct ModRecord { + id: String, + repository: String, + latest_version: String, + validated: bool, + } + let limit = per_page; let offset = (page - 1) * per_page; let query_string = format!("%{query}%"); - let records: Vec = sqlx::query_as!(ModRecord, r#"SELECT * FROM mods WHERE id LIKE $1 LIMIT $2 OFFSET $3"#, query_string, limit, offset) + let records: Vec = sqlx::query_as!(ModRecord, "SELECT * FROM mods WHERE id LIKE $1 LIMIT $2 OFFSET $3", query_string, limit, offset) .fetch_all(&mut *pool) .await.or(Err(Error::DbError))?; let count = sqlx::query_scalar!("SELECT COUNT(*) FROM mods") .fetch_one(&mut *pool) .await.or(Err(Error::DbError))?.unwrap_or(0); - let ids = records.iter().map(|x| x.id.as_str()).collect(); - let versions = ModVersion::get_versions_for_mods(pool, ids).await?; + let ids: Vec<_> = records.iter().map(|x| x.id.as_str()).collect(); + let versions = ModVersion::get_versions_for_mods(pool, &ids).await?; let ret = records.into_iter().map(|x| { - let version_vec = versions.get(&x.id).map(|x| x.clone()).unwrap_or_default(); + let version_vec = versions.get(&x.id).cloned().unwrap_or_default(); Mod { id: x.id.clone(), repository: x.repository.clone(), diff --git a/src/types/models/mod_version.rs b/src/types/models/mod_version.rs index ff48555..952aea8 100644 --- a/src/types/models/mod_version.rs +++ b/src/types/models/mod_version.rs @@ -48,7 +48,11 @@ struct ModVersionRecord { } impl ModVersion { - pub async fn get_versions_for_mods(pool: &mut PgConnection, ids: Vec<&str>) -> Result>, Error> { + pub async fn get_versions_for_mods(pool: &mut PgConnection, ids: &[&str]) -> Result>, Error> { + if ids.is_empty() { + return Ok(Default::default()); + } + let mut query_builder: QueryBuilder = QueryBuilder::new( "SELECT * FROM mod_versions WHERE mod_id IN (" ); @@ -85,6 +89,6 @@ impl ModVersion { Entry::Occupied(mut e) => { e.get_mut().push(version) } } } - return Ok(ret); + Ok(ret) } } \ No newline at end of file From 417570cc4a4f97d3afdf8ac49101b675b11d3932 Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Wed, 10 Jan 2024 15:46:09 -0300 Subject: [PATCH 3/3] rename some endpoints in spec --- spec.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec.yml b/spec.yml index bdd2215..4db5c37 100644 --- a/spec.yml +++ b/spec.yml @@ -39,11 +39,11 @@ paths: items: $ref: '#/components/schemas/Mod' - /mods/{id}: + /mods/{id}/latest: get: tags: - mods - summary: Get info for a mod + summary: Get info for the latest version of a mod description: Returns info for the current latest version of the mod parameters: - $ref: '#/components/parameters/ModID' @@ -54,7 +54,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/Mod' + $ref: '#/components/schemas/ModVersion' /mods/{id}/versions: get: @@ -69,7 +69,7 @@ paths: content: application/json: schema: - type: string + $ref: '#/components/schemas/Mod' /mods/{id}/{version}: get: @@ -85,7 +85,7 @@ paths: content: application/json: schema: - type: string + $ref: '#/components/schemas/ModVersion' /mods/updates: post: @@ -276,7 +276,7 @@ components: description: Page number (default 1) required: false schema: - type: int + type: integer PerPage: name: per_page @@ -284,4 +284,4 @@ components: description: Number of elements to fetch per page (default 10) required: false schema: - type: int + type: integer