Skip to content

Commit

Permalink
Update area admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 19, 2023
1 parent c7329ce commit a773977
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
31 changes: 19 additions & 12 deletions src/controller/area_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ struct PostArgs {

#[derive(Serialize, Deserialize)]
struct PostJsonArgs {
id: String,
tags: HashMap<String, Value>,
}

Expand Down Expand Up @@ -87,28 +86,36 @@ async fn post_json(
) -> Result<impl Responder, ApiError> {
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),
})))
}

Expand Down Expand Up @@ -355,8 +362,8 @@ mod tests {

let args = r#"
{
"id": "test-area",
"tags": {
"url_alias": "test-area",
"string": "bar",
"int": 5,
"float": 12.34,
Expand Down
16 changes: 5 additions & 11 deletions src/model/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Area {
.prepare(query)?
.query_map(
named_params! { ":limit": limit.unwrap_or(std::i32::MAX) },
full_mapper(),
mapper(),
)?
.collect::<Result<Vec<Area>, _>>()?)
}
Expand All @@ -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::<Result<Vec<Area>, _>>()?)
}
Expand All @@ -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()?)
}
Expand Down Expand Up @@ -190,15 +186,13 @@ impl Area {
}
}

const fn full_mapper() -> fn(&Row) -> Result<Area> {
const fn mapper() -> fn(&Row) -> Result<Area> {
|row: &Row| -> Result<Area> {
let tags: Option<String> = row.get(1)?;
let tags: Option<HashMap<String, Value>> =
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)?,
Expand Down

0 comments on commit a773977

Please sign in to comment.