11use crate :: prelude:: * ;
2+ use crate :: hello:: middleware:: Pg ;
23use serde:: Serialize ;
3- use anansi:: { check, prep } ;
4+ use anansi:: check;
45use super :: util:: get_query;
5- use std:: borrow:: Cow ;
6- use anansi:: db:: DbRow ;
76use rand:: Rng ;
8- use std:: fmt:: Write ;
97use tokio_postgres:: types:: ToSql ;
108
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-
319fn random_num ( ) -> i32 {
3210 rand:: thread_rng ( ) . gen_range ( 1 ..=10_000 )
3311}
@@ -39,36 +17,36 @@ pub struct World {
3917}
4018
4119#[ derive( Serialize , Debug ) ]
42- pub struct Fortune {
20+ pub struct Fortune < ' a > {
4321 id : i32 ,
44- message : Cow < ' static , str > ,
22+ message : & ' a str ,
4523}
4624
4725#[ base_view]
4826fn base < R : Request > ( _req : & mut R ) -> Result < Response > { }
4927
5028#[ 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+ }
5238 async fn get_worlds ( req : & R ) -> Result < Vec < World > > {
5339 let q = get_query ( req. params ( ) ) ;
5440 let mut worlds = Vec :: with_capacity ( q as usize ) ;
5541 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 ?;
6143 worlds. push ( world) ;
6244 }
6345 Ok ( worlds)
6446 }
6547 #[ check( Site :: is_visitor) ]
6648 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 ?;
7250 Response :: json ( & world)
7351 }
7452 #[ check( Site :: is_visitor) ]
@@ -83,11 +61,11 @@ impl<R: Request> WorldView<R> {
8361 let mut fortunes = Vec :: with_capacity ( rows. len ( ) + 1 ) ;
8462 fortunes. push ( Fortune {
8563 id : 0 ,
86- message : Cow :: Borrowed ( "Additional fortune added at request time." )
64+ message : "Additional fortune added at request time." ,
8765 } ) ;
8866 fortunes. extend ( rows. iter ( ) . map ( |row| Fortune {
8967 id : row. get ( 0 ) ,
90- message : Cow :: Owned ( row. get ( 1 ) ) ,
68+ message : row. get ( 1 ) ,
9169 } ) ) ;
9270 fortunes. sort_by ( |it, next| it. message . cmp ( & next. message ) ) ;
9371 }
@@ -99,7 +77,7 @@ impl<R: Request> WorldView<R> {
9977 for _ in 0 ..q {
10078 let row = req. get_world ( ) . await ?;
10179 let world = World {
102- id : row. try_i32 ( "id" ) ? ,
80+ id : row. get_i32 ( 0 ) ,
10381 randomnumber : random_num ( ) ,
10482 } ;
10583 worlds. push ( world) ;
@@ -111,7 +89,7 @@ impl<R: Request> WorldView<R> {
11189 for world in & worlds {
11290 params. push ( & world. id ) ;
11391 }
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 ?;
11593 Response :: json ( & worlds)
11694 }
11795 #[ check( Site :: is_visitor) ]
0 commit comments