Skip to content
This repository has been archived by the owner on Mar 19, 2021. It is now read-only.

Commit

Permalink
feat(cards): add meta to card view
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Aug 29, 2019
1 parent e25683d commit a0aa82d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/models/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
}
}
31 changes: 31 additions & 0 deletions src/routes/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponse>;
Expand Down Expand Up @@ -177,6 +178,33 @@ pub fn toggle_useful(
})
.responder()
}
/// GET /cards/{card_id}/meta/
pub fn meta(auth: AuthOptional, path: Path<CardPath>, state: State<AppState>) -> 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<AppState>) -> Scope<AppState> {
Expand All @@ -189,6 +217,9 @@ pub fn scope(scope: Scope<AppState>) -> Scope<AppState> {
.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);
Expand Down
13 changes: 13 additions & 0 deletions src/views.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use chrono::NaiveDateTime;

/// Serialization for User model
/// Without password field
#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -24,3 +26,14 @@ pub struct UserSettings {
pub display_name: Option<String>,
pub gravatar_email: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CardMeta {
pub title: String,
pub description: String,
pub id: i32,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
pub preview: Option<String>,
}

0 comments on commit a0aa82d

Please sign in to comment.