forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.rs
More file actions
74 lines (64 loc) · 2.01 KB
/
Copy pathpost.rs
File metadata and controls
74 lines (64 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use auth::User;
use chrono::NaiveDateTime;
use comment::Comment;
use schema::posts;
#[derive(Queryable, Associations, Identifiable)]
#[belongs_to(User)]
pub struct Post {
pub id: i32,
pub user_id: i32,
pub title: String,
pub body: String,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
pub status: Status,
}
pub enum Status {
Draft,
Published { at: NaiveDateTime },
}
use diesel::pg::Pg;
use diesel::query_source::Queryable;
use diesel::sql_types::{Nullable, Timestamp};
impl Queryable<Nullable<Timestamp>, Pg> for Status {
type Row = Option<NaiveDateTime>;
fn build(row: Self::Row) -> Self {
match row {
Some(at) => Status::Published { at },
None => Status::Draft,
}
}
}
pub fn render(post: &Post, user: &User, comments: &[(Comment, User)]) {
use self::Status::*;
println!("{} (id: {})", post.title, post.id);
println!("By {}", user.username);
let edited_at = post.updated_at.format("%F %T");
match post.status {
Draft => println!("DRAFT (last edited at {})", edited_at),
Published { at } if at != post.updated_at => {
let published_at = at.format("%F %T");
println!(
"Published at {} (last edited at {})",
published_at, edited_at
);
}
Published { at } => println!("Published at {}", at.format("%F %T")),
}
print!("\n");
println!("{}", post.body);
if !comments.is_empty() {
println!("---------------\n");
println!("{} Comments\n", comments.len());
for &(ref comment, ref user) in comments {
let at = comment.created_at.format("%F %T");
print!("{} at {}", user.username, at);
if comment.updated_at != comment.created_at {
let edited = comment.updated_at.format("%F %T");
print!(" (last edited {})", edited);
}
print!("\n{}\n", comment.body);
}
}
print!("===============\n\n");
}