1
1
use crate :: prelude:: * ;
2
+ use crate :: hello:: middleware:: Pg ;
2
3
use serde:: Serialize ;
3
- use anansi:: { check, prep } ;
4
+ use anansi:: check;
4
5
use super :: util:: get_query;
5
- use std:: borrow:: Cow ;
6
- use anansi:: db:: DbRow ;
7
6
use rand:: Rng ;
8
- use std:: fmt:: Write ;
9
7
use tokio_postgres:: types:: ToSql ;
10
8
11
- fn update_statement ( num : u16 ) -> String {
12
- let mut pl = 1 ;
13
- let mut q = "UPDATE world SET randomnumber = CASE id " . to_string ( ) ;
14
- for _ in 1 ..=num {
15
- let _ = write ! ( q, "WHEN ${} THEN ${} " , pl, pl + 1 ) ;
16
- pl += 2 ;
17
- }
18
-
19
- q. push_str ( "ELSE randomnumber END WHERE id IN (" ) ;
20
-
21
- for _ in 1 ..=num {
22
- let _ = write ! ( q, "${}," , pl) ;
23
- pl += 1 ;
24
- }
25
-
26
- q. pop ( ) ;
27
- q. push ( ')' ) ;
28
- q
29
- }
30
-
31
9
fn random_num ( ) -> i32 {
32
10
rand:: thread_rng ( ) . gen_range ( 1 ..=10_000 )
33
11
}
@@ -39,36 +17,36 @@ pub struct World {
39
17
}
40
18
41
19
#[ derive( Serialize , Debug ) ]
42
- pub struct Fortune {
20
+ pub struct Fortune < ' a > {
43
21
id : i32 ,
44
- message : Cow < ' static , str > ,
22
+ message : & ' a str ,
45
23
}
46
24
47
25
#[ base_view]
48
26
fn base < R : Request > ( _req : & mut R ) -> Result < Response > { }
49
27
50
28
#[ viewer]
51
- impl < R : Request > WorldView < R > {
29
+ impl < R : Request + Pg > WorldView < R > {
30
+ async fn one_world ( req : & R ) -> Result < World > {
31
+ let row = req. get_world ( ) . await ?;
32
+ let world = World {
33
+ id : row. get_i32 ( 0 ) ,
34
+ randomnumber : row. get_i32 ( 1 ) ,
35
+ } ;
36
+ Ok ( world)
37
+ }
52
38
async fn get_worlds ( req : & R ) -> Result < Vec < World > > {
53
39
let q = get_query ( req. params ( ) ) ;
54
40
let mut worlds = Vec :: with_capacity ( q as usize ) ;
55
41
for _ in 0 ..q {
56
- let row = req. get_world ( ) . await ?;
57
- let world = World {
58
- id : row. try_i32 ( "id" ) ?,
59
- randomnumber : row. try_i32 ( "randomnumber" ) ?,
60
- } ;
42
+ let world = Self :: one_world ( req) . await ?;
61
43
worlds. push ( world) ;
62
44
}
63
45
Ok ( worlds)
64
46
}
65
47
#[ check( Site :: is_visitor) ]
66
48
pub async fn db ( req : & mut R ) -> Result < Response > {
67
- let row = req. get_world ( ) . await ?;
68
- let world = World {
69
- id : row. get_i32 ( 0 ) ,
70
- randomnumber : row. get_i32 ( 1 ) ,
71
- } ;
49
+ let world = Self :: one_world ( req) . await ?;
72
50
Response :: json ( & world)
73
51
}
74
52
#[ check( Site :: is_visitor) ]
@@ -83,11 +61,11 @@ impl<R: Request> WorldView<R> {
83
61
let mut fortunes = Vec :: with_capacity ( rows. len ( ) + 1 ) ;
84
62
fortunes. push ( Fortune {
85
63
id : 0 ,
86
- message : Cow :: Borrowed ( "Additional fortune added at request time." )
64
+ message : "Additional fortune added at request time." ,
87
65
} ) ;
88
66
fortunes. extend ( rows. iter ( ) . map ( |row| Fortune {
89
67
id : row. get ( 0 ) ,
90
- message : Cow :: Owned ( row. get ( 1 ) ) ,
68
+ message : row. get ( 1 ) ,
91
69
} ) ) ;
92
70
fortunes. sort_by ( |it, next| it. message . cmp ( & next. message ) ) ;
93
71
}
@@ -99,7 +77,7 @@ impl<R: Request> WorldView<R> {
99
77
for _ in 0 ..q {
100
78
let row = req. get_world ( ) . await ?;
101
79
let world = World {
102
- id : row. try_i32 ( "id" ) ? ,
80
+ id : row. get_i32 ( 0 ) ,
103
81
randomnumber : random_num ( ) ,
104
82
} ;
105
83
worlds. push ( world) ;
@@ -111,7 +89,7 @@ impl<R: Request> WorldView<R> {
111
89
for world in & worlds {
112
90
params. push ( & world. id ) ;
113
91
}
114
- prep ! ( req, format! ( "update{}" , q ) , update_statement ( q as u16 ) , params. as_slice( ) , execute ) ?;
92
+ req. update_worlds ( q - 1 , params. as_slice ( ) ) . await ?;
115
93
Response :: json ( & worlds)
116
94
}
117
95
#[ check( Site :: is_visitor) ]
0 commit comments