1
1
use accounts:: { AccountId , AccountsManager } ;
2
+ use sqlx:: postgres:: { PgConnectOptions , PgPoolOptions } ;
2
3
use sqlx:: { Acquire , PgConnection , PgPool , Postgres } ;
3
4
use time:: OffsetDateTime ;
4
5
use uuid:: Uuid ;
5
- use sqlx:: postgres:: { PgConnectOptions , PgPoolOptions } ;
6
6
7
7
#[ derive( sqlx:: Type , Copy , Clone , Debug ) ]
8
8
#[ sqlx( transparent) ]
@@ -41,17 +41,13 @@ pub struct PaymentsManager {
41
41
}
42
42
43
43
impl PaymentsManager {
44
- pub async fn setup (
45
- opts : PgConnectOptions ,
46
- ) -> sqlx:: Result < Self > {
44
+ pub async fn setup ( opts : PgConnectOptions ) -> sqlx:: Result < Self > {
47
45
let pool = PgPoolOptions :: new ( )
48
46
. max_connections ( 5 )
49
47
. connect_with ( opts)
50
48
. await ?;
51
49
52
- sqlx:: migrate!( )
53
- . run ( & pool)
54
- . await ?;
50
+ sqlx:: migrate!( ) . run ( & pool) . await ?;
55
51
56
52
Ok ( Self { pool } )
57
53
}
@@ -61,8 +57,8 @@ impl PaymentsManager {
61
57
pub async fn create (
62
58
& self ,
63
59
account_id : AccountId ,
64
- currency : & str ,
65
- amount : rust_decimal:: Decimal ,
60
+ currency : & str ,
61
+ amount : rust_decimal:: Decimal ,
66
62
) -> sqlx:: Result < Payment > {
67
63
// Check-out a connection to avoid paying the overhead of acquiring one for each call.
68
64
let mut conn = self . pool . acquire ( ) . await ?;
@@ -91,8 +87,8 @@ impl PaymentsManager {
91
87
currency,
92
88
amount,
93
89
)
94
- . fetch_one ( & mut * conn)
95
- . await ?;
90
+ . fetch_one ( & mut * conn)
91
+ . await ?;
96
92
97
93
// We then create the record with the payment vendor...
98
94
let external_payment_id = "foobar1234" ;
@@ -103,17 +99,17 @@ impl PaymentsManager {
103
99
// the order of columns gets baked into the binary, so if it changes between compile time and
104
100
// run-time, you may run into errors.
105
101
let payment = sqlx:: query_as!(
106
- Payment ,
107
- "update payment \
102
+ Payment ,
103
+ "update payment \
108
104
set status = $1, external_payment_id = $2 \
109
105
where payment_id = $3 \
110
106
returning *",
111
- PaymentStatus :: Created ,
112
- external_payment_id,
113
- payment_id. 0 ,
114
- )
115
- . fetch_one ( & mut * conn)
116
- . await ?;
107
+ PaymentStatus :: Created ,
108
+ external_payment_id,
109
+ payment_id. 0 ,
110
+ )
111
+ . fetch_one ( & mut * conn)
112
+ . await ?;
117
113
118
114
Ok ( payment)
119
115
}
@@ -125,9 +121,7 @@ impl PaymentsManager {
125
121
"select * from payment where payment_id = $1" ,
126
122
payment_id. 0
127
123
)
128
- . fetch_optional ( & self . pool )
129
- . await
124
+ . fetch_optional ( & self . pool )
125
+ . await
130
126
}
131
127
}
132
-
133
-
0 commit comments