This commit is contained in:
JMARyA 2024-08-28 22:03:39 +02:00
parent acf5ebae78
commit 0e174dc06e
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
6 changed files with 53 additions and 16 deletions

7
Cargo.lock generated
View file

@ -288,6 +288,7 @@ name = "cdb"
version = "0.1.0"
dependencies = [
"chrono",
"env_logger",
"futures",
"log",
"mdq",
@ -523,7 +524,7 @@ dependencies = [
"convert_case",
"proc-macro2",
"quote",
"rustc_version 0.4.0",
"rustc_version 0.4.1",
"syn 2.0.76",
]
@ -1762,9 +1763,9 @@ dependencies = [
[[package]]
name = "rustc_version"
version = "0.4.0"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
dependencies = [
"semver 1.0.23",
]

View file

@ -18,3 +18,4 @@ tokio = { version = "1.35.1", features = ["full"] }
toml = "0.8.8"
uuid = { version = "1.8.0", features = ["v4"] }
mongod = { git = "https://git.hydrar.de/jmarya/mongod" }
env_logger = "0.11.5"

View file

@ -1,15 +1,19 @@
version: '3'
services:
# cdb:
# build: .
# ports:
# - "8080:8080"
# depends_on:
# - mongodb
# volumes:
# - ./itemdb:/itemdb
# environment:
# - "DB_URI=mongodb://user:pass@mongodb:27017"
cdb:
build: .
ports:
- "8080:8080"
depends_on:
- mongodb
volumes:
- ./itemdb/items:/itemdb
environment:
- "DB_URI=mongodb://user:pass@mongodb:27017"
- "DB=cdb"
- "RUST_LOG=debug"
- "ROCKET_ADDRESS=0.0.0.0"
- "ROCKET_PORT=8080"
mongodb:
image: mongo:latest
@ -20,4 +24,3 @@ services:
MONGO_INITDB_ROOT_PASSWORD: pass
volumes:
- ./db:/data/db

View file

@ -9,6 +9,11 @@
"title": "Item Name",
"description": "The name of the Item"
},
"category": {
"title": "Category",
"description": "The category of the item",
"type": "string"
},
"variants": {
"type": "array",
"minItems": 1,
@ -26,5 +31,8 @@
}
}
},
"required": ["name", "variants"]
"required": [
"name",
"variants"
]
}

View file

@ -25,6 +25,8 @@ mod variant;
#[launch]
async fn rocket() -> _ {
env_logger::init();
let cors = rocket_cors::CorsOptions {
allowed_origins: rocket_cors::AllowedOrigins::all(),
allowed_methods: vec![Method::Get, Method::Post, Method::Options]
@ -53,7 +55,8 @@ async fn rocket() -> _ {
routes::item::demand_route,
routes::item::transaction_route,
routes::item::inventory_route,
routes::item::variant_stat_route
routes::item::variant_stat_route,
routes::item::unique_field_route
],
)
.manage(itemdb)

View file

@ -55,3 +55,24 @@ pub async fn transaction_route(transaction: &str) -> FallibleApiResponse {
.ok_or_else(|| api_error("No transaction with this UUID"))?;
Ok(t.api_json())
}
/// Returns unique values for a field
#[get("/item/<item_id>/<variant_id>/unique?<field>")]
pub async fn unique_field_route(
item_id: &str,
variant_id: &str,
field: &str,
itemdb: &State<ItemDB>,
) -> FallibleApiResponse {
let variant = itemdb
.get_item(item_id)
.ok_or_else(item_does_not_exist_error)?
.variant(variant_id)
.ok_or_else(variant_does_not_exist_error)?;
match field {
"origin" => Ok(json!(variant.get_unique_origins().await)),
"destination" => Ok(json!(variant.get_unique_destinations().await)),
_ => Err(api_error("Unknown field")),
}
}