Skip to content

Commit 3dd8472

Browse files
committed
use ChannelListResponse
1 parent 75cc08f commit 3dd8472

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

sentry/src/db/channel.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bb8::RunError;
33
use primitives::{Channel, ChannelId, ValidatorId};
44
use std::str::FromStr;
55

6-
pub use list_channels::{list_channels, ListChannels};
6+
pub use list_channels::list_channels;
77

88
pub async fn get_channel_by_id(
99
pool: &DbPool,
@@ -75,6 +75,7 @@ mod list_channels {
7575
use bb8::RunError;
7676
use bb8_postgres::tokio_postgres::types::{accepts, FromSql, ToSql, Type};
7777
use chrono::{DateTime, Utc};
78+
use primitives::sentry::ChannelListResponse;
7879
use primitives::{Channel, ValidatorId};
7980
use std::error::Error;
8081
use std::str::FromStr;
@@ -91,20 +92,14 @@ mod list_channels {
9192
accepts!(VARCHAR, TEXT);
9293
}
9394

94-
#[derive(Debug)]
95-
pub struct ListChannels {
96-
pub total_count: u64,
97-
pub channels: Vec<Channel>,
98-
}
99-
10095
pub async fn list_channels(
10196
pool: &DbPool,
10297
skip: u64,
10398
limit: u32,
10499
creator: &Option<String>,
105100
validator: &Option<ValidatorId>,
106101
valid_until_ge: &DateTime<Utc>,
107-
) -> Result<ListChannels, RunError<bb8_postgres::tokio_postgres::Error>> {
102+
) -> Result<ChannelListResponse, RunError<bb8_postgres::tokio_postgres::Error>> {
108103
let validator = validator.as_ref().map(|validator_id| {
109104
serde_json::Value::from_str(&format!(r#"[{{"id": "{}"}}]"#, validator_id))
110105
.expect("Not a valid json")
@@ -135,12 +130,18 @@ mod list_channels {
135130
})
136131
.await?;
137132

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,
144145
channels,
145146
})
146147
}

sentry/src/routes/channel.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use futures::TryStreamExt;
88
use hex::FromHex;
99
use hyper::{Body, Request, Response};
1010
use primitives::adapter::Adapter;
11-
use primitives::sentry::{ChannelListResponse, SuccessResponse};
11+
use primitives::sentry::SuccessResponse;
1212
use primitives::{Channel, ChannelId};
1313
use slog::error;
1414

@@ -72,7 +72,7 @@ pub async fn channel_list<A: Adapter>(
7272
.checked_mul(app.config.channels_find_limit.into())
7373
.ok_or_else(|| ResponseError::BadRequest("Page and/or limit is too large".into()))?;
7474

75-
let list_channels = list_channels(
75+
let list_response = list_channels(
7676
&app.pool,
7777
skip,
7878
app.config.channels_find_limit,
@@ -82,18 +82,6 @@ pub async fn channel_list<A: Adapter>(
8282
)
8383
.await?;
8484

85-
// fast ceil for total_pages
86-
let total_pages = if list_channels.total_count == 0 {
87-
1
88-
} else {
89-
1 + ((list_channels.total_count - 1) / app.config.channels_find_limit as u64)
90-
};
91-
92-
let list_response = ChannelListResponse {
93-
channels: list_channels.channels,
94-
total_pages,
95-
};
96-
9785
Ok(success_response(serde_json::to_string(&list_response)?))
9886
}
9987

0 commit comments

Comments
 (0)