Skip to content

Commit

Permalink
Add setelementtag and removeelementtag RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Aug 28, 2024
1 parent 371247d commit 18e6b64
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 45 deletions.
69 changes: 30 additions & 39 deletions src/area/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Value>,
}

pub async fn create(
Params(args): Params<CreateAreaArgs>,
Params(args): Params<CreateArgs>,
pool: Data<Arc<Pool>>,
) -> Result<Area, Error> {
let token = pool
Expand All @@ -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<GetAreaArgs>, pool: Data<Arc<Pool>>) -> Result<Area, Error> {
pub async fn get(Params(args): Params<GetArgs>, pool: Data<Arc<Pool>>) -> Result<Area, Error> {
pool.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
Expand All @@ -54,49 +54,42 @@ pub async fn get(Params(args): Params<GetAreaArgs>, pool: Data<Arc<Pool>>) -> 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<SetAreaTagArgs>,
Params(args): Params<SetTagArgs>,
pool: Data<Arc<Pool>>,
) -> Result<Area, Error> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&params.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(
&params.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(&params.tag_value)?,
args.name,
serde_json::to_string(&args.value)?,
area.name(),
area.id,
);
Expand All @@ -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<RemoveAreaTagArgs>,
Params(args): Params<RemoveTagArgs>,
pool: Data<Arc<Pool>>,
) -> Result<Area, Error> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&params.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(&params.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,
);
Expand All @@ -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<RemoveAreaArgs>,
Params(args): Params<RemoveArgs>,
pool: Data<Arc<Pool>>,
) -> Result<Area, Error> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&params.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(&params.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/{}",
Expand Down
91 changes: 85 additions & 6 deletions src/element/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GetElementArgs>, pool: Data<Arc<Pool>>) -> Result<Element> {
pub async fn get(Params(args): Params<GetArgs>, pool: Data<Arc<Pool>>) -> Result<Element> {
pool.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
Expand All @@ -34,16 +34,95 @@ pub async fn get(Params(args): Params<GetElementArgs>, pool: Data<Arc<Pool>>) ->
}

#[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<SetTagArgs>, pool: Data<Arc<Pool>>) -> Result<Element> {
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<BoostElementArgs>,
pub async fn remove_tag(
Params(args): Params<RemoveTagArgs>,
pool: Data<Arc<Pool>>,
) -> Result<Element> {
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<BoostArgs>, pool: Data<Arc<Pool>>) -> Result<Element> {
let token = pool
.get()
.await?
Expand Down
2 changes: 2 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 18e6b64

Please sign in to comment.