File tree 4 files changed +20
-11
lines changed
4 files changed +20
-11
lines changed Original file line number Diff line number Diff line change @@ -2,11 +2,15 @@ use mysql;
2
2
use mysql:: params;
3
3
use mysql:: prelude:: * ;
4
4
5
- pub struct Inventory { }
5
+ pub trait Inventory {
6
+ fn add_items ( & mut self , tx : & mut mysql:: Transaction , amount : i64 ) -> crate :: Result < ( ) > ;
7
+ }
8
+
9
+ pub struct MySqlInventory { }
6
10
7
- impl Inventory {
11
+ impl Inventory for MySqlInventory {
8
12
// アイテムを指定された量だけ追加する
9
- pub fn add_items ( & mut self , tx : & mut mysql:: Transaction , amount : i64 ) -> crate :: Result < ( ) > {
13
+ fn add_items ( & mut self , tx : & mut mysql:: Transaction , amount : i64 ) -> crate :: Result < ( ) > {
10
14
let user_id: i64 = 1 ;
11
15
let item_id: i64 = 1 ;
12
16
let stmt = "\
Original file line number Diff line number Diff line change @@ -15,8 +15,8 @@ fn main() -> Result<()> {
15
15
let pool = mysql:: Pool :: new ( url) ?;
16
16
let mut conn = pool. get_conn ( ) ?;
17
17
18
- let mut inventory = Inventory { } ;
19
- let mut wallet = Wallet { } ;
18
+ let mut inventory = MySqlInventory { } ;
19
+ let mut wallet = MySqlWallet { } ;
20
20
21
21
purchase_item ( & mut inventory, & mut wallet, & mut conn) ?;
22
22
Original file line number Diff line number Diff line change @@ -6,8 +6,8 @@ use anyhow::anyhow;
6
6
7
7
// アイテムを購入する
8
8
pub fn purchase_item (
9
- inventory : & mut Inventory ,
10
- wallet : & mut Wallet ,
9
+ inventory : & mut impl Inventory ,
10
+ wallet : & mut impl Wallet ,
11
11
conn : & mut mysql:: PooledConn
12
12
) -> crate :: Result < ( ) > {
13
13
Original file line number Diff line number Diff line change @@ -3,11 +3,16 @@ use mysql::params;
3
3
use mysql:: prelude:: * ;
4
4
use anyhow:: anyhow;
5
5
6
- pub struct Wallet { }
6
+ pub trait Wallet {
7
+ fn get_balance ( & self , tx : & mut mysql:: Transaction ) -> crate :: Result < i64 > ;
8
+ fn pay ( & mut self , tx : & mut mysql:: Transaction , amount : i64 ) -> crate :: Result < ( ) > ;
9
+ }
10
+
11
+ pub struct MySqlWallet { }
7
12
8
- impl Wallet {
13
+ impl Wallet for MySqlWallet {
9
14
// 現在の残高を返す
10
- pub fn get_balance ( & self , tx : & mut mysql:: Transaction ) -> crate :: Result < i64 > {
15
+ fn get_balance ( & self , tx : & mut mysql:: Transaction ) -> crate :: Result < i64 > {
11
16
let user_id: i64 = 1 ;
12
17
let query = "\
13
18
SELECT `balance` \
@@ -21,7 +26,7 @@ impl Wallet {
21
26
}
22
27
23
28
// 残高を減らす
24
- pub fn pay ( & mut self , tx : & mut mysql:: Transaction , amount : i64 ) -> crate :: Result < ( ) > {
29
+ fn pay ( & mut self , tx : & mut mysql:: Transaction , amount : i64 ) -> crate :: Result < ( ) > {
25
30
let user_id: i64 = 1 ;
26
31
let stmt = "\
27
32
UPDATE `wallet` \
You can’t perform that action at this time.
0 commit comments