articles_rs/articles/
models.rs

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use chrono::{DateTime, Utc};
use serde::Serialize;
use uuid::Uuid;

use crate::article_repository::DbArticle;

#[derive(Debug, Serialize, Clone)]
pub struct Article {
    pub id: Option<Uuid>,
    pub title: String,
    pub hero_image: String,
    pub slug: String,
    pub description: String,
    pub author: Vec<String>,
    pub status: String,
    pub created: DateTime<Utc>,
    pub content: String,
    pub images: Vec<String>,
    pub source: String,
    pub published: Option<DateTime<Utc>>,
}

impl Article {
    pub fn new(title: &str, slug: &str, description: &str, author: Vec<&str>) -> Self {
        let now = Utc::now();
        Article {
            id: None,
            title: title.to_string(),
            hero_image: "".to_string(),
            slug: slug.to_string(),
            description: description.to_string(),
            author: author.iter().map(|s| s.to_string()).collect(),
            status: "NEW".to_string(),
            created: now,
            content: String::new(),
            images: Vec::new(),
            source: "".to_string(),
            published: None,
        }
    }

    pub fn add_image(&mut self, image_url: String) {
        self.images.push(image_url);
    }

    pub fn add_images(&mut self, image_urls: Vec<String>) {
        self.images.extend(image_urls);
    }
}

impl From<DbArticle> for Article {
    fn from(db_article: DbArticle) -> Self {
        Article {
            id: db_article.id,
            title: db_article.title,
            hero_image: db_article.hero_image,
            slug: db_article.slug,
            description: db_article.description,
            author: db_article
                .author
                .split(",")
                .map(|s| s.to_string())
                .collect(),
            status: db_article.status,
            created: db_article.created,
            content: db_article.content,
            images: Vec::new(),
            source: db_article.source,
            published: db_article.published,
        }
    }
}

impl From<Article> for DbArticle {
    fn from(article: Article) -> Self {
        DbArticle {
            id: article.id,
            title: article.title,
            hero_image: article.hero_image,
            slug: article.slug,
            description: article.description,
            author: article.author.join(","),
            status: article.status,
            created: article.created,
            content: article.content,
            source: article.source,
            published: article.published,
        }
    }
}