diff --git a/src/area/model.rs b/src/area/model.rs index 5601c90..ec5fa26 100644 --- a/src/area/model.rs +++ b/src/area/model.rs @@ -147,12 +147,6 @@ impl Area { .map_err(Into::into) } - pub fn patch_tag(id: i64, name: &str, value: Value, conn: &Connection) -> Result> { - 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, @@ -323,7 +317,7 @@ const fn mapper() -> fn(&Row) -> rusqlite::Result { 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}; @@ -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()); @@ -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(); diff --git a/src/area/service.rs b/src/area/service.rs index 4e0dcf0..37871d3 100644 --- a/src/area/service.rs +++ b/src/area/service.rs @@ -32,18 +32,11 @@ pub fn insert(tags: Map, conn: &mut Connection) -> Result { 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, + conn: &Connection, ) -> Result { - 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, conn: &Connection) -> Result { if tags.contains_key("url_alias") { return Err(Error::InvalidInput("url_alias can't be changed".into())); } diff --git a/src/rpc/set_area_icon.rs b/src/rpc/set_area_icon.rs index 191dc40..3d67e87 100644 --- a/src/rpc/set_area_icon.rs +++ b/src/rpc/set_area_icon.rs @@ -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"; @@ -37,19 +37,12 @@ pub async fn run(Params(args): Params, pool: Data>) -> Result, pool: Data>) -> Result { 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/{}",