1
//! Reading the splits of a transaction and their tags.
2

            
3
use std::collections::HashMap;
4

            
5
use finance::split::Split;
6
use finance::tag::Tag;
7
use sqlx::PgConnection;
8
use sqlx::types::Uuid;
9

            
10
use crate::db::DBError;
11
use crate::error::ServerError;
12
use crate::user::User;
13

            
14
/// Every stored tag of each split by split id, ordered by name and value; a
15
/// name with two values keeps both.
16
///
17
/// # Errors
18
/// A query failing.
19
89047
pub(crate) async fn split_tags_of(
20
89047
    conn: &mut PgConnection,
21
89047
    splits: &[Split],
22
89047
) -> Result<HashMap<Uuid, Vec<Tag>>, sqlx::Error> {
23
89047
    let mut all = HashMap::with_capacity(splits.len());
24
89047
    for split in splits {
25
223020
        all.insert(split.id, tags_of_split(conn, split.id).await?);
26
    }
27
89047
    Ok(all)
28
89047
}
29

            
30
/// The tags of one split on a held connection, in the order of [`split_tags_of`].
31
///
32
/// # Errors
33
/// The query failing.
34
224647
pub(crate) async fn tags_of_split(
35
224647
    conn: &mut PgConnection,
36
224647
    split_id: Uuid,
37
224647
) -> Result<Vec<Tag>, sqlx::Error> {
38
224647
    sqlx::query_file_as!(Tag, "sql/select/tags/by_split.sql", split_id)
39
224647
        .fetch_all(conn)
40
224647
        .await
41
224647
}
42

            
43
/// Logging is left to the caller's boundary, which knows a missing row from a fault.
44
5
fn db_error(err: sqlx::Error) -> ServerError {
45
5
    ServerError::DB(DBError::Sqlx(err))
46
5
}
47

            
48
impl User {
49
    /// Confirms the transaction exists.
50
    ///
51
    /// # Errors
52
    /// `RowNotFound` when it does not, or a failing query.
53
3
    pub async fn require_transaction(&self, tx_id: Uuid) -> Result<(), ServerError> {
54
3
        let mut conn = self.get_connection().await?;
55
2
        sqlx::query_file!("sql/select/transactions/by_id.sql", tx_id)
56
2
            .fetch_one(&mut *conn)
57
2
            .await
58
2
            .map_err(db_error)?;
59
1
        Ok(())
60
3
    }
61

            
62
    /// Confirms the split exists.
63
    ///
64
    /// # Errors
65
    /// `RowNotFound` when it does not, or a failing query.
66
1625
    pub async fn require_split(&self, split_id: Uuid) -> Result<(), ServerError> {
67
1445
        let mut conn = self.get_connection().await?;
68
1444
        sqlx::query_file!("sql/select/splits/tx_id_by_id.sql", &split_id)
69
1444
            .fetch_one(&mut *conn)
70
1444
            .await
71
1444
            .map_err(db_error)?;
72
1442
        Ok(())
73
1445
    }
74

            
75
    /// The splits of a transaction, without their tags.
76
    ///
77
    /// # Errors
78
    /// The connection or the query failing.
79
2945
    pub async fn get_transaction_splits(&self, tx_id: Uuid) -> Result<Vec<Split>, ServerError> {
80
2945
        let mut conn = self.get_connection().await?;
81
2945
        sqlx::query_file_as!(Split, "sql/select/splits/by_transaction.sql", tx_id)
82
2945
            .fetch_all(&mut *conn)
83
2945
            .await
84
2945
            .map_err(db_error)
85
2945
    }
86

            
87
    /// Every stored tag of each of `splits`, as `split_tags_of` reads them.
88
    ///
89
    /// # Errors
90
    /// The connection or a query failing.
91
2580
    pub async fn split_tags_of(
92
2580
        &self,
93
2580
        splits: &[Split],
94
2580
    ) -> Result<HashMap<Uuid, Vec<Tag>>, ServerError> {
95
2580
        let mut conn = self.get_connection().await?;
96
2580
        split_tags_of(&mut conn, splits).await.map_err(db_error)
97
2580
    }
98
}
99

            
100
#[cfg(test)]
101
#[path = "split_tests.rs"]
102
mod tests;