Skip to content

Commit

Permalink
Shrink query API surface
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 14, 2024
1 parent f5f6a6b commit 7801ca0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 51 deletions.
31 changes: 6 additions & 25 deletions src/area/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,6 @@ impl Area {
.map_err(Into::into)
}

pub fn patch_tag(id: i64, name: &str, value: Value, conn: &Connection) -> Result<Option<Area>> {
let mut patch_set = Map::new();
patch_set.insert(name.into(), value);
Area::patch_tags(id, patch_set, conn)
}

pub fn patch_tags(
id: i64,
tags: Map<String, Value>,
Expand Down Expand Up @@ -323,7 +317,7 @@ const fn mapper() -> fn(&Row) -> rusqlite::Result<Area> {
mod test {
use crate::{area::Area, test::mock_conn, Result};
use geojson::{Feature, GeoJson};
use serde_json::{json, Map, Value};
use serde_json::{json, Map};
use time::ext::NumericalDuration;
use time::{macros::datetime, OffsetDateTime};

Expand Down Expand Up @@ -394,7 +388,11 @@ mod test {
Area::insert(Area::mock_tags(), &conn)?.unwrap();
Area::insert(Area::mock_tags(), &conn)?.unwrap();
Area::insert(Area::mock_tags(), &conn)?.unwrap();
Area::patch_tag(2, "name", "sushi".into(), &conn)?;
Area::patch_tags(
2,
Map::from_iter([("name".into(), "sushi".into())].into_iter()),
&conn,
)?;
assert_eq!(1, Area::select_by_search_query("sus", &conn)?.len());
assert_eq!(1, Area::select_by_search_query("hi", &conn)?.len());
assert_eq!(0, Area::select_by_search_query("sashimi", &conn)?.len());
Expand Down Expand Up @@ -427,23 +425,6 @@ mod test {
Ok(())
}

#[test]
fn patch_tag() -> Result<()> {
let conn = mock_conn();
let area = Area::insert(Area::mock_tags(), &conn)?.ok_or("failed to insert area")?;
let new_tag_name = "new_tag";
let new_tag_value: Value = "new_tag_value".into();
Area::patch_tag(area.id, new_tag_name, new_tag_value.clone(), &conn)?;
assert_eq!(
Some(&new_tag_value),
Area::select_by_id(1, &conn)?
.ok_or("")?
.tags
.get(new_tag_name)
);
Ok(())
}

#[test]
fn patch_tags() -> Result<()> {
let conn = mock_conn();
Expand Down
15 changes: 4 additions & 11 deletions src/area/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,11 @@ pub fn insert(tags: Map<String, Value>, conn: &mut Connection) -> Result<Area> {
Ok(area)
}

pub fn patch_tag(
id_or_alias: &str,
tag_name: &str,
tag_value: &Value,
conn: &mut Connection,
pub fn patch_tags(
area_id_or_alias: &str,
tags: Map<String, Value>,
conn: &Connection,
) -> Result<Area> {
let mut tags = Map::new();
tags.insert(tag_name.to_string(), tag_value.clone());
patch_tags(id_or_alias, tags, conn)
}

fn patch_tags(area_id_or_alias: &str, tags: Map<String, Value>, conn: &Connection) -> Result<Area> {
if tags.contains_key("url_alias") {
return Err(Error::InvalidInput("url_alias can't be changed".into()));
}
Expand Down
15 changes: 4 additions & 11 deletions src/rpc/set_area_icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use base64::prelude::*;
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::Deserialize;
use serde_json::Value;
use serde_json::Map;
use std::{fs::OpenOptions, io::Write, sync::Arc};

const NAME: &str = "set_area_icon";
Expand Down Expand Up @@ -37,19 +37,12 @@ pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<Rp
))?;
file.write_all(&bytes)?;
file.flush()?;
let url = format!("https://static.btcmap.org/images/areas/{file_name}");
let patch_set = Map::from_iter([("icon_square".into(), url.into())].into_iter());
let area = pool
.get()
.await?
.interact(move |conn| {
Area::patch_tag(
area.id,
"icon:square",
Value::String(format!(
"https://static.btcmap.org/images/areas/{file_name}"
)),
conn,
)
})
.interact(move |conn| Area::patch_tags(area.id, patch_set, conn))
.await??
.ok_or("Failed to update area tag")?;
Ok(area.into())
Expand Down
7 changes: 3 additions & 4 deletions src/rpc/set_area_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
use deadpool_sqlite::Pool;
use jsonrpc_v2::{Data, Params};
use serde::Deserialize;
use serde_json::Value;
use serde_json::{Map, Value};
use std::sync::Arc;
use tracing::info;

Expand All @@ -23,12 +23,11 @@ pub struct Args {

pub async fn run(Params(args): Params<Args>, pool: Data<Arc<Pool>>) -> Result<RpcArea> {
let admin = admin::service::check_rpc(&args.password, NAME, &pool).await?;
let cloned_name = args.name.clone();
let cloned_value = args.value.clone();
let patch_set = Map::from_iter([(args.name.clone(), args.value.clone())].into_iter());
let area = pool
.get()
.await?
.interact(move |conn| area::service::patch_tag(&args.id, &cloned_name, &cloned_value, conn))
.interact(move |conn| area::service::patch_tags(&args.id, patch_set, conn))
.await??;
let log_message = format!(
"{} set tag {} = {} for area {} https://api.btcmap.org/v3/areas/{}",
Expand Down

0 comments on commit 7801ca0

Please sign in to comment.