Skip to content

Commit

Permalink
Simplify area queries
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 26, 2023
1 parent 050c1cc commit 9c53f98
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 122 deletions.
6 changes: 3 additions & 3 deletions src/command/add_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{model::Area, Error, Result};
use rusqlite::Connection;
use serde_json::{Map, Value};

pub async fn run(conn: &mut Connection) -> Result<()> {
pub async fn run(conn: &Connection) -> Result<()> {
println!("Adding area");
let mut tags = HashMap::new();

Expand Down Expand Up @@ -99,8 +99,8 @@ pub async fn run(conn: &mut Connection) -> Result<()> {
Some(_) => Err(Error::Other(
"Area with this url_alias already exists".into(),
))?,
None => Area::insert(&tags, &conn)?,
}
None => Area::insert(&tags, conn)?,
};

Ok(())
}
6 changes: 2 additions & 4 deletions src/command/fix_tags.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::HashMap;

use rusqlite::Connection;
use serde_json::Value;
use tracing::warn;
Expand All @@ -16,8 +14,8 @@ pub fn run(conn: &Connection) -> Result<()> {
if geo_json.is_string() {
warn!(area.id, "Found improperly formatted geo_json tag");
let unescaped = geo_json.as_str().unwrap().replace("\\\"", "\"");
let geo_json: HashMap<String, Value> = serde_json::from_str(&unescaped)?;
Area::insert_tag_as_json_obj(area.id, "geo_json", &geo_json, &conn)?;
let geo_json: Value = serde_json::from_str(&unescaped)?;
Area::set_tag(area.id, "geo_json", &geo_json, &conn)?;
warn!(area.id, "Fixed geo_json tag");
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/command/generate_android_icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ impl OverpassElement {
icon_id = "home"
}

if landuse == "farmyard" {
icon_id = "agriculture"
}

if building == "commercial" {
icon_id = "business"
}
Expand Down
4 changes: 2 additions & 2 deletions src/command/import_countries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ pub fn run(path: &str, conn: &mut Connection) -> Result<()> {

match Area::select_by_url_alias(&json.id, &tx)? {
Some(area) => {
Area::merge_tags(area.id, &json.tags, &tx)?;
info!(json.id, "Merged tags into an existing area");
Area::patch_tags(area.id, &json.tags, &tx)?;
info!(json.id, "Patched tags for an existing area");
}
None => {
Area::insert(&json.tags, &tx)?;
Expand Down
8 changes: 4 additions & 4 deletions src/controller/area_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async fn patch_by_url_alias(

match Area::select_by_url_alias(&area_url_alias, &conn)? {
Some(area) => {
Area::merge_tags(area.id, &args.tags, &conn)?;
Area::patch_tags(area.id, &args.tags, &conn)?;
}
None => {
return Err(ApiError::new(
Expand Down Expand Up @@ -192,7 +192,7 @@ async fn patch_tags(
);

match Area::select_by_url_alias(&url_alias, &conn)? {
Some(area) => Area::merge_tags(area.id, &args, &conn)?,
Some(area) => Area::patch_tags(area.id, &args, &conn)?,
None => {
return Err(ApiError::new(
404,
Expand Down Expand Up @@ -228,9 +228,9 @@ async fn post_tags(
match area {
Some(area) => {
if args.value.len() > 0 {
Area::insert_tag_as_str(area.id, &args.name, &args.value, &conn)?;
Area::set_tag(area.id, &args.name, &args.value.clone().into(), &conn)?;
} else {
Area::delete_tag(area.id, &args.name, &conn)?;
Area::remove_tag(area.id, &args.name, &conn)?;
}

Ok(HttpResponse::Created())
Expand Down
10 changes: 9 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fmt::Display};
use std::{collections::HashMap, fmt::Display, num::TryFromIntError};

use actix_web::{http::header::ContentType, HttpResponse, ResponseError};
use reqwest::StatusCode;
Expand All @@ -12,6 +12,7 @@ pub enum Error {
Reqwest(reqwest::Error),
Serde(serde_json::Error),
Api(ApiError),
DbTableRowNotFound,
Other(String),
}

Expand All @@ -25,6 +26,7 @@ impl Display for Error {
Error::Reqwest(err) => err.fmt(f),
Error::Serde(err) => err.fmt(f),
Error::Api(err) => err.fmt(f),
Error::DbTableRowNotFound => write!(f, "DbTableRowNotFound"),
Error::Other(err) => write!(f, "{}", err),
}
}
Expand Down Expand Up @@ -66,6 +68,12 @@ impl From<Error> for std::io::Error {
}
}

impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self {
Error::Other("Integer casting failed".into())
}
}

#[derive(Debug)]
pub struct ApiError {
pub http_code: StatusCode,
Expand Down
Loading

0 comments on commit 9c53f98

Please sign in to comment.