Skip to content

Commit

Permalink
Merge branch 'main' of github.com:geode-sdk/server
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 10, 2024
2 parents 58b7e09 + 417570c commit b137523
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 23 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy-api-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
- name: Generate Swagger UI
uses: Legion2/swagger-ui-action@v1
with:
version: "5.11.0"
output: swagger-ui
spec-file: ./spec.yml

Expand Down
16 changes: 8 additions & 8 deletions spec.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
openapi: 3.0.0
openapi: 3.1.0

info:
title: Geode Index
Expand Down Expand Up @@ -39,11 +39,11 @@ paths:
items:
$ref: '#/components/schemas/Mod'

/mods/{id}:
/mods/{id}/latest:
get:
tags:
- mods
summary: Get info for a mod
summary: Get info for the latest version of a mod
description: Returns info for the current latest version of the mod
parameters:
- $ref: '#/components/parameters/ModID'
Expand All @@ -54,7 +54,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Mod'
$ref: '#/components/schemas/ModVersion'

/mods/{id}/versions:
get:
Expand All @@ -69,7 +69,7 @@ paths:
content:
application/json:
schema:
type: string
$ref: '#/components/schemas/Mod'

/mods/{id}/{version}:
get:
Expand All @@ -85,7 +85,7 @@ paths:
content:
application/json:
schema:
type: string
$ref: '#/components/schemas/ModVersion'

/mods/updates:
post:
Expand Down Expand Up @@ -276,12 +276,12 @@ components:
description: Page number (default 1)
required: false
schema:
type: int
type: integer

PerPage:
name: per_page
in: query
description: Number of elements to fetch per page (default 10)
required: false
schema:
type: int
type: integer
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
21 changes: 11 additions & 10 deletions src/types/models/mod_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ 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}%");
Expand All @@ -30,11 +31,11 @@ impl Mod {
.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 b137523

Please sign in to comment.