Skip to content

Commit aeeff43

Browse files
committed
fix warnings and dberror when no mods
1 parent ceb92b3 commit aeeff43

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

src/endpoints/mods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{AppData, Error};
88
use crate::types::models::mod_entity::Mod;
99

1010
#[derive(Deserialize)]
11-
struct IndexQueryParams {
11+
pub struct IndexQueryParams {
1212
page: Option<i64>,
1313
per_page: Option<i64>,
1414
query: Option<String>

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ use env_logger::Env;
66
mod endpoints;
77
mod types;
88

9-
struct AppData {
9+
pub struct AppData {
1010
db: sqlx::postgres::PgPool,
1111
}
1212

1313
#[derive(Debug)]
14-
enum Error {
14+
pub enum Error {
1515
FsError,
1616
DbAcquireError,
1717
DbError,

src/types/models/mod_entity.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,29 @@ pub struct Mod {
1313

1414
impl Mod {
1515
pub async fn get_index(pool: &mut PgConnection, page: i64, per_page: i64, query: String) -> Result<PaginatedData<Mod>, Error> {
16-
#[derive(Debug)]
17-
struct ModRecord {
18-
id: String,
19-
repository: String,
20-
latest_version: String,
21-
validated: bool,
22-
}
16+
#[derive(Debug)]
17+
struct ModRecord {
18+
id: String,
19+
repository: String,
20+
latest_version: String,
21+
validated: bool,
22+
}
23+
2324
let limit = per_page;
2425
let offset = (page - 1) * per_page;
2526
let query_string = format!("%{query}%");
26-
let records: Vec<ModRecord> = sqlx::query_as!(ModRecord, r#"SELECT * FROM mods WHERE id LIKE $1 LIMIT $2 OFFSET $3"#, query_string, limit, offset)
27+
let records: Vec<ModRecord> = sqlx::query_as!(ModRecord, "SELECT * FROM mods WHERE id LIKE $1 LIMIT $2 OFFSET $3", query_string, limit, offset)
2728
.fetch_all(&mut *pool)
2829
.await.or(Err(Error::DbError))?;
2930
let count = sqlx::query_scalar!("SELECT COUNT(*) FROM mods")
3031
.fetch_one(&mut *pool)
3132
.await.or(Err(Error::DbError))?.unwrap_or(0);
3233

33-
let ids = records.iter().map(|x| x.id.as_str()).collect();
34-
let versions = ModVersion::get_versions_for_mods(pool, ids).await?;
34+
let ids: Vec<_> = records.iter().map(|x| x.id.as_str()).collect();
35+
let versions = ModVersion::get_versions_for_mods(pool, &ids).await?;
3536

3637
let ret = records.into_iter().map(|x| {
37-
let version_vec = versions.get(&x.id).map(|x| x.clone()).unwrap_or_default();
38+
let version_vec = versions.get(&x.id).cloned().unwrap_or_default();
3839
Mod {
3940
id: x.id.clone(),
4041
repository: x.repository.clone(),

src/types/models/mod_version.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ struct ModVersionRecord {
4848
}
4949

5050
impl ModVersion {
51-
pub async fn get_versions_for_mods(pool: &mut PgConnection, ids: Vec<&str>) -> Result<HashMap<String, Vec<ModVersion>>, Error> {
51+
pub async fn get_versions_for_mods(pool: &mut PgConnection, ids: &[&str]) -> Result<HashMap<String, Vec<ModVersion>>, Error> {
52+
if ids.is_empty() {
53+
return Ok(Default::default());
54+
}
55+
5256
let mut query_builder: QueryBuilder<Postgres> = QueryBuilder::new(
5357
"SELECT * FROM mod_versions WHERE mod_id IN ("
5458
);
@@ -85,6 +89,6 @@ impl ModVersion {
8589
Entry::Occupied(mut e) => { e.get_mut().push(version) }
8690
}
8791
}
88-
return Ok(ret);
92+
Ok(ret)
8993
}
9094
}

0 commit comments

Comments
 (0)