Skip to content

Commit

Permalink
fix warnings, error on invalid page, case insensitive search
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Feb 11, 2024
1 parent 7ea7581 commit 72e8d62
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/endpoints/mod_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct IndexQueryParams {
}

#[derive(Deserialize)]
struct CreateQueryParams {
pub struct CreateQueryParams {
download_url: String
}

Expand Down
24 changes: 12 additions & 12 deletions src/types/models/mod_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ struct ModRecordGetOne {
impl Mod {
pub async fn get_index(pool: &mut PgConnection, query: IndexQueryParams) -> Result<PaginatedData<Mod>, 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<VerPlatform> = vec![];
if query.platforms.is_some() {
for i in query.platforms.unwrap().split(",") {
Expand All @@ -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<Postgres> = 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 (");
Expand Down

0 comments on commit 72e8d62

Please sign in to comment.