rustyroad 1.0.26

Rusty Road is a framework written in Rust that is based on Ruby on Rails. It is designed to provide the familiar conventions and ease of use of Ruby on Rails, while also taking advantage of the performance and efficiency of Rust.
Documentation
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use crate::database::{Database, PoolConnection};
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use serde::Deserialize;
use sqlx::{query_as, FromRow};

/// # Name: Page
/// ### Description: A struct that represents a page created with page
/// ### Example:
/// ```
/// use rustyroad::features::Page;
/// let new_html = Page::new();
/// ```
/// ### Fields:
/// * id: Option<i32>
/// * html_content: String
/// * created_at: DateTime<chrono::Utc>
/// * updated_at: DateTime<chrono::Utc>
/// * associated_user_id: i32
/// * metadata: String
/// ### Methods:
/// * create_new_database_page(new_html: Page) -> Result<serde_json::Value, sqlx::Error>
/// * get_page_by_id(id: i32) -> Result<Page, sqlx::Error>
/// * get_db_pool() -> Result<sqlx::PgPool, sqlx::Error>
/// ### Example:
/// ```
/// use rustyroad::features::Page;
///
/// let new_html = Page::new();
/// let result = Page::create_new_database_page(new_html);
/// ```
/// ### Example:
/// ```
/// use rustyroad::features::Page;
///
/// let id = 1;
///
/// let result = Page::get_page_by_id(id);
/// ```
///
/// ### Example:
/// ```
/// use rustyroad::features::Page;
///
/// let result = Page::get_db_pool();
/// ```
#[derive(Debug, Clone, serde_derive::Serialize, serde_derive::Deserialize, FromRow)]
pub struct Page {
    pub id: Option<i32>,
    pub title: String,
    pub html_content: String,
    pub created_at: Option<DateTime<Utc>>,
    pub updated_at: Option<DateTime<Utc>>,
    pub associated_user_id: i32,
    pub summary: Option<String>,
    pub author: Option<String>,
    pub excerpt: Option<String>,
    pub slug: Option<String>,
    pub page_status: Option<String>,
    pub author_image: Option<String>,
    pub author_thumbnail: Option<String>,
    pub author_url: Option<String>,
    pub featured_image: Option<String>,
    pub featured_image_thumbnail: Option<String>,
    pub seo_title: Option<String>,
    pub seo_description: Option<String>,
    pub seo_keywords: Option<String>,
    pub seo_focus_keyphrase: Option<String>,
    pub seo_canonical_url: Option<String>,
    pub seo_no_index: Option<bool>,
    pub seo_no_follow: Option<bool>,
    pub seo_og_title: Option<String>,
    pub seo_og_locale: Option<String>,
    pub seo_og_type: Option<String>,
    pub seo_og_description: Option<String>,
    pub seo_og_image: Option<String>,
    pub seo_og_image_width: Option<i32>,
    pub seo_og_image_height: Option<i32>,
    pub seo_twitter_title: Option<String>,
    pub seo_twitter_description: Option<String>,
    pub seo_twitter_image: Option<String>,
    pub seo_twitter_image_alt: Option<String>,
    pub seo_twitter_card: Option<String>,
    pub schema_type: Option<String>,
    pub schema_page_type: Option<String>,
    pub schema_article_type: Option<String>,
    pub schema_description: Option<String>,
    pub schema_author: Option<String>,
    pub schema_publisher: Option<String>,
    pub schema_image: Option<String>,
    pub schema_url: Option<String>,
    pub schema_name: Option<String>,
    pub schema_headline: Option<String>,
    pub schema_date_published: Option<NaiveDateTime>,
    pub schema_date_modified: Option<NaiveDateTime>,
    pub is_secure: bool,
    pub is_published: bool,
}

impl Default for Page {
    fn default() -> Self {
        Self::new()
    }
}

