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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
pub use shopless_types::Country;

#[derive(Debug, PartialEq, Clone, sqlx::FromRow)]
pub struct Address {
    pub id: i64,
    pub recipient: String,
    pub line1: String,
    pub line2: String,
    pub postal_code: String,
    pub city: String,
    pub province_code: Option<String>,
    pub province_name: Option<String>,
    pub country_code: Country,
    pub country_name: String,
    pub phone: Option<String>,
    pub vat: Option<String>,
}

pub async fn fetch<'e>(
    id: i64,
    conn: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
) -> Result<Option<Address>, sqlx::Error> {
    sqlx::query_as("SELECT * FROM addresses WHERE id = $1")
        .bind(id)
        .fetch_optional(conn)
        .await
}

#[derive(Debug)]
pub struct NewAddress<'a> {
    pub recipient: &'a str,
    pub line1: &'a str,
    pub line2: &'a str,
    pub postal_code: &'a str,
    pub city: &'a str,
    pub province_code: Option<&'a str>,
    pub province_name: Option<&'a str>,
    pub country_code: Country,
    pub country_name: &'a str,
    pub phone: Option<&'a str>,
}

pub async fn insert<'e>(
    address: &NewAddress<'_>,
    conn: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
) -> Result<Address, sqlx::Error> {
    // deconstruct to not miss new properties in the future
    let NewAddress {
        recipient,
        line1,
        line2,
        postal_code,
        city,
        province_code,
        province_name,
        country_code,
        country_name,
        phone,
    } = address;

    sqlx::query_as(
        "
            INSERT INTO addresses (
                recipient,
                line1,
                line2,
                postal_code,
                city,
                province_code,
                province_name,
                country_code,
                country_name,
                phone
            ) VALUES (
                $1, $2, $3, $4, $5, $6, $7, $8, $9, $10
            )
            RETURNING *
        ",
    )
    .bind(recipient)
    .bind(line1)
    .bind(line2)
    .bind(postal_code)
    .bind(city)
    .bind(province_code)
    .bind(province_name)
    .bind(country_code)
    .bind(country_name)
    .bind(phone)
    .fetch_one(conn)
    .await
}

#[derive(Debug, PartialEq)]
pub struct FirstLastName<'a> {
    pub first_name: &'a str,
    pub last_name: &'a str,
}

impl Address {
    pub fn first_last_name(&self) -> FirstLastName<'_> {
        let fullname = self.recipient.trim();
        if let Some(ix) = fullname.rfind(' ') {
            let (first_name, last_name) = fullname.split_at(ix);
            FirstLastName {
                first_name,
                last_name: last_name.trim(),
            }
        } else {
            FirstLastName {
                first_name: fullname,
                last_name: fullname,
            }
        }
    }
}

#[cfg(test)]
mod test {
    use super::{Address, FirstLastName};
    use shopless_types::Country;

    impl<'a> FirstLastName<'a> {
        pub fn new(first_name: &'a str, last_name: &'a str) -> Self {
            FirstLastName {
                first_name,
                last_name,
            }
        }
    }

    impl Address {
        fn with_name(fullname: &str) -> Self {
            Address {
                id: 0,
                recipient: fullname.to_string(),
                line1: String::new(),
                line2: String::new(),
                postal_code: String::new(),
                city: String::new(),
                province_code: None,
                province_name: None,
                country_code: Country::DE,
                country_name: String::new(),
                phone: None,
                vat: None,
            }
        }
    }

    #[test]
    fn first_last_name_split() {
        assert_eq!(
            Address::with_name("Foo Bar").first_last_name(),
            FirstLastName::new("Foo", "Bar")
        );
        assert_eq!(
            Address::with_name("Foo Bar ").first_last_name(),
            FirstLastName::new("Foo", "Bar")
        );
        assert_eq!(
            Address::with_name("Foo A Bar").first_last_name(),
            FirstLastName::new("Foo A", "Bar")
        );
        assert_eq!(
            Address::with_name("Foo A-Bar").first_last_name(),
            FirstLastName::new("Foo", "A-Bar")
        );
        assert_eq!(
            Address::with_name("Foo Bar GmbH - EDV").first_last_name(),
            FirstLastName::new("Foo Bar GmbH -", "EDV")
        );
        assert_eq!(
            Address::with_name("Foobar").first_last_name(),
            FirstLastName::new("Foobar", "Foobar")
        );
    }
}