From 72e8d628448b46833648bce7962468853dfac69f Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Sun, 11 Feb 2024 16:17:08 -0300 Subject: [PATCH] fix warnings, error on invalid page, case insensitive search --- src/endpoints/mod_versions.rs | 6 +++--- src/endpoints/mods.rs | 2 +- src/types/models/mod_entity.rs | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/endpoints/mod_versions.rs b/src/endpoints/mod_versions.rs index 63c6962..1e4823c 100644 --- a/src/endpoints/mod_versions.rs +++ b/src/endpoints/mod_versions.rs @@ -5,18 +5,18 @@ use sqlx::Acquire; use crate::{AppData, types::{api::{ApiError, ApiResponse}, models::{mod_version::ModVersion, mod_entity::{download_geode_file, Mod}}, mod_json::ModJson}}; #[derive(Deserialize)] -struct GetOnePath { +pub struct GetOnePath { id: String, version: String } #[derive(Deserialize)] -struct CreateQueryParams { +pub struct CreateQueryParams { download_url: String } #[derive(Deserialize)] -struct CreateVersionPath { +pub struct CreateVersionPath { id: String } diff --git a/src/endpoints/mods.rs b/src/endpoints/mods.rs index b3d2910..7630db1 100644 --- a/src/endpoints/mods.rs +++ b/src/endpoints/mods.rs @@ -21,7 +21,7 @@ pub struct IndexQueryParams { } #[derive(Deserialize)] -struct CreateQueryParams { +pub struct CreateQueryParams { download_url: String } diff --git a/src/types/models/mod_entity.rs b/src/types/models/mod_entity.rs index 8b72378..05c25ea 100644 --- a/src/types/models/mod_entity.rs +++ b/src/types/models/mod_entity.rs @@ -45,10 +45,12 @@ struct ModRecordGetOne { impl Mod { pub async fn get_index(pool: &mut PgConnection, query: IndexQueryParams) -> Result, ApiError> { let page = query.page.unwrap_or(1); + if page <= 0 { + return Err(ApiError::BadRequest("Invalid page number, must be >= 1".into())); + } let per_page = query.per_page.unwrap_or(10); let limit = per_page; let offset = (page - 1) * per_page; - let query_string = format!("%{}%", query.query.unwrap_or("".to_string())); let mut platforms: Vec = vec![]; if query.platforms.is_some() { for i in query.platforms.unwrap().split(",") { @@ -66,25 +68,23 @@ impl Mod { "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 " + WHERE m.validated = true AND LOWER(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 " + WHERE m.validated = true AND LOWER(mv.name) LIKE " ); + let query_string = format!("%{}%", query.query.unwrap_or("".to_string()).to_lowercase()); 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 => () - }; + if let Some(g) = query.gd { + builder.push(" AND mgv.gd = "); + builder.push_bind(g); + counter_builder.push(" AND mgv.gd = "); + counter_builder.push_bind(g); + } for (i, platform) in platforms.iter().enumerate() { if i == 0 { builder.push(" AND mgv.platform IN (");