From 8d6242dced5937ddb7eed87a6a654b7c1e907021 Mon Sep 17 00:00:00 2001 From: Yuki Kodama Date: Fri, 27 Sep 2019 00:29:10 +0900 Subject: [PATCH] Support _updatedOn field --- README.md | 4 ++-- src/client.rs | 45 +++++++++++++++++++++++++++++++++++++-------- tests/client.rs | 19 +++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5ae41d6..37f996f 100644 --- a/README.md +++ b/README.md @@ -24,13 +24,13 @@ fn main() -> Result<(), Error> { // Create client with let client = Client::new("enjoy_your_first_jsonbox_rs"); - // Put data + // Insert data let data = Data { name: "kuy".into(), message: "Hello, Jsonbox!".into(), }; let (record, meta) = client.create(&data)?; - println!("CREATE: data={:?}, meta={:?}", record, meta); + println!("CREATE: data={:?}, id={} @{}", record, meta.id, meta.created_on); Ok(()) } diff --git a/src/client.rs b/src/client.rs index bcf9587..c8b9195 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,17 +1,41 @@ use reqwest; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use snafu::ResultExt; +use std::convert::From; use crate::error::{self, Error, Result}; use crate::query_builder::QueryBuilder; use crate::url; #[derive(Deserialize, Debug)] -pub struct Meta { +struct MetaRaw { #[serde(rename = "_id")] - pub id: String, + id: String, #[serde(rename = "_createdOn")] + created_on: String, + #[serde(rename = "_updatedOn", default)] + updated_on: Option, +} + +#[derive(Debug)] +pub struct Meta { + pub id: String, pub created_on: String, + pub updated_on: String, +} + +impl From for Meta { + fn from(meta: MetaRaw) -> Self { + let updated_on = match meta.updated_on { + Some(date) => date, + None => meta.created_on.clone(), + }; + Meta { + id: meta.id.clone(), + created_on: meta.created_on.clone(), + updated_on, + } + } } #[derive(Deserialize, Debug)] @@ -52,8 +76,9 @@ impl Client { if res.status().is_success() { let raw = res.text().context(error::Network {})?; let data: T = serde_json::from_str(&raw).context(error::Json { reason: "data" })?; - let meta: Meta = serde_json::from_str(&raw).context(error::Json { reason: "meta" })?; - Ok((data, meta)) + let meta: MetaRaw = + serde_json::from_str(&raw).context(error::Json { reason: "meta" })?; + Ok((data, Meta::from(meta))) } else { let err: ErrorMessage = res.json().context(error::Network {})?; Err(Error::General { @@ -76,8 +101,9 @@ impl Client { if res.status().is_success() { let raw = res.text().context(error::Network {})?; let data: T = serde_json::from_str(&raw).context(error::Json { reason: "data" })?; - let meta: Meta = serde_json::from_str(&raw).context(error::Json { reason: "meta" })?; - Ok((data, meta)) + let meta: MetaRaw = + serde_json::from_str(&raw).context(error::Json { reason: "meta" })?; + Ok((data, Meta::from(meta))) } else { let err: ErrorMessage = res.json().context(error::Network {})?; Err(Error::General { @@ -97,9 +123,12 @@ impl Client { let raw = res.text().context(error::Network {})?; let data: Vec = serde_json::from_str(&raw).context(error::Json { reason: "data" })?; - let meta: Vec = + let meta: Vec = serde_json::from_str(&raw).context(error::Json { reason: "meta" })?; - Ok(data.into_iter().zip(meta.into_iter()).collect()) + Ok(data + .into_iter() + .zip(meta.into_iter().map(|meta| Meta::from(meta))) + .collect()) } else { let err: ErrorMessage = res.json().context(error::Network {})?; Err(Error::General { diff --git a/tests/client.rs b/tests/client.rs index 7291a8e..0083f74 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -29,6 +29,7 @@ fn test_create() { assert_eq!(data.count, 42); assert_eq!(meta.id, "11111111111111111111"); assert_eq!(meta.created_on, "2019-09-22T12:24:37.513Z"); + assert_eq!(meta.updated_on, "2019-09-22T12:24:37.513Z"); } #[test] @@ -148,6 +149,7 @@ fn test_read() { assert_eq!(data.count, 42); assert_eq!(meta.id, "11111111111111111111"); assert_eq!(meta.created_on, "2019-09-22T12:24:37.513Z"); + assert_eq!(meta.updated_on, "2019-09-22T12:24:37.513Z"); } #[test] @@ -172,6 +174,7 @@ fn test_update() { .with_header("content-type", "application/json; charset=utf-8") .with_body(r#"{"message":"Record updated."}"#) .create(); + let client = Client::with_base_url("00000000000000000000", &mockito::server_url()); let data = Data { name: "cargo".into(), @@ -179,6 +182,22 @@ fn test_update() { }; let res = client.update("33333333333333333333", &data); assert!(res.is_ok()); + + let _m = mock("GET", "/00000000000000000000/33333333333333333333") + .with_status(200) + .with_header("content-type", "application/json; charset=utf-8") + .with_body(r#"{"_id":"33333333333333333333","name":"cargo","count":42,"_createdOn":"2019-09-22T12:24:37.513Z","_updatedOn":"2019-09-22T12:25:52.114Z"}"#) + .create(); + + let res = client.read().id::("33333333333333333333"); + assert!(res.is_ok()); + + let (data, meta) = res.unwrap(); + assert_eq!(data.name, "cargo"); + assert_eq!(data.count, 42); + assert_eq!(meta.id, "33333333333333333333"); + assert_eq!(meta.created_on, "2019-09-22T12:24:37.513Z"); + assert_eq!(meta.updated_on, "2019-09-22T12:25:52.114Z"); } #[test]