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 e090be8 commit ba19e38
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 34 deletions.
34 changes: 1 addition & 33 deletions src/area/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,13 @@ use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
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 CreateArgs {
pub token: String,
pub tags: Map<String, Value>,
}

pub async fn create(
Params(args): Params<CreateArgs>,
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 area = pool
.get()
.await?
.interact(move |conn| area::service::insert(args.tags, conn))
.await??;
let log_message = format!(
"{} created area {} https://api.btcmap.org/v3/areas/{}",
token.owner,
area.name(),
area.id,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
}

#[derive(Deserialize)]
pub struct GetArgs {
pub token: String,
Expand Down
37 changes: 37 additions & 0 deletions src/rpc/add_area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{area, discord, Error};
use crate::{area::Area, auth::Token};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::Deserialize;
use serde_json::{Map, Value};
use std::sync::Arc;
use tracing::info;

#[derive(Deserialize)]
pub struct Args {
pub token: String,
pub tags: Map<String, Value>,
}

pub async fn run(Params(args): Params<Args>, 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 area = pool
.get()
.await?
.interact(move |conn| area::service::insert(args.tags, conn))
.await??;
let log_message = format!(
"{} created area {} https://api.btcmap.org/v3/areas/{}",
token.owner,
area.name(),
area.id,
);
info!(log_message);
discord::send_message_to_channel(&log_message, discord::CHANNEL_API).await;
Ok(area)
}
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod add_area;
pub mod add_element_comment;
pub mod boost_element;
pub mod generate_element_issues;
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub async fn run() -> Result<()> {
.with_method("boostelement", rpc::boost_element::run)
.with_method("addelementcomment", rpc::add_element_comment::run)
.with_method("generateelementissues", rpc::generate_element_issues::run)
.with_method("createarea", area::rpc::create)
.with_method("addarea", rpc::add_area::run)
.with_method("getarea", area::rpc::get)
.with_method("setareatag", area::rpc::set_tag)
.with_method("removeareatag", area::rpc::remove_tag)
Expand Down

0 comments on commit ba19e38

Please sign in to comment.