1
use forms::{COMMODITY_EDIT, FormValues, Row};
2
use server::command::commodity_tag::SetCommodityTag;
3
use sqlx::PgPool;
4
use uuid::Uuid;
5

            
6
use super::test_seed::{seed_commodity, seed_user};
7
use crate::forms::{Failure, load, submit};
8

            
9
29
pub(super) async fn loaded(user: Uuid, commodity: Uuid) -> anyhow::Result<FormValues> {
10
29
    Ok(load(user, &COMMODITY_EDIT, &commodity.to_string()).await?)
11
29
}
12

            
13
43
pub(super) async fn set_tag(
14
43
    user: Uuid,
15
43
    commodity: Uuid,
16
43
    name: &str,
17
43
    value: &str,
18
43
) -> anyhow::Result<()> {
19
43
    SetCommodityTag::new()
20
43
        .user_id(user)
21
43
        .commodity_id(commodity)
22
43
        .tag_name(name.to_string())
23
43
        .tag_value(value.to_string())
24
43
        .run()
25
43
        .await?;
26
43
    Ok(())
27
43
}
28

            
29
12
pub(super) fn tag_row(name: &str, value: &str) -> Row {
30
12
    Row::from([
31
12
        ("name".to_string(), name.to_string()),
32
12
        ("value".to_string(), value.to_string()),
33
12
    ])
34
12
}
35

            
36
/// The values with `tags` replaced by `rows`; `set_rows` keeps old rows, so
37
/// rebuild from the scalar fields.
38
6
pub(super) fn rebuilt(values: &FormValues, rows: Vec<Row>) -> FormValues {
39
6
    let mut edited = FormValues::default();
40
6
    for key in ["id", "revision", "symbol", "name"] {
41
24
        edited.set(key, values.text(key).unwrap_or_default());
42
    }
43
6
    edited.set_rows("tags");
44
6
    for row in rows {
45
12
        edited.push_row("tags", row);
46
    }
47
6
    edited
48
6
}
49

            
50
13
pub(super) async fn invalid_fields(user: Uuid, values: &FormValues) -> anyhow::Result<Vec<String>> {
51
13
    match submit(user, &COMMODITY_EDIT, values).await {
52
15
        Err(Failure::Invalid(errors)) => Ok(errors.into_iter().map(|e| e.field).collect()),
53
        other => anyhow::bail!("expected field errors, got {other:?}"),
54
    }
55
13
}
56

            
57
pub(super) struct Seeded {
58
    pub(super) user: Uuid,
59
    pub(super) commodity: Uuid,
60
}
61

            
62
20
pub(super) async fn seeded(pool: &PgPool) -> anyhow::Result<Seeded> {
63
20
    let user = seed_user(pool).await?;
64
20
    let commodity = seed_commodity(user, "USD").await?;
65
20
    set_tag(user, commodity, "kind", "fiat").await?;
66
20
    set_tag(user, commodity, "color", "green").await?;
67
20
    Ok(Seeded { user, commodity })
68
20
}
69

            
70
4
pub(super) async fn link_raw_tag(
71
4
    pool: &PgPool,
72
4
    commodity: Uuid,
73
4
    name: &str,
74
4
    value: &str,
75
4
) -> anyhow::Result<()> {
76
4
    let tag = Uuid::new_v4();
77
4
    sqlx::query("INSERT INTO tags (id, tag_name, tag_value) VALUES ($1, $2, $3)")
78
4
        .bind(tag)
79
4
        .bind(name)
80
4
        .bind(value)
81
4
        .execute(pool)
82
4
        .await?;
83
4
    sqlx::query("INSERT INTO commodity_tags (commodity_id, tag_id) VALUES ($1, $2)")
84
4
        .bind(commodity)
85
4
        .bind(tag)
86
4
        .execute(pool)
87
4
        .await?;
88
4
    Ok(())
89
4
}