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] 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