diff --git a/src/endpoints/mods.rs b/src/endpoints/mods.rs index ce6bae0..75e3ee3 100644 --- a/src/endpoints/mods.rs +++ b/src/endpoints/mods.rs @@ -14,7 +14,8 @@ pub struct IndexQueryParams { pub page: Option, pub per_page: Option, pub query: Option, - pub gd: GDVersionEnum + #[serde(default)] + pub gd: Option } #[derive(Deserialize)] diff --git a/src/main.rs b/src/main.rs index 9cd1f0a..df6a4f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ async fn main() -> anyhow::Result<()> { sqlx::migrate!("./migrations") .run(&pool) .await?; - let addr = "127.0.0.1"; + let addr = "0.0.0.0"; let port = dotenvy::var("PORT").map_or(8080, |x: String| x.parse::().unwrap()); let debug = dotenvy::var("APP_DEBUG").unwrap_or("0".to_string()) == "1"; let app_url = dotenvy::var("APP_URL").unwrap_or("http://localhost".to_string()); diff --git a/src/types/models/mod_entity.rs b/src/types/models/mod_entity.rs index bf5d3fc..04f6c4f 100644 --- a/src/types/models/mod_entity.rs +++ b/src/types/models/mod_entity.rs @@ -2,7 +2,7 @@ use actix_web::web::Bytes; use serde::Serialize; use sqlx::{PgConnection, QueryBuilder, Postgres}; use std::io::Cursor; -use crate::{types::{models::{mod_version::ModVersion, mod_gd_version::GDVersionEnum}, api::{PaginatedData, ApiError}, mod_json::ModJson}, endpoints::mods::IndexQueryParams}; +use crate::{types::{models::mod_version::ModVersion, api::{PaginatedData, ApiError}, mod_json::ModJson}, endpoints::mods::IndexQueryParams}; use super::mod_gd_version::{ModGDVersion, DetailedGDVersion}; @@ -15,9 +15,10 @@ pub struct Mod { pub versions: Vec } -#[derive(Debug)] +#[derive(Debug, sqlx::FromRow)] struct ModRecord { id: String, + #[sqlx(default)] repository: Option, latest_version: String, validated: bool, @@ -48,25 +49,56 @@ impl Mod { let limit = per_page; let offset = (page - 1) * per_page; let query_string = format!("%{}%", query.query.unwrap_or("".to_string())); - log::info!("{}", query_string); - let records: Vec = sqlx::query_as!(ModRecord, r#"SELECT DISTINCT - m.id, m.repository, m.latest_version, m.validated FROM mods m - INNER JOIN mod_versions mv ON m.id = mv.mod_id - INNER JOIN mod_gd_versions mgv ON mgv.mod_id = mv.id - WHERE m.validated = true AND mv.name LIKE $1 AND mgv.gd = $2 - LIMIT $3 OFFSET $4"#, - query_string, query.gd as GDVersionEnum, limit, offset) + let mut builder: QueryBuilder = QueryBuilder::new( + "SELECT DISTINCT m.id, m.repository, m.latest_version, m.validated FROM mods m + INNER JOIN mod_versions mv ON m.id = mv.mod_id + INNER JOIN mod_gd_versions mgv ON mgv.mod_id = mv.id + WHERE m.validated = true AND mv.name LIKE " + ); + let mut counter_builder: QueryBuilder = QueryBuilder::new( + "SELECT COUNT(*) FROM mods m + INNER JOIN mod_versions mv ON m.id = mv.mod_id + INNER JOIN mod_gd_versions mgv ON mgv.mod_id = mv.id + WHERE m.validated = true AND mv.name LIKE " + ); + counter_builder.push_bind(&query_string); + builder.push_bind(&query_string); + match query.gd { + Some(g) => { + builder.push(" AND mgv.gd = "); + builder.push_bind(g); + counter_builder.push(" AND mgv.gd = "); + counter_builder.push_bind(g); + }, + None => () + }; + builder.push(" LIMIT "); + builder.push_bind(limit); + builder.push(" OFFSET "); + builder.push_bind(offset); + + let result = builder.build_query_as::() .fetch_all(&mut *pool) - .await.or(Err(ApiError::DbError))?; - - let count = sqlx::query_scalar!("SELECT COUNT(*) - FROM mods m - INNER JOIN mod_versions mv ON m.id = mv.mod_id - INNER JOIN mod_gd_versions mgv ON mgv.mod_id = mv.id - WHERE m.validated = true AND mv.name LIKE $1 AND mgv.gd = $2", - query_string, query.gd as GDVersionEnum) + .await; + let records = match result { + Err(e) => { + log::error!("{}", e); + return Err(ApiError::DbError); + }, + Ok(r) => r + }; + + let result = counter_builder.build_query_scalar() .fetch_one(&mut *pool) - .await.or(Err(ApiError::DbError))?.unwrap_or(0); + .await; + let count = match result { + Err(e) => { + log::error!("{}", e); + return Err(ApiError::DbError); + }, + Ok(c) => c + }; + if records.is_empty() { return Ok(PaginatedData { data: vec![], count: 0 }); } diff --git a/src/types/models/mod_version.rs b/src/types/models/mod_version.rs index 8600b6f..2dee33a 100644 --- a/src/types/models/mod_version.rs +++ b/src/types/models/mod_version.rs @@ -71,7 +71,7 @@ impl ModVersion { pub fn modify_download_link(&mut self, app_url: &str) { self.download_link = format!("{}/v1/mods/{}/versions/{}/download", app_url, self.mod_id, self.version); } - pub async fn get_latest_for_mods(pool: &mut PgConnection, ids: &[&str], gd: GDVersionEnum) -> Result>, ApiError> { + pub async fn get_latest_for_mods(pool: &mut PgConnection, ids: &[&str], gd: Option) -> Result>, ApiError> { if ids.is_empty() { return Ok(Default::default()); } @@ -82,9 +82,12 @@ impl ModVersion { mv.early_load, mv.api, mv.mod_id, m.changelog FROM mod_versions mv INNER JOIN mod_gd_versions mgv ON mgv.mod_id = mv.id INNER JOIN mods m ON m.id = mv.mod_id - WHERE mv.version = m.latest_version AND mgv.gd = "# + WHERE mv.version = m.latest_version"# ); - query_builder.push_bind(gd as GDVersionEnum); + if gd.is_some() { + query_builder.push(" AND mgv.gd = "); + query_builder.push_bind(gd.unwrap() as GDVersionEnum); + } query_builder.push(" AND mv.mod_id IN ("); let mut separated = query_builder.separated(","); for id in ids.iter() {