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 ba19e38 commit e8bdfd5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
21 changes: 0 additions & 21 deletions src/area/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,6 @@ use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;
use tracing::info;

#[derive(Deserialize)]
pub struct GetArgs {
pub token: String,
pub id: String,
}

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))
.await??
.unwrap();
let area = pool
.get()
.await?
.interact(move |conn| Area::select_by_id_or_alias(&args.id, conn))
.await??
.unwrap();
Ok(area)
}

#[derive(Deserialize)]
pub struct SetTagArgs {
pub token: String,
Expand Down
5 changes: 3 additions & 2 deletions src/rpc/add_area.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{area, discord, Error};
use crate::Result;
use crate::{area, discord};
use crate::{area::Area, auth::Token};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
Expand All @@ -13,7 +14,7 @@ pub struct Args {
pub tags: Map<String, Value>,
}

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area, Error> {
pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
let token = pool
.get()
.await?
Expand Down
26 changes: 26 additions & 0 deletions src/rpc/get_area.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::{area::Area, auth::Token, Result};
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::Deserialize;
use std::sync::Arc;

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

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Area> {
pool.get()
.await?
.interact(move |conn| Token::select_by_secret(&args.token, conn))
.await??
.unwrap();
let area = pool
.get()
.await?
.interact(move |conn| Area::select_by_id_or_alias(&args.id, conn))
.await??
.unwrap();
Ok(area)
}
1 change: 1 addition & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod add_area;
pub mod add_element_comment;
pub mod boost_element;
pub mod generate_element_issues;
pub mod get_area;
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub async fn run() -> Result<()> {
.with_method("addelementcomment", rpc::add_element_comment::run)
.with_method("generateelementissues", rpc::generate_element_issues::run)
.with_method("addarea", rpc::add_area::run)
.with_method("getarea", area::rpc::get)
.with_method("getarea", rpc::get_area::run)
.with_method("setareatag", area::rpc::set_tag)
.with_method("removeareatag", area::rpc::remove_tag)
.with_method("gettrendingcountries", area::rpc::get_trending_countries)
Expand Down

0 comments on commit e8bdfd5

Please sign in to comment.