summaryrefslogtreecommitdiff
path: root/src/db/records.rs
blob: 1f2e5bff55735ecf6401ed2f9c6845108c186d91 (plain) (blame)
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
use crate::db::models::Record;
use crate::db::strings;
use sqlx::types::Uuid;
use sqlx::{Pool, Postgres};

pub async fn create_record(
    pool: &Pool<Postgres>,
    zone_id: &str,
    name: &str,
    record_type: &str,
    content: &str,
    ttl: u32,
) -> Result<Record, sqlx::Error> {
    let mut transaction = pool.begin().await?;
    let zone = sqlx::query_as::<_, Record>(&strings::CREATE_RECORD)
        .bind(zone_id)
        .bind(name)
        .bind(record_type)
        .bind(content)
        .bind(ttl)
        .fetch_one(&mut transaction)
        .await?;
    transaction.commit().await?;
    Ok(zone)
}

pub async fn update_record(
    pool: &Pool<Postgres>,
    record_id: &Uuid,
    name: &str,
    record_type: &str,
    content: &str,
    ttl: u32,
) -> Result<Record, sqlx::Error> {
    let mut transaction = pool.begin().await?;
    let record = sqlx::query_as::<_, Record>(&strings::UPDATE_RECORD)
        .bind(name)
        .bind(record_type)
        .bind(content)
        .bind(ttl)
        .bind(record_id)
        .fetch_one(&mut transaction)
        .await?;
    transaction.commit().await?;
    Ok(record)
}

pub async fn get_records(pool: &Pool<Postgres>, zone_id: &str) -> Result<Vec<Record>, sqlx::Error> {
    let records = sqlx::query_as::<_, Record>(&strings::GET_RECORDS)
        .bind(zone_id)
        .fetch_all(pool)
        .await?;
    Ok(records)
}