bridge_common/repo/
pages.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
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
// Copyright 2024 StarfleetAI
// SPDX-License-Identifier: Apache-2.0

use chrono::Utc;
use serde::{Deserialize, Serialize};
use sqlx::{query, query_as, Executor, Postgres};

use crate::types::{
    pages::{Page, ShortPage},
    Result,
};

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct CreateParams {
    pub title: String,
    pub text: String,
}

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct UpdateParams {
    pub title: String,
    pub text: String,
}

/// Create page.
///
/// # Errors
///
/// Returns error if there was a problem while creating page.
pub async fn create<'a, E>(executor: E, company_id: i32, params: CreateParams) -> Result<Page>
where
    E: Executor<'a, Database = Postgres>,
{
    let current_datetime = Utc::now();

    Ok(query_as!(
        Page,
        r#"
        INSERT INTO pages (company_id, title, text, created_at, updated_at)
        VALUES ($1, $2, $3, $4, $4)
        RETURNING *
        "#,
        company_id,
        params.title,
        params.text,
        current_datetime
    )
    .fetch_one(executor)
    .await?)
}

/// List all pages.
///
/// # Errors
///
/// Returns error if there was a problem while accessing database.
pub async fn list<'a, E>(executor: E, company_id: i32) -> Result<Vec<ShortPage>>
where
    E: Executor<'a, Database = Postgres>,
{
    Ok(query_as!(
        ShortPage,
        "SELECT id, title, created_at, updated_at FROM pages WHERE company_id = $1",
        company_id
    )
    .fetch_all(executor)
    .await?)
}

/// Get page by id.
///
/// # Errors
///
/// Returns error if there was a problem while fetching page.
pub async fn get<'a, E>(executor: E, company_id: i32, id: i32) -> Result<Page>
where
    E: Executor<'a, Database = Postgres>,
{
    Ok(query_as!(
        Page,
        "SELECT * FROM pages WHERE company_id = $1 AND id = $2",
        company_id,
        id
    )
    .fetch_one(executor)
    .await?)
}

/// Update page text.
///
/// # Errors
///
/// Returns error if there was a problem while updating page text.
pub async fn update<'a, E>(
    executor: E,
    company_id: i32,
    id: i32,
    data: UpdateParams,
) -> Result<Page>
where
    E: Executor<'a, Database = Postgres>,
{
    let current_datetime = Utc::now();

    Ok(query_as!(
        Page,
        r#"
        UPDATE pages
        SET title = $3, text = $4, updated_at = $5
        WHERE company_id = $1 AND id = $2
        RETURNING *
        "#,
        company_id,
        id,
        data.title,
        data.text,
        current_datetime
    )
    .fetch_one(executor)
    .await?)
}

/// Delete page.
///
/// # Errors
///
/// Returns error if there was a problem while deleting page.
pub async fn delete<'a, E>(executor: E, company_id: i32, id: i32) -> Result<()>
where
    E: Executor<'a, Database = Postgres>,
{
    query!(
        "DELETE FROM pages WHERE company_id = $1 AND id = $2",
        company_id,
        id
    )
    .execute(executor)
    .await?;

    Ok(())
}