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
use std::fmt::Debug;

use serde::{Deserialize, Serialize};
use sqlx::PgPool;
use uuid::Uuid;

#[derive(sqlx::FromRow, Serialize, Deserialize, Clone)]
pub struct Field {
    pub id: Uuid,
    pub form_id: Uuid,
    pub field_type: String,
}

impl Debug for Field {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Field")
            .field("id", &self.id)
            .field("form_id", &self.form_id)
            .field("field_type", &self.field_type)
            .finish()
    }
}

pub struct NewField {
    pub id: Uuid,
    pub form_id: Uuid,
    pub field_type: String,
}

impl NewField {
    pub fn default() -> Self {
        Self {
            id: Uuid::new_v4(),
            form_id: Uuid::new_v4(),
            field_type: String::from(""),
        }
    }

    pub async fn store(&self, pool: &PgPool) -> Result<(), anyhow::Error> {
        sqlx::query!(
            "INSERT INTO form_input (id, form_id, type)
             VALUES ($1, $2, $3)",
            self.id,
            self.form_id,
            self.field_type,
        )
        .execute(pool)
        .await?;

        Ok(())
    }
}