Skip to content

Commit

Permalink
fix warnings and dberror when no mods
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Jan 10, 2024
1 parent ceb92b3 commit aeeff43
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/endpoints/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{AppData, Error};
use crate::types::models::mod_entity::Mod;

#[derive(Deserialize)]
struct IndexQueryParams {
pub struct IndexQueryParams {
page: Option<i64>,
per_page: Option<i64>,
query: Option<String>
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 12 additions & 11 deletions src/types/models/mod_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PaginatedData<Mod>, 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<ModRecord> = sqlx::query_as!(ModRecord, r#"SELECT * FROM mods WHERE id LIKE $1 LIMIT $2 OFFSET $3"#, query_string, limit, offset)
let records: Vec<ModRecord> = 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(),
Expand Down
8 changes: 6 additions & 2 deletions src/types/models/mod_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ struct ModVersionRecord {
}

impl ModVersion {
pub async fn get_versions_for_mods(pool: &mut PgConnection, ids: Vec<&str>) -> Result<HashMap<String, Vec<ModVersion>>, Error> {
pub async fn get_versions_for_mods(pool: &mut PgConnection, ids: &[&str]) -> Result<HashMap<String, Vec<ModVersion>>, Error> {
if ids.is_empty() {
return Ok(Default::default());
}

let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
"SELECT * FROM mod_versions WHERE mod_id IN ("
);
Expand Down Expand Up @@ -85,6 +89,6 @@ impl ModVersion {
Entry::Occupied(mut e) => { e.get_mut().push(version) }
}
}
return Ok(ret);
Ok(ret)
}
}

0 comments on commit aeeff43

Please sign in to comment.