1
1
use self :: channel_list:: ChannelListQuery ;
2
- use crate :: db:: { get_channel_by_id, insert_channel} ;
2
+ use crate :: db:: { get_channel_by_id, insert_channel, list_channels, list_channels_total_pages} ;
3
+ use crate :: routes:: channel:: channel_list:: ChannelListResponse ;
3
4
use crate :: success_response;
4
5
use crate :: Application ;
5
6
use crate :: ResponseError ;
@@ -62,16 +63,39 @@ pub async fn create_channel<A: Adapter>(
62
63
Ok ( success_response ( serde_json:: to_string ( & create_response) ?) )
63
64
}
64
65
65
- pub async fn channel_list ( req : Request < Body > ) -> Result < Response < Body > , ResponseError > {
66
- // @TODO: Get from Config
67
- let _channel_find_limit = 5 ;
68
-
66
+ pub async fn channel_list < A : Adapter > (
67
+ req : Request < Body > ,
68
+ app : & Application < A > ,
69
+ ) -> Result < Response < Body > , ResponseError > {
69
70
let query = serde_urlencoded:: from_str :: < ChannelListQuery > ( & req. uri ( ) . query ( ) . unwrap_or ( "" ) ) ?;
70
-
71
- // @TODO: List all channels returned from the DB
72
- println ! ( "{:?}" , query) ;
73
-
74
- Err ( ResponseError :: NotFound )
71
+ let skip = query
72
+ . page
73
+ . checked_mul ( app. config . channels_find_limit . into ( ) )
74
+ . ok_or_else ( || ResponseError :: BadRequest ( "Page and/or limit is too large" . into ( ) ) ) ?;
75
+
76
+ let channels = list_channels (
77
+ & app. pool ,
78
+ skip,
79
+ app. config . channels_find_limit ,
80
+ & query. creator ,
81
+ & query. validator ,
82
+ & query. valid_until_ge ,
83
+ )
84
+ . await ?;
85
+ let total_pages = list_channels_total_pages (
86
+ & app. pool ,
87
+ & query. creator ,
88
+ & query. validator ,
89
+ & query. valid_until_ge ,
90
+ )
91
+ . await ?;
92
+
93
+ let list_response = ChannelListResponse {
94
+ channels,
95
+ total_pages,
96
+ } ;
97
+
98
+ Ok ( success_response ( serde_json:: to_string ( & list_response) ?) )
75
99
}
76
100
77
101
pub async fn last_approved < A : Adapter > (
@@ -94,39 +118,29 @@ pub async fn last_approved<A: Adapter>(
94
118
95
119
mod channel_list {
96
120
use chrono:: { DateTime , Utc } ;
97
- use serde:: { Deserialize , Deserializer } ;
121
+ use primitives:: { Channel , ValidatorId } ;
122
+ use serde:: { Deserialize , Serialize } ;
123
+
124
+ #[ derive( Debug , Serialize ) ]
125
+ #[ serde( rename_all = "camelCase" ) ]
126
+ pub ( super ) struct ChannelListResponse {
127
+ pub channels : Vec < Channel > ,
128
+ pub total_pages : u64 ,
129
+ }
98
130
99
131
#[ derive( Debug , Deserialize ) ]
100
- pub ( crate ) struct ChannelListQuery {
101
- /// page to show, should be >= 1
132
+ pub ( super ) struct ChannelListQuery {
102
133
#[ serde( default = "default_page" ) ]
103
134
pub page : u64 ,
104
- /// channels limit per page, should be >= 1
105
- #[ serde( default = "default_limit" ) ]
106
- pub limit : u32 ,
107
135
/// filters the list on `valid_until >= valid_until_ge`
108
136
#[ serde( default = "Utc::now" ) ]
109
137
pub valid_until_ge : DateTime < Utc > ,
138
+ pub creator : Option < String > ,
110
139
/// filters the channels containing a specific validator if provided
111
- #[ serde( default , deserialize_with = "deserialize_validator" ) ]
112
- pub validator : Option < String > ,
113
- }
114
-
115
- /// Deserialize the `Option<String>`, but if the `String` is empty it will return `None`
116
- fn deserialize_validator < ' de , D > ( de : D ) -> Result < Option < String > , D :: Error >
117
- where
118
- D : Deserializer < ' de > ,
119
- {
120
- let value: String = Deserialize :: deserialize ( de) ?;
121
- let option = Some ( value) . filter ( |string| !string. is_empty ( ) ) ;
122
- Ok ( option)
123
- }
124
-
125
- fn default_limit ( ) -> u32 {
126
- 1
140
+ pub validator : Option < ValidatorId > ,
127
141
}
128
142
129
143
fn default_page ( ) -> u64 {
130
- 1
144
+ 0
131
145
}
132
146
}
0 commit comments