diff --git a/src/area/rpc.rs b/src/area/rpc.rs index e96f29d..0a52687 100644 --- a/src/area/rpc.rs +++ b/src/area/rpc.rs @@ -8,13 +8,13 @@ use std::sync::Arc; use tracing::info; #[derive(Deserialize)] -pub struct CreateAreaArgs { +pub struct CreateArgs { pub token: String, pub tags: Map, } pub async fn create( - Params(args): Params, + Params(args): Params, pool: Data>, ) -> Result { let token = pool @@ -40,12 +40,12 @@ pub async fn create( } #[derive(Deserialize)] -pub struct GetAreaArgs { +pub struct GetArgs { pub token: String, - pub area_id_or_alias: String, + pub id: String, } -pub async fn get(Params(args): Params, pool: Data>) -> Result { +pub async fn get(Params(args): Params, pool: Data>) -> Result { pool.get() .await? .interact(move |conn| Token::select_by_secret(&args.token, conn)) @@ -54,49 +54,42 @@ pub async fn get(Params(args): Params, pool: Data>) -> Re let area = pool .get() .await? - .interact(move |conn| Area::select_by_id_or_alias(&args.area_id_or_alias, conn)) + .interact(move |conn| Area::select_by_id_or_alias(&args.id, conn)) .await?? .unwrap(); Ok(area) } #[derive(Deserialize)] -pub struct SetAreaTagArgs { +pub struct SetTagArgs { pub token: String, - pub area_id_or_alias: String, - pub tag_name: String, - pub tag_value: Value, + pub id: String, + pub name: String, + pub value: Value, } pub async fn set_tag( - Params(params): Params, + Params(args): Params, pool: Data>, ) -> Result { let token = pool .get() .await? - .interact(move |conn| Token::select_by_secret(¶ms.token, conn)) + .interact(move |conn| Token::select_by_secret(&args.token, conn)) .await?? .unwrap(); - let cloned_tag_name = params.tag_name.clone(); - let cloned_tag_value = params.tag_value.clone(); + let cloned_name = args.name.clone(); + let cloned_value = args.value.clone(); let area = pool .get() .await? - .interact(move |conn| { - area::service::patch_tag( - ¶ms.area_id_or_alias, - &cloned_tag_name, - &cloned_tag_value, - conn, - ) - }) + .interact(move |conn| area::service::patch_tag(&args.id, &cloned_name, &cloned_value, conn)) .await??; let log_message = format!( "{} set tag {} = {} for area {} https://api.btcmap.org/v3/areas/{}", token.owner, - params.tag_name, - serde_json::to_string(¶ms.tag_value)?, + args.name, + serde_json::to_string(&args.value)?, area.name(), area.id, ); @@ -106,34 +99,32 @@ pub async fn set_tag( } #[derive(Deserialize)] -pub struct RemoveAreaTagArgs { +pub struct RemoveTagArgs { pub token: String, - pub area_id_or_alias: String, - pub tag_name: String, + pub id: String, + pub tag: String, } pub async fn remove_tag( - Params(params): Params, + Params(args): Params, pool: Data>, ) -> Result { let token = pool .get() .await? - .interact(move |conn| Token::select_by_secret(¶ms.token, conn)) + .interact(move |conn| Token::select_by_secret(&args.token, conn)) .await?? .unwrap(); - let cloned_tag_name = params.tag_name.clone(); + let cloned_tag = args.tag.clone(); let area = pool .get() .await? - .interact(move |conn| { - area::service::remove_tag(¶ms.area_id_or_alias, &cloned_tag_name, conn) - }) + .interact(move |conn| area::service::remove_tag(&args.id, &cloned_tag, conn)) .await??; let log_message = format!( "{} removed tag {} from area {} https://api.btcmap.org/v3/areas/{}", token.owner, - params.tag_name, + args.tag, area.name(), area.id, ); @@ -143,25 +134,25 @@ pub async fn remove_tag( } #[derive(Deserialize)] -pub struct RemoveAreaArgs { +pub struct RemoveArgs { pub token: String, - pub area_id_or_alias: String, + pub id: String, } pub async fn remove( - Params(params): Params, + Params(args): Params, pool: Data>, ) -> Result { let token = pool .get() .await? - .interact(move |conn| Token::select_by_secret(¶ms.token, conn)) + .interact(move |conn| Token::select_by_secret(&args.token, conn)) .await?? .unwrap(); let area = pool .get() .await? - .interact(move |conn| area::service::soft_delete(¶ms.area_id_or_alias, conn)) + .interact(move |conn| area::service::soft_delete(&args.id, conn)) .await??; let log_message = format!( "{} removed area {} https://api.btcmap.org/v3/areas/{}", diff --git a/src/element/rpc.rs b/src/element/rpc.rs index c2a7486..ba21e11 100644 --- a/src/element/rpc.rs +++ b/src/element/rpc.rs @@ -13,12 +13,12 @@ use time::OffsetDateTime; use tracing::info; #[derive(Deserialize)] -pub struct GetElementArgs { +pub struct GetArgs { pub token: String, pub id: String, } -pub async fn get(Params(args): Params, pool: Data>) -> Result { +pub async fn get(Params(args): Params, pool: Data>) -> Result { pool.get() .await? .interact(move |conn| Token::select_by_secret(&args.token, conn)) @@ -34,16 +34,95 @@ pub async fn get(Params(args): Params, pool: Data>) -> } #[derive(Deserialize)] -pub struct BoostElementArgs { +pub struct SetTagArgs { pub token: String, pub id: String, - pub days: i64, + pub name: String, + pub value: Value, +} + +pub async fn set_tag(Params(args): Params, pool: Data>) -> Result { + let token = pool + .get() + .await? + .interact(move |conn| Token::select_by_secret(&args.token, conn)) + .await?? + .unwrap(); + let element = pool + .get() + .await? + .interact(move |conn| Element::select_by_id_or_osm_id(&args.id, conn)) + .await?? + .unwrap(); + let cloned_name = args.name.clone(); + let cloned_value = args.value.clone(); + let element = pool + .get() + .await? + .interact(move |conn| Element::set_tag(element.id, &cloned_name, &cloned_value, conn)) + .await??; + let log_message = format!( + "{} set tag {} = {} for element {} https://api.btcmap.org/v3/elements/{}", + token.owner, + args.name, + serde_json::to_string(&args.value)?, + element.name(), + element.id, + ); + info!(log_message); + discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await; + Ok(element) +} + +#[derive(Deserialize)] +pub struct RemoveTagArgs { + pub token: String, + pub id: String, + pub tag: String, } -pub async fn boost( - Params(args): Params, +pub async fn remove_tag( + Params(args): Params, pool: Data>, ) -> Result { + let token = pool + .get() + .await? + .interact(move |conn| Token::select_by_secret(&args.token, conn)) + .await?? + .unwrap(); + let element = pool + .get() + .await? + .interact(move |conn| Element::select_by_id_or_osm_id(&args.id, conn)) + .await?? + .unwrap(); + let cloned_tag = args.tag.clone(); + let element = pool + .get() + .await? + .interact(move |conn| Element::remove_tag(element.id, &cloned_tag, conn)) + .await??; + let log_message = format!( + "{} removed tag {} from element {} https://api.btcmap.org/v3/elements/{}", + token.owner, + args.tag, + element.name(), + element.id, + ); + info!(log_message); + discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await; + Ok(element) +} + +#[derive(Deserialize)] +pub struct BoostArgs { + pub token: String, + pub id: String, + pub days: i64, +} + +pub async fn boost(Params(args): Params, pool: Data>) -> Result { let token = pool .get() .await? diff --git a/src/server/mod.rs b/src/server/mod.rs index 38cfde3..6be4d8f 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -82,6 +82,8 @@ pub async fn run() -> Result<()> { jsonrpc_v2::Server::new() .with_data(jsonrpc_v2::Data::new(pool.clone())) .with_method("getelement", element::rpc::get) + .with_method("setelementtag", element::rpc::set_tag) + .with_method("removeelementtag", element::rpc::remove_tag) .with_method("boostelement", element::rpc::boost) .with_method("createarea", area::rpc::create) .with_method("getarea", area::rpc::get)