You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-29 22:01:14 +03:00
storage: simplify the paginated queries
This commit is contained in:
@ -23,7 +23,7 @@ use uuid::Uuid;
|
||||
use crate::{
|
||||
pagination::{Page, QueryBuilderExt},
|
||||
tracing::ExecuteExt,
|
||||
Clock, DatabaseError, LookupResultExt,
|
||||
Clock, DatabaseError, LookupResultExt, Pagination,
|
||||
};
|
||||
|
||||
#[async_trait]
|
||||
@ -60,10 +60,7 @@ pub trait UpstreamOAuthLinkRepository: Send + Sync {
|
||||
async fn list_paginated(
|
||||
&mut self,
|
||||
user: &User,
|
||||
before: Option<Ulid>,
|
||||
after: Option<Ulid>,
|
||||
first: Option<usize>,
|
||||
last: Option<usize>,
|
||||
pagination: &Pagination,
|
||||
) -> Result<Page<UpstreamOAuthLink>, Self::Error>;
|
||||
}
|
||||
|
||||
@ -275,10 +272,7 @@ impl<'c> UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'c> {
|
||||
async fn list_paginated(
|
||||
&mut self,
|
||||
user: &User,
|
||||
before: Option<Ulid>,
|
||||
after: Option<Ulid>,
|
||||
first: Option<usize>,
|
||||
last: Option<usize>,
|
||||
pagination: &Pagination,
|
||||
) -> Result<Page<UpstreamOAuthLink>, Self::Error> {
|
||||
let mut query = QueryBuilder::new(
|
||||
r#"
|
||||
@ -295,7 +289,7 @@ impl<'c> UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'c> {
|
||||
query
|
||||
.push(" WHERE user_id = ")
|
||||
.push_bind(Uuid::from(user.id))
|
||||
.generate_pagination("upstream_oauth_link_id", before, after, first, last)?;
|
||||
.generate_pagination("upstream_oauth_link_id", pagination);
|
||||
|
||||
let edges: Vec<LinkLookup> = query
|
||||
.build_query_as()
|
||||
@ -303,7 +297,7 @@ impl<'c> UpstreamOAuthLinkRepository for PgUpstreamOAuthLinkRepository<'c> {
|
||||
.fetch_all(&mut *self.conn)
|
||||
.await?;
|
||||
|
||||
let page = Page::process(edges, first, last)?.map(UpstreamOAuthLink::from);
|
||||
let page = pagination.process(edges).map(UpstreamOAuthLink::from);
|
||||
Ok(page)
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ mod tests {
|
||||
use sqlx::PgPool;
|
||||
|
||||
use super::*;
|
||||
use crate::{user::UserRepository, Clock, PgRepository, Repository};
|
||||
use crate::{user::UserRepository, Clock, Pagination, PgRepository, Repository};
|
||||
|
||||
#[sqlx::test(migrator = "crate::MIGRATOR")]
|
||||
async fn test_repository(pool: PgPool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
@ -144,7 +144,7 @@ mod tests {
|
||||
|
||||
let links = repo
|
||||
.upstream_oauth_link()
|
||||
.list_paginated(&user, None, None, Some(10), None)
|
||||
.list_paginated(&user, &Pagination::first(10))
|
||||
.await?;
|
||||
assert!(!links.has_previous_page);
|
||||
assert!(!links.has_next_page);
|
||||
|
@ -25,7 +25,7 @@ use uuid::Uuid;
|
||||
use crate::{
|
||||
pagination::{Page, QueryBuilderExt},
|
||||
tracing::ExecuteExt,
|
||||
Clock, DatabaseError, DatabaseInconsistencyError, LookupResultExt,
|
||||
Clock, DatabaseError, DatabaseInconsistencyError, LookupResultExt, Pagination,
|
||||
};
|
||||
|
||||
#[async_trait]
|
||||
@ -52,10 +52,7 @@ pub trait UpstreamOAuthProviderRepository: Send + Sync {
|
||||
/// Get a paginated list of upstream OAuth providers
|
||||
async fn list_paginated(
|
||||
&mut self,
|
||||
before: Option<Ulid>,
|
||||
after: Option<Ulid>,
|
||||
first: Option<usize>,
|
||||
last: Option<usize>,
|
||||
pagination: &Pagination,
|
||||
) -> Result<Page<UpstreamOAuthProvider>, Self::Error>;
|
||||
|
||||
/// Get all upstream OAuth providers
|
||||
@ -243,10 +240,7 @@ impl<'c> UpstreamOAuthProviderRepository for PgUpstreamOAuthProviderRepository<'
|
||||
)]
|
||||
async fn list_paginated(
|
||||
&mut self,
|
||||
before: Option<Ulid>,
|
||||
after: Option<Ulid>,
|
||||
first: Option<usize>,
|
||||
last: Option<usize>,
|
||||
pagination: &Pagination,
|
||||
) -> Result<Page<UpstreamOAuthProvider>, Self::Error> {
|
||||
let mut query = QueryBuilder::new(
|
||||
r#"
|
||||
@ -264,7 +258,7 @@ impl<'c> UpstreamOAuthProviderRepository for PgUpstreamOAuthProviderRepository<'
|
||||
"#,
|
||||
);
|
||||
|
||||
query.generate_pagination("upstream_oauth_provider_id", before, after, first, last)?;
|
||||
query.generate_pagination("upstream_oauth_provider_id", pagination);
|
||||
|
||||
let edges: Vec<ProviderLookup> = query
|
||||
.build_query_as()
|
||||
@ -272,7 +266,7 @@ impl<'c> UpstreamOAuthProviderRepository for PgUpstreamOAuthProviderRepository<'
|
||||
.fetch_all(&mut *self.conn)
|
||||
.await?;
|
||||
|
||||
let page = Page::process(edges, first, last)?.try_map(TryInto::try_into)?;
|
||||
let page = pagination.process(edges).try_map(TryInto::try_into)?;
|
||||
Ok(page)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user