Skip to content

Commit

Permalink
Refactor area model
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Aug 18, 2024
1 parent 746a023 commit 86b0ce0
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 166 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ strip = "symbols"
actix-web = { version = "4.9.0", default-features = false, features = ["macros", "compress-brotli"] }

# https://github.com/rusqlite/rusqlite/releases
rusqlite = { version = "0.31.0", default-features = false, features = ["bundled", "time"] }
rusqlite = { version = "0.31.0", default-features = false, features = ["bundled", "time", "serde_json"] }

# https://github.com/seanmonstar/reqwest/releases
reqwest = { version = "0.12.5", default-features = false, features = ["rustls-tls", "json"] }
Expand Down
28 changes: 18 additions & 10 deletions src/area/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn post(
let area = pool
.get()
.await?
.interact(move |conn| area::service::insert(&args.tags, conn))
.interact(move |conn| area::service::insert(args.tags.clone(), conn))
.await??;
let log_message = format!(
"{} created a new area: https://api.btcmap.org/v3/areas/{}",
Expand All @@ -70,16 +70,20 @@ pub async fn patch(
pool: Data<Arc<Pool>>,
) -> Result<Json<AreaView>, Error> {
let token = auth.check(&req).await?;
let area = Area::select_by_id_or_alias_async(&id_or_alias, &pool)
let cloned_id_or_alias = id_or_alias.clone();
let area = pool
.get()
.await?
.interact(move |conn| Area::select_by_id_or_alias(&cloned_id_or_alias, &conn))
.await??
.ok_or(Error::HttpNotFound(format!(
"There is no area with id or alias = {}",
id_or_alias,
)))?;
let area = pool
.get()
.await?
.interact(move |conn| area::service::patch_tags(area.id, &args.tags, conn))
.interact(move |conn| area::service::patch_tags(area.id, args.tags.clone(), conn))
.await??;
let log_message = format!(
"{} updated area https://api.btcmap.org/v3/areas/{}",
Expand All @@ -98,8 +102,12 @@ pub async fn delete(
pool: Data<Arc<Pool>>,
) -> Result<Json<AreaView>, Error> {
let token = auth.check(&req).await?;
let area = Area::select_by_id_or_alias_async(&id_or_alias, &pool)
let cloned_id_or_alias = id_or_alias.clone();
let area = pool
.get()
.await?
.interact(move |conn| Area::select_by_id_or_alias(&cloned_id_or_alias, &conn))
.await??
.ok_or(Error::HttpNotFound(format!(
"There is no area with id or alias = {}",
id_or_alias,
Expand Down Expand Up @@ -197,7 +205,7 @@ mod test {
#[test]
async fn patch_should_return_401_if_unauthorized() -> Result<()> {
let state = mock_state().await;
Area::insert(&Map::new(), &state.conn)?;
Area::insert(Map::new(), &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
Expand All @@ -221,7 +229,7 @@ mod test {
let url_alias = "test";
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(url_alias.into()));
Area::insert(&tags, &state.conn)?;
Area::insert(tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
Expand All @@ -247,7 +255,7 @@ mod test {
.to_request();
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);
let area = Area::select_by_url_alias(&url_alias, &state.conn)?.unwrap();
let area = Area::select_by_alias(&url_alias, &state.conn)?.unwrap();
assert!(area.tags["string"].is_string());
assert!(area.tags["unsigned"].is_u64());
assert!(area.tags["float"].is_f64());
Expand All @@ -261,7 +269,7 @@ mod test {
let url_alias = "test";
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(url_alias.into()));
Area::insert(&tags, &state.conn)?;
Area::insert(tags, &state.conn)?;
let app = test::init_service(
App::new()
.app_data(Data::new(state.pool))
Expand All @@ -287,7 +295,7 @@ mod test {
let mut tags = Map::new();
tags.insert("url_alias".into(), Value::String(url_alias.into()));
tags.insert("geo_json".into(), phuket_geo_json());
Area::insert(&tags, &state.conn)?;
Area::insert(tags, &state.conn)?;

let area_element = state
.element_repo
Expand Down Expand Up @@ -327,7 +335,7 @@ mod test {
let res = test::call_service(&app, req).await;
assert_eq!(res.status(), StatusCode::OK);

let area: Option<Area> = Area::select_by_url_alias(&url_alias, &state.conn)?;
let area: Option<Area> = Area::select_by_alias(&url_alias, &state.conn)?;
assert!(area.is_some());
assert!(area.unwrap().deleted_at.is_some());

Expand Down
Loading

0 comments on commit 86b0ce0

Please sign in to comment.