diff --git a/Cargo.lock b/Cargo.lock index 4a100b8..4bfbdf9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/Cargo.toml b/Cargo.toml index 1d01c73..98309aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/docker-compose.yml b/docker-compose.yml index 849b4dd..6525c06 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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 - diff --git a/schema/item.json b/schema/item.json index 90b759b..434382d 100644 --- a/schema/item.json +++ b/schema/item.json @@ -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" + ] } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 92eba48..325f813 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) diff --git a/src/routes/item/mod.rs b/src/routes/item/mod.rs index e51678c..3126612 100644 --- a/src/routes/item/mod.rs +++ b/src/routes/item/mod.rs @@ -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///unique?")] +pub async fn unique_field_route( + item_id: &str, + variant_id: &str, + field: &str, + itemdb: &State, +) -> 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")), + } +}