Skip to content

Commit

Permalink
Refactor RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Sep 4, 2024
1 parent e8bdfd5 commit 9457d2b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 40 deletions.
39 changes: 0 additions & 39 deletions src/area/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,12 @@ use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
use tracing::info;

#[derive(Deserialize)]
pub struct SetTagArgs {
pub token: String,
pub id: String,
pub name: String,
pub value: Value,
}

pub async fn set_tag(
Params(args): Params<SetTagArgs>,
pool: Data<Arc<Pool>>,
) -> Result<Area, Error> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
.await??
.unwrap();
let cloned_name = args.name.clone();
let cloned_value = args.value.clone();
let area = pool
.get()
.await?
.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,
args.name,
serde_json::to_string(&args.value)?,
area.name(),
area.id,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
}

#[derive(Deserialize)]
pub struct RemoveTagArgs {
pub token: String,
Expand Down
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod add_element_comment;
pub mod boost_element;
pub mod generate_element_issues;
pub mod get_area;
pub mod set_area_tag;
46 changes: 46 additions & 0 deletions src/rpc/set_area_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::{
area::{self, Area},
auth::Token,
discord, Result,
};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::Deserialize;
use serde_json::Value;
use std::sync::Arc;
use tracing::info;

#[derive(Deserialize)]
pub struct Args {
pub token: String,
pub id: String,
pub name: String,
pub value: Value,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
let token = pool
.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
.await??
.unwrap();
let cloned_name = args.name.clone();
let cloned_value = args.value.clone();
let area = pool
.get()
.await?
.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,
args.name,
serde_json::to_string(&args.value)?,
area.name(),
area.id,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
}
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub async fn run() -> Result<()> {
.with_method("generateelementissues", rpc::generate_element_issues::run)
.with_method("addarea", rpc::add_area::run)
.with_method("getarea", rpc::get_area::run)
.with_method("setareatag", area::rpc::set_tag)
.with_method("setareatag", rpc::set_area_tag::run)
.with_method("removeareatag", area::rpc::remove_tag)
.with_method("gettrendingcountries", area::rpc::get_trending_countries)
.with_method(
Expand Down

0 comments on commit 9457d2b

Please sign in to comment.