impl Page {
    pub fn new() -> Self {
        Self {
            id: None,
            title: "".to_string(),
            html_content: "".to_string(),
            created_at: Some(chrono::Utc::now()),
            updated_at: Some(chrono::Utc::now()),
            associated_user_id: 0,
            summary: None,
            author: None,
            excerpt: None,
            slug: None,
            page_status: None,
            author_image: None,
            author_thumbnail: None,
            author_url: None,
            featured_image: None,
            featured_image_thumbnail: None,
            seo_title: None,
            seo_description: None,
            seo_keywords: None,
            seo_focus_keyphrase: None,
            seo_canonical_url: None,
            seo_no_index: None,
            seo_no_follow: None,
            seo_og_title: None,
            seo_og_locale: None,
            seo_og_type: None,
            seo_og_description: None,
            seo_og_image: None,
            seo_og_image_width: None,
            seo_og_image_height: None,
            seo_twitter_title: None,
            seo_twitter_description: None,
            seo_twitter_image: None,
            seo_twitter_image_alt: None,
            seo_twitter_card: None,
            schema_type: None,
            schema_page_type: None,
            schema_article_type: None,
            schema_description: None,
            schema_author: None,
            schema_publisher: None,
            schema_image: None,
            schema_url: None,
            schema_name: None,
            schema_headline: None,
            schema_date_published: None,
            schema_date_modified: None,
            is_secure: false,
            is_published: false,
        }
    }

    /// # Name: create_page
    /// ### Description: Creates a new database page
    /// ### Parameters: new_html: Page
    /// ### Returns: Result<serde_json::Value, sqlx::Error>
    /// ### Example:
    /// ```
    /// use rustyroad::features::Page;
    ///
    /// let new_html = Page::new();
    /// let result = Page::create_new_database_page(new_html);
    /// ```
    pub async fn create_page(new_html: Page) -> Result<serde_json::Value, sqlx::Error> {
        let sql = r#"INSERT INTO page (
            title,
            html_content,
            associated_user_id,
            summary,
            author,
            excerpt,
            slug,
            page_status,
            author_image,
            author_thumbnail,
            author_url,
            featured_image,
            featured_image_thumbnail,
            seo_title,
            seo_description,
            seo_keywords,
            seo_focus_keyphrase,
            seo_canonical_url,
            seo_no_index,
            seo_no_follow,
            seo_og_title,
            seo_og_locale,
            seo_og_type,
            seo_og_description,
            seo_og_image,
            seo_og_image_width,
            seo_og_image_height,
            seo_twitter_title,
            seo_twitter_description,
            seo_twitter_image,
            seo_twitter_image_alt,
            seo_twitter_card,
            schema_type,
            schema_page_type,
            schema_article_type,
            schema_description,
            schema_author,
            schema_publisher,
            schema_image,
            schema_url,
            schema_name,
            schema_headline,
            schema_date_published,
            schema_date_modified,
            is_secure,
            is_published
        )
        VALUES (
            $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
        ) RETURNING *;
        "#;

        let database = Database::get_database_from_rustyroad_toml().unwrap();

        let pool = Database::get_db_pool(database).await.unwrap();

        let pool_connection = match pool {
            PoolConnection::Pg(pool) => pool,

            _ => panic!("Error getting pg pool"),
        };

        let new_page: Page = query_as(sql)
            .bind(new_html.title)
            .bind(new_html.html_content)
            .bind(new_html.associated_user_id)
            .bind(new_html.summary)
            .bind(new_html.author)
            .bind(new_html.excerpt)
            .bind(new_html.slug)
            .bind(new_html.page_status)
            .bind(new_html.author_image)
            .bind(new_html.author_thumbnail)
            .bind(new_html.author_url)
            .bind(new_html.featured_image)
            .bind(new_html.featured_image_thumbnail)
            .bind(new_html.seo_title)
            .bind(new_html.seo_description)
            .bind(new_html.seo_keywords)
            .bind(new_html.seo_focus_keyphrase)
            .bind(new_html.seo_canonical_url)
            .bind(new_html.seo_no_index)
            .bind(new_html.seo_no_follow)
            .bind(new_html.seo_og_title)
            .bind(new_html.seo_og_locale)
            .bind(new_html.seo_og_type)
            .bind(new_html.seo_og_description)
            .bind(new_html.seo_og_image)
            .bind(new_html.seo_og_image_width)
            .bind(new_html.seo_og_image_height)
            .bind(new_html.seo_twitter_title)
            .bind(new_html.seo_twitter_description)
            .bind(new_html.seo_twitter_image)
            .bind(new_html.seo_twitter_image_alt)
            .bind(new_html.seo_twitter_card)
            .bind(new_html.schema_type)
            .bind(new_html.schema_page_type)
            .bind(new_html.schema_article_type)
            .bind(new_html.schema_description)
            .bind(new_html.schema_author)
            .bind(new_html.schema_publisher)
            .bind(new_html.schema_image)
            .bind(new_html.schema_url)
            .bind(new_html.schema_name)
            .bind(new_html.schema_headline)
            .bind(new_html.schema_date_published)
            .bind(new_html.schema_date_modified)
            .bind(new_html.is_secure)
            .bind(new_html.is_published)
            .fetch_one(&pool_connection)
            .await?;

