From a0aa82d52848b0522d3c0ef0a8be5064bd9eff44 Mon Sep 17 00:00:00 2001 From: Sergey Sova Date: Thu, 29 Aug 2019 23:05:03 +0300 Subject: [PATCH] feat(cards): add meta to card view --- src/models/card.rs | 12 ++++++++++++ src/routes/cards.rs | 31 +++++++++++++++++++++++++++++++ src/views.rs | 13 +++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/models/card.rs b/src/models/card.rs index a03e5fc..4502514 100644 --- a/src/models/card.rs +++ b/src/models/card.rs @@ -4,6 +4,7 @@ use crate::models::User; use crate::schema::cards; use crate::slate::plain_serialize; use crate::time; +use crate::views::CardMeta as CardMetaView; use diesel::dsl::sql; use diesel::prelude::*; use serde_json::Value; @@ -207,4 +208,15 @@ impl Card { .get_result(conn) .ok() } + + pub fn encodable_meta(self) -> CardMetaView { + CardMetaView { + id: self.id, + title: self.title, + description: self.content_for_search, + created_at: self.created_at, + updated_at: self.updated_at, + preview: None, + } + } } diff --git a/src/routes/cards.rs b/src/routes/cards.rs index b0c9be5..e7cad61 100644 --- a/src/routes/cards.rs +++ b/src/routes/cards.rs @@ -5,6 +5,7 @@ use serde_json::Value; use crate::app_state::AppState; use crate::auth::{Auth, AuthOptional}; use crate::models::*; +use crate::views::CardMeta as CardMetaView; use actix_web::State; type FutRes = FutureResponse; @@ -177,6 +178,33 @@ pub fn toggle_useful( }) .responder() } +/// GET /cards/{card_id}/meta/ +pub fn meta(auth: AuthOptional, path: Path, state: State) -> FutRes { + use crate::handlers::cards::get::*; + + #[derive(Serialize)] + pub struct R { + meta: CardMetaView, + } + + state + .pg + .send(CardFetch { + card_id: path.card_id, + requester_id: auth.user.map(|user| user.id), + }) + .from_err() + .and_then(|res| match res { + Some(card) => Ok(answer_success!( + Ok, + R { + meta: card.encodable_meta() + } + )), + None => Ok(answer_error!(NotFound, "id_not_found".to_string())), + }) + .responder() +} #[inline] pub fn scope(scope: Scope) -> Scope { @@ -189,6 +217,9 @@ pub fn scope(scope: Scope) -> Scope { .resource("/{card_id}/useful/", |r| { r.post().with(self::toggle_useful); }) + .resource("/{card_id}/meta/", |r| { + r.get().with(self::meta); + }) .resource("/", |r| { r.post().with(self::create); r.get().with(self::list); diff --git a/src/views.rs b/src/views.rs index 7ce437d..c914645 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,3 +1,5 @@ +use chrono::NaiveDateTime; + /// Serialization for User model /// Without password field #[derive(Serialize, Deserialize, Debug)] @@ -24,3 +26,14 @@ pub struct UserSettings { pub display_name: Option, pub gravatar_email: Option, } + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CardMeta { + pub title: String, + pub description: String, + pub id: i32, + pub created_at: Option, + pub updated_at: Option, + pub preview: Option, +}