From a7739771923b9e8f5019bcde2579b8a28d0ac42c Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Thu, 19 Oct 2023 14:16:51 +0700 Subject: [PATCH] Update area admin API --- src/controller/area_v2.rs | 31 +++++++++++++++++++------------ src/model/area.rs | 16 +++++----------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/controller/area_v2.rs b/src/controller/area_v2.rs index 755dc74..ab69486 100644 --- a/src/controller/area_v2.rs +++ b/src/controller/area_v2.rs @@ -31,7 +31,6 @@ struct PostArgs { #[derive(Serialize, Deserialize)] struct PostJsonArgs { - id: String, tags: HashMap, } @@ -87,28 +86,36 @@ async fn post_json( ) -> Result { let token = get_admin_token(&conn, &req)?; - warn!( - user_id = token.user_id, - area_url = args.id, - "User attempted to create an area", - ); + if !args.tags.contains_key("url_alias") { + Err(ApiError::new(500, format!("url_alias is missing")))? + } + + let url_alias = &args.tags.get("url_alias").unwrap(); + + if !url_alias.is_string() { + Err(ApiError::new(500, format!("url_alias should be a string")))? + } + + let url_alias = url_alias.as_str().unwrap(); + + warn!(token.user_id, url_alias, "User attempted to create an area",); - if let Some(_) = Area::select_by_url_alias(&args.id, &conn)? { + if let Some(_) = Area::select_by_url_alias(url_alias, &conn)? { Err(ApiError::new( 303, - format!("Area with url = {} already exists", args.id), + format!("Area with url_alias = {} already exists", url_alias), ))? } - if let Err(_) = Area::insert_or_replace(&args.id, Some(&args.tags), &conn) { + if let Err(_) = Area::insert_or_replace(url_alias, Some(&args.tags), &conn) { Err(ApiError::new( 500, - format!("Failed to insert area with url = {}", args.id), + format!("Failed to insert area with url_alias = {}", url_alias), ))? } Ok(Json(json!({ - "message": format!("Area with url = {} has been created", args.id), + "message": format!("Area with url_alias = {} has been created", url_alias), }))) } @@ -355,8 +362,8 @@ mod tests { let args = r#" { - "id": "test-area", "tags": { + "url_alias": "test-area", "string": "bar", "int": 5, "float": 12.34, diff --git a/src/model/area.rs b/src/model/area.rs index d3d749e..453f3a8 100644 --- a/src/model/area.rs +++ b/src/model/area.rs @@ -59,7 +59,7 @@ impl Area { .prepare(query)? .query_map( named_params! { ":limit": limit.unwrap_or(std::i32::MAX) }, - full_mapper(), + mapper(), )? .collect::, _>>()?) } @@ -86,7 +86,7 @@ impl Area { .prepare(query)? .query_map( named_params! { ":updated_since": updated_since, ":limit": limit.unwrap_or(std::i32::MAX) }, - full_mapper(), + mapper(), )? .collect::, _>>()?) } @@ -103,11 +103,7 @@ impl Area { WHERE json_extract(tags, '$.url_alias') = :url_alias "#; - let res = conn.query_row( - query, - named_params! { ":url_alias": url_alias }, - full_mapper(), - ); + let res = conn.query_row(query, named_params! { ":url_alias": url_alias }, mapper()); Ok(res.optional()?) } @@ -190,15 +186,13 @@ impl Area { } } -const fn full_mapper() -> fn(&Row) -> Result { +const fn mapper() -> fn(&Row) -> Result { |row: &Row| -> Result { let tags: Option = row.get(1)?; - let tags: Option> = - tags.map(|it| serde_json::from_str(&it).unwrap()); Ok(Area { id: row.get(0)?, - tags: tags, + tags: tags.map(|it| serde_json::from_str(&it).unwrap()), created_at: row.get(2)?, updated_at: row.get(3)?, deleted_at: row.get(4)?,