        Ok(serde_json::json!({
            "status": "success",
            "message": "Page saved successfully",
            "data": new_page
        }))
    }

    /// # Name: get_page_by_id
    /// ### Description: Gets a page by id
    /// ### Parameters: id: i32
    /// ### Returns: Result<serde_json::Value, sqlx::Error>
    /// ### Example:
    /// ```
    /// use rustyroad::features::Page;
    ///
    /// let id = 1;
    /// let result = Page::get_page_by_id(id);
    /// ```
    pub async fn get_page_by_id(id: i32) -> Result<Page, sqlx::Error> {
        let sql = r#"SELECT * FROM page WHERE id = $1"#;
        let database = Database::get_database_from_rustyroad_toml().unwrap();

        let pool = Database::get_db_pool(database).await.unwrap();

        let pool_connection = match pool {
            PoolConnection::Pg(pool) => pool,

            _ => panic!("Error getting pg pool"),
        };

        let page: Page = query_as(sql).bind(id).fetch_one(&pool_connection).await?;
        Ok(page)
    }

    /// # Name: get_page_by_slug
    /// ### Description: Gets a page by slug
    /// ### Parameters: slug: String
    /// ### Returns: Result<serde_json::Value, sqlx::Error>
    /// ### Example:
    /// ```
    /// use rustyroad::features::Page;
    /// let slug = "index";
    /// let result = Page::get_page_by_slug(slug.to_string());
    /// ```
    pub async fn get_page_by_slug(slug: String) -> Result<Page, sqlx::Error> {
        let sql = r#"SELECT * FROM page WHERE slug = $1"#;
        let database = Database::get_database_from_rustyroad_toml().unwrap();

        let pool = Database::get_db_pool(database).await.unwrap();

        let pool_connection = match pool {
            PoolConnection::Pg(pool) => pool,

            _ => panic!("Error getting pg pool"),
        };

        let page: Page = query_as(sql).bind(slug).fetch_one(&pool_connection).await?;
        Ok(page)
    }

    /// # Name: update_page
    /// ### Description: Updates a page
    /// ### Parameters: id: i32, new_html: Page
    /// ### Returns: Result<serde_json::Value, sqlx::Error>
    /// ### Example:
    /// ```
    /// use rustyroad::features::Page;
    /// let id = 1;
    /// let new_html = Page::new();
    /// let result = Page::update_page(id, new_html);
    /// ```
    pub async fn update_page(id: i32, new_html: Page) -> Result<serde_json::Value, sqlx::Error> {
        let sql = r#"
        UPDATE page
        SET
            title = $1,
            html_content = $2,
            created_at = $3,
            associated_user_id = $4,
            summary = $5,
            author = $6,
            excerpt = $7,
            slug = $8,
            page_status = $9,
            author_image = $10,
            author_thumbnail = $11,
            author_url = $12,
            featured_image = $13,
            featured_image_thumbnail = $14,
            seo_title = $15,
            seo_description = $16,
            seo_keywords = $17,
            seo_focus_keyphrase = $18,
            seo_canonical_url = $19,
            seo_no_index = $20,
            seo_no_follow = $21,
            seo_og_title = $22,
            seo_og_locale = $23,
            seo_og_type = $24,
            seo_og_description = $25,
            seo_og_image = $26,
            seo_og_image_width = $27,
            seo_og_image_height = $28,
            seo_twitter_title = $29,
            seo_twitter_description = $30,
            seo_twitter_image = $31,
            seo_twitter_image_alt = $32,
            seo_twitter_card = $33,
            schema_type = $34,
            schema_page_type = $35,
            schema_article_type = $36,
            schema_description = $37,
            schema_author = $38,
            schema_publisher = $39,
            schema_image = $40,
            schema_url = $41,
            schema_name = $42,
            schema_headline = $43,
            schema_date_published = $44,
            schema_date_modified = $45,
            is_secure = $46,
            is_published = $47
        WHERE id = $48
        RETURNING *;
              "#;

        let database = Database::get_database_from_rustyroad_toml().unwrap();

        let pool = Database::get_db_pool(database).await.unwrap();

        let pool_connection = match pool {
            PoolConnection::Pg(pool) => pool,

            _ => panic!("Error getting pg pool"),
        };

        let updated_page: Page = query_as(sql)
            .bind(new_html.title)
            .bind(new_html.html_content)
            .bind(new_html.created_at)
            .bind(new_html.associated_user_id)
            .bind(new_html.summary)
            .bind(new_html.author)
            .bind(new_html.excerpt)
            .bind(new_html.slug)
            .bind(new_html.page_status)
            .bind(new_html.author_image)
            .bind(new_html.author_thumbnail)
            .bind(new_html.author_url)
            .bind(new_html.featured_image)
            .bind(new_html.featured_image_thumbnail)
            .bind(new_html.seo_title)
            .bind(new_html.seo_description)
            .bind(new_html.seo_keywords)
            .bind(new_html.seo_focus_keyphrase)
            .bind(new_html.seo_canonical_url)
            .bind(new_html.seo_no_index)
            .bind(new_html.seo_no_follow)
            .bind(new_html.seo_og_title)
            .bind(new_html.seo_og_locale)
            .bind(new_html.seo_og_type)
            .bind(new_html.seo_og_description)
            .bind(new_html.seo_og_image)
            .bind(new_html.seo_og_image_width)
            .bind(new_html.seo_og_image_height)
            .bind(new_html.seo_twitter_title)
            .bind(new_html.seo_twitter_description)
            .bind(new_html.seo_twitter_image)
            .bind(new_html.seo_twitter_image_alt)
            .bind(new_html.seo_twitter_card)
            .bind(new_html.schema_type)
            .bind(new_html.schema_page_type)
            .bind(new_html.schema_article_type)
            .bind(new_html.schema_description)
            .bind(new_html.schema_author)
            .bind(new_html.schema_publisher)
            .bind(new_html.schema_image)
            .bind(new_html.schema_url)
            .bind(new_html.schema_name)
            .bind(new_html.schema_headline)
            .bind(new_html.schema_date_published)
            .bind(new_html.schema_date_modified)
            .bind(new_html.is_secure)
            .bind(new_html.is_published)
            .bind(id)
            .fetch_one(&pool_connection)
            .await?;

        Ok(serde_json::json!({
            "status": "success",
            "message": "Page updated successfully",
            "data": updated_page
        }))
    }

    /// # Name: get_all_pages
    /// ### Description: Gets all pages
    /// ### Returns: Result<serde_json::Value, sqlx::Error>
    /// ### Example:
    /// ```
    /// use rustyroad::features::Page;
    /// let result = Page::get_all_pages();
    /// ```
    pub async fn get_all_pages() -> Result<serde_json::Value, sqlx::Error> {
        let sql = "SELECT * FROM page";
        let database = Database::get_database_from_rustyroad_toml().unwrap();

        let pool = Database::get_db_pool(database).await.unwrap();

        let pool_connection = match pool {
            PoolConnection::Pg(pool) => pool,

            _ => panic!("Error getting pg pool"),
        };

        let pages: Vec<Page> = query_as(sql).fetch_all(&pool_connection).await?;
        Ok(serde_json::json!({
            "status": "success",
            "message": "Pages retrieved successfully",
            "data": pages
        }))
    }
}

fn deserialize_unix_timestamp<'de, D>(deserializer: D) -> Result<NaiveDateTime, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let timestamp = i64::deserialize(deserializer)?;
    Ok(Utc
        .timestamp_opt(timestamp, 0)
        .single()
        .unwrap()
        .naive_utc())
}