Skip to content

Commit

Permalink
make "gd" optional on index
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 27, 2024
1 parent a97ed80 commit a984b49
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/endpoints/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub struct IndexQueryParams {
pub page: Option<i64>,
pub per_page: Option<i64>,
pub query: Option<String>,
pub gd: GDVersionEnum
#[serde(default)]
pub gd: Option<GDVersionEnum>
}

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u16>().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());
Expand Down
70 changes: 51 additions & 19 deletions src/types/models/mod_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -15,9 +15,10 @@ pub struct Mod {
pub versions: Vec<ModVersion>
}

#[derive(Debug)]
#[derive(Debug, sqlx::FromRow)]
struct ModRecord {
id: String,
#[sqlx(default)]
repository: Option<String>,
latest_version: String,
validated: bool,
Expand Down Expand Up @@ -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<ModRecord> = 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<Postgres> = 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<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 "
);
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::<ModRecord>()
.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 });
}
Expand Down
9 changes: 6 additions & 3 deletions src/types/models/mod_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<String, Vec<ModVersion>>, ApiError> {
pub async fn get_latest_for_mods(pool: &mut PgConnection, ids: &[&str], gd: Option<GDVersionEnum>) -> Result<HashMap<String, Vec<ModVersion>>, ApiError> {
if ids.is_empty() {
return Ok(Default::default());
}
Expand All @@ -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() {
Expand Down

0 comments on commit a984b49

Please sign in to comment.