@@ -3,7 +3,7 @@ use bb8::RunError;
3
3
use primitives:: { Channel , ChannelId , ValidatorId } ;
4
4
use std:: str:: FromStr ;
5
5
6
- pub use list_channels:: { list_channels, ListChannels } ;
6
+ pub use list_channels:: list_channels;
7
7
8
8
pub async fn get_channel_by_id (
9
9
pool : & DbPool ,
@@ -75,6 +75,7 @@ mod list_channels {
75
75
use bb8:: RunError ;
76
76
use bb8_postgres:: tokio_postgres:: types:: { accepts, FromSql , ToSql , Type } ;
77
77
use chrono:: { DateTime , Utc } ;
78
+ use primitives:: sentry:: ChannelListResponse ;
78
79
use primitives:: { Channel , ValidatorId } ;
79
80
use std:: error:: Error ;
80
81
use std:: str:: FromStr ;
@@ -91,20 +92,14 @@ mod list_channels {
91
92
accepts ! ( VARCHAR , TEXT ) ;
92
93
}
93
94
94
- #[ derive( Debug ) ]
95
- pub struct ListChannels {
96
- pub total_count : u64 ,
97
- pub channels : Vec < Channel > ,
98
- }
99
-
100
95
pub async fn list_channels (
101
96
pool : & DbPool ,
102
97
skip : u64 ,
103
98
limit : u32 ,
104
99
creator : & Option < String > ,
105
100
validator : & Option < ValidatorId > ,
106
101
valid_until_ge : & DateTime < Utc > ,
107
- ) -> Result < ListChannels , RunError < bb8_postgres:: tokio_postgres:: Error > > {
102
+ ) -> Result < ChannelListResponse , RunError < bb8_postgres:: tokio_postgres:: Error > > {
108
103
let validator = validator. as_ref ( ) . map ( |validator_id| {
109
104
serde_json:: Value :: from_str ( & format ! ( r#"[{{"id": "{}"}}]"# , validator_id) )
110
105
. expect ( "Not a valid json" )
@@ -135,12 +130,18 @@ mod list_channels {
135
130
} )
136
131
. await ?;
137
132
138
- Ok ( ListChannels {
139
- total_count : list_channels_total_count (
140
- & pool,
141
- ( & total_count_params. 0 , total_count_params. 1 ) ,
142
- )
143
- . await ?,
133
+ let total_count =
134
+ list_channels_total_count ( & pool, ( & total_count_params. 0 , total_count_params. 1 ) ) . await ?;
135
+
136
+ // fast ceil for total_pages
137
+ let total_pages = if total_count == 0 {
138
+ 1
139
+ } else {
140
+ 1 + ( ( total_count - 1 ) / limit as u64 )
141
+ } ;
142
+
143
+ Ok ( ChannelListResponse {
144
+ total_pages,
144
145
channels,
145
146
} )
146
147
}
0 commit comments