@@ -9,66 +9,60 @@ use hyper::{Body, Request, Response};
9
9
use primitives:: adapter:: Adapter ;
10
10
use primitives:: { Channel , ChannelId } ;
11
11
12
- pub struct ChannelController < ' a , A : Adapter > {
13
- pub app : & ' a Application < A > ,
14
- }
15
12
16
- impl < ' a , A : Adapter > ChannelController < ' a , A > {
17
- pub fn new ( app : & ' a Application < A > ) -> Self {
18
- Self { app }
13
+ mod channel_create {
14
+ use serde:: Serialize ;
15
+
16
+ #[ derive( Serialize ) ]
17
+ pub ( crate ) struct ChannelCreateResponse {
18
+ pub success : bool ,
19
19
}
20
+ }
20
21
21
- pub async fn channel ( & self , req : Request < Body > ) -> Result < Response < Body > , ResponseError > {
22
- let body = req. into_body ( ) . try_concat ( ) . await ?;
23
- let channel = serde_json:: from_slice :: < Channel > ( & body) ?;
22
+ pub async fn create_channel < A : Adapter > ( req : Request < Body > , app : & Application < A > ) -> Result < Response < Body > , ResponseError > {
23
+ let body = req. into_body ( ) . try_concat ( ) . await ?;
24
+ let channel = serde_json:: from_slice :: < Channel > ( & body) ?;
25
+ // insert into database
24
26
25
- let create_response = channel_create:: ChannelCreateResponse {
26
- // @TODO get validate_channel response error
27
- success : self . app . adapter . validate_channel ( & channel) . unwrap_or ( false ) ,
28
- } ;
29
- let body = serde_json:: to_string ( & create_response) ?. into ( ) ;
30
27
31
- Ok ( Response :: builder ( ) . status ( 200 ) . body ( body) . unwrap ( ) )
32
- }
28
+ let create_response = channel_create:: ChannelCreateResponse {
29
+ // @TODO get validate_channel response error
30
+ success : app. adapter . validate_channel ( & channel) . unwrap_or ( false ) ,
31
+ } ;
32
+ let body = serde_json:: to_string ( & create_response) ?. into ( ) ;
33
33
34
- pub async fn channel_list ( & self , req : Request < Body > ) -> Result < Response < Body > , ResponseError > {
35
- // @TODO: Get from Config
36
- let _channel_find_limit = 5 ;
34
+ Ok ( Response :: builder ( ) . status ( 200 ) . body ( body) . unwrap ( ) )
35
+ }
37
36
38
- let query =
39
- serde_urlencoded:: from_str :: < ChannelListQuery > ( & req. uri ( ) . query ( ) . unwrap_or ( "" ) ) ?;
37
+ pub async fn channel_list ( req : Request < Body > ) -> Result < Response < Body > , ResponseError > {
38
+ // @TODO: Get from Config
39
+ let _channel_find_limit = 5 ;
40
40
41
- // @TODO: List all channels returned from the DB
42
- println ! ( "{:?}" , query ) ;
41
+ let query =
42
+ serde_urlencoded :: from_str :: < ChannelListQuery > ( & req . uri ( ) . query ( ) . unwrap_or ( "" ) ) ? ;
43
43
44
- Err ( ResponseError :: NotFound )
45
- }
44
+ // @TODO: List all channels returned from the DB
45
+ println ! ( "{:?}" , query ) ;
46
46
47
- pub async fn last_approved ( & self , req : Request < Body > ) -> Result < Response < Body > , ResponseError > {
48
- // get request params
49
- let route_params = req
50
- . extensions ( )
51
- . get :: < RouteParams > ( )
52
- . expect ( "request should have route params" ) ;
53
- let channel_id = ChannelId :: from_hex ( route_params. index ( 0 ) ) ?;
54
- let channel = get_channel ( & self . app . pool , & channel_id) . await ?. unwrap ( ) ;
55
-
56
- Ok ( Response :: builder ( )
57
- . header ( "Content-type" , "application/json" )
58
- . body ( serde_json:: to_string ( & channel) ?. into ( ) )
59
- . unwrap ( ) )
60
- }
47
+ Err ( ResponseError :: NotFound )
61
48
}
62
49
63
- mod channel_create {
64
- use serde:: Serialize ;
65
-
66
- #[ derive( Serialize ) ]
67
- pub ( crate ) struct ChannelCreateResponse {
68
- pub success : bool ,
69
- }
50
+ pub async fn last_approved < A : Adapter > ( req : Request < Body > , app : & Application < A > ) -> Result < Response < Body > , ResponseError > {
51
+ // get request params
52
+ let route_params = req
53
+ . extensions ( )
54
+ . get :: < RouteParams > ( )
55
+ . expect ( "request should have route params" ) ;
56
+ let channel_id = ChannelId :: from_hex ( route_params. index ( 0 ) ) ?;
57
+ let channel = get_channel ( & app. pool , & channel_id) . await ?. unwrap ( ) ;
58
+
59
+ Ok ( Response :: builder ( )
60
+ . header ( "Content-type" , "application/json" )
61
+ . body ( serde_json:: to_string ( & channel) ?. into ( ) )
62
+ . unwrap ( ) )
70
63
}
71
64
65
+
72
66
mod channel_list {
73
67
use chrono:: { DateTime , Utc } ;
74
68
use serde:: { Deserialize , Deserializer } ;
0 commit comments