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
pub use shopless_types::Country;
use sqlx::executor::RefExecutor;
use sqlx::postgres::PgQueryAs;
use sqlx::Postgres;
#[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 RefExecutor<'e, Database = Postgres> + Send + 'e,
) -> 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 RefExecutor<'e, Database = Postgres> + Send + 'e,
) -> Result<Address, sqlx::Error> {
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
}