Skip to content

Commit

Permalink
Add OSM user mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 24, 2023
1 parent 0112654 commit e0abeb0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 12 deletions.
19 changes: 10 additions & 9 deletions src/controller/user_v2.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::model::user;
use crate::model::OsmUserJson;
use crate::model::User;
use crate::service::auth::get_admin_token;
use crate::ApiError;
Expand Down Expand Up @@ -28,8 +29,8 @@ pub struct GetArgs {

#[derive(Serialize, Deserialize)]
pub struct GetItem {
pub id: i64,
pub osm_json: Value,
pub id: i32,
pub osm_json: OsmUserJson,
pub tags: Map<String, Value>,
pub created_at: String,
pub updated_at: String,
Expand Down Expand Up @@ -186,7 +187,7 @@ mod tests {
user::INSERT,
named_params! {
":id": 1,
":osm_json": "{}",
":osm_json": serde_json::to_string(&OsmUserJson::mock())?,
},
)?;

Expand All @@ -210,12 +211,12 @@ mod tests {
db::migrate(&mut conn)?;

conn.execute(
"INSERT INTO user (id, osm_json, updated_at) VALUES (1, '{}', '2022-01-05')",
[],
"INSERT INTO user (id, osm_json, updated_at) VALUES (1, ?, '2022-01-05')",
[serde_json::to_string(&OsmUserJson::mock())?],
)?;
conn.execute(
"INSERT INTO user (id, osm_json, updated_at) VALUES (2, '{}', '2022-02-05')",
[],
"INSERT INTO user (id, osm_json, updated_at) VALUES (2, ?, '2022-02-05')",
[serde_json::to_string(&OsmUserJson::mock())?],
)?;

let app = test::init_service(
Expand Down Expand Up @@ -244,7 +245,7 @@ mod tests {
user::INSERT,
named_params! {
":id": user_id,
":osm_json": "{}",
":osm_json": serde_json::to_string(&OsmUserJson::mock())?,
},
)?;

Expand Down Expand Up @@ -277,7 +278,7 @@ mod tests {
user::INSERT,
named_params! {
":id": user_id,
":osm_json": "{}",
":osm_json": serde_json::to_string(&OsmUserJson::mock())?,
},
)?;

Expand Down
3 changes: 3 additions & 0 deletions src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ pub use token::Token;

pub mod overpass_element_json;
pub use overpass_element_json::OverpassElementJson;

pub mod osm_user_json;
pub use osm_user_json::OsmUserJson;
71 changes: 71 additions & 0 deletions src/model/osm_user_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

#[derive(Serialize, Deserialize)]
pub struct OsmUserJson {
pub id: i32,
pub display_name: String,
#[serde(with = "time::serde::rfc3339")]
pub account_created: OffsetDateTime,
pub description: String,
pub contributor_terms: ContributorTerms,
pub img: Option<Img>,
pub roles: Vec<String>,
pub changesets: Changesets,
pub traces: Traces,
pub blocks: Blocks,
}

#[derive(Serialize, Deserialize)]
pub struct ContributorTerms {
pub agreed: bool,
}

#[derive(Serialize, Deserialize)]
pub struct Img {
pub href: String,
}

#[derive(Serialize, Deserialize)]
pub struct Changesets {
pub count: i32,
}

#[derive(Serialize, Deserialize)]
pub struct Traces {
pub count: i32,
}

#[derive(Serialize, Deserialize)]
pub struct Blocks {
received: BlocksReceived,
}

#[derive(Serialize, Deserialize)]
pub struct BlocksReceived {
count: i32,
active: i32,
}

impl OsmUserJson {
#[cfg(test)]
pub fn mock() -> OsmUserJson {
OsmUserJson {
id: 1,
display_name: "".into(),
account_created: OffsetDateTime::now_utc(),
description: "".into(),
contributor_terms: ContributorTerms { agreed: true },
img: None,
roles: vec![],
changesets: Changesets { count: 0 },
traces: Traces { count: 0 },
blocks: Blocks {
received: BlocksReceived {
count: 0,
active: 0,
},
},
}
}
}
8 changes: 5 additions & 3 deletions src/model/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use rusqlite::Row;
use serde_json::Map;
use serde_json::Value;

use super::OsmUserJson;

pub struct User {
pub id: i64,
pub osm_json: Value,
pub id: i32,
pub osm_json: OsmUserJson,
pub tags: Map<String, Value>,
pub created_at: String,
pub updated_at: String,
Expand Down Expand Up @@ -82,7 +84,7 @@ pub static UPDATE_OSM_JSON: &str = r#"
const fn full_mapper() -> fn(&Row) -> Result<User> {
|row: &Row| -> Result<User> {
let osm_json: String = row.get(1)?;
let osm_json: Value = serde_json::from_str(&osm_json).unwrap_or_default();
let osm_json: OsmUserJson = serde_json::from_str(&osm_json).unwrap();

let tags: String = row.get(2)?;
let tags: Map<String, Value> = serde_json::from_str(&tags).unwrap_or_default();
Expand Down

0 comments on commit e0abeb0

Please sign in to comment.