Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/v2/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ impl Api {
}

fn default_max_user_profile_page_size() -> u8 {
30
100
}
}
9 changes: 7 additions & 2 deletions src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,13 @@ pub trait Database: Sync + Send {
/// Get `UserProfile` from `username`.
async fn get_user_profile_from_username(&self, username: &str) -> Result<UserProfile, Error>;

/// Get all user profiles in a paginated form as `UserProfilesResponse`.
async fn get_user_profiles_paginated(&self, offset: u64, page_size: u8) -> Result<UserProfilesResponse, Error>;
/// Get all user profiles in a paginated and sorted form as `UserProfilesResponse` from `search`,`offset` and `page_size`.
async fn get_user_profiles_search_paginated(
&self,
search: &Option<String>,
offset: u64,
page_size: u8,
) -> Result<UserProfilesResponse, Error>;

/// Get `UserCompact` from `user_id`.
async fn get_user_compact_from_id(&self, user_id: i64) -> Result<UserCompact, Error>;
Expand Down
16 changes: 14 additions & 2 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,23 @@ impl Database for Mysql {
.map_err(|_| database::Error::UserNotFound)
}

async fn get_user_profiles_paginated(&self, offset: u64, limit: u8) -> Result<UserProfilesResponse, database::Error> {
let mut query_string = "SELECT * FROM torrust_user_profiles".to_string();
async fn get_user_profiles_search_paginated(
&self,
search: &Option<String>,
offset: u64,
limit: u8,
) -> Result<UserProfilesResponse, database::Error> {
let user_name = match search {
None => "%".to_string(),
Some(v) => format!("%{v}%"),
};

let mut query_string = "SELECT * FROM torrust_user_profiles WHERE username LIKE ?".to_string();

let count_query = format!("SELECT COUNT(*) as count FROM ({query_string}) AS count_table");

let count_result: Result<i64, database::Error> = query_as(&count_query)
.bind(user_name.clone())
.fetch_one(&self.pool)
.await
.map(|(v,)| v)
Expand All @@ -171,6 +182,7 @@ impl Database for Mysql {
query_string = format!("{query_string} LIMIT ?, ?");

let res: Vec<UserProfile> = sqlx::query_as::<_, UserProfile>(&query_string)
.bind(user_name.clone())
.bind(i64::saturating_add_unsigned(0, offset))
.bind(limit)
.fetch_all(&self.pool)
Expand Down
16 changes: 14 additions & 2 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,23 @@ impl Database for Sqlite {
.map_err(|_| database::Error::UserNotFound)
}

async fn get_user_profiles_paginated(&self, offset: u64, limit: u8) -> Result<UserProfilesResponse, database::Error> {
let mut query_string = "SELECT * FROM torrust_user_profiles".to_string();
async fn get_user_profiles_search_paginated(
&self,
search: &Option<String>,
offset: u64,
limit: u8,
) -> Result<UserProfilesResponse, database::Error> {
let user_name = match search {
None => "%".to_string(),
Some(v) => format!("%{v}%"),
};

let mut query_string = "SELECT * FROM torrust_user_profiles WHERE username LIKE ?".to_string();

let count_query = format!("SELECT COUNT(*) as count FROM ({query_string}) AS count_table");

let count_result: Result<i64, database::Error> = query_as(&count_query)
.bind(user_name.clone())
.fetch_one(&self.pool)
.await
.map(|(v,)| v)
Expand All @@ -172,6 +183,7 @@ impl Database for Sqlite {
query_string = format!("{query_string} LIMIT ?, ?");

let res: Vec<UserProfile> = sqlx::query_as::<_, UserProfile>(&query_string)
.bind(user_name.clone())
.bind(i64::saturating_add_unsigned(0, offset))
.bind(limit)
.fetch_all(&self.pool)
Expand Down
10 changes: 8 additions & 2 deletions src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ fn no_email() -> String {
pub struct ListingRequest {
pub page_size: Option<u8>,
pub page: Option<u32>,
pub search: Option<String>,
}

/// Internal specification for user profiles listings.
#[derive(Debug, Deserialize)]
pub struct ListingSpecification {
pub offset: u64,
pub page_size: u8,
pub search: Option<String>,
}

pub struct RegistrationService {
Expand Down Expand Up @@ -401,7 +403,11 @@ impl ListingService {

let offset = u64::from(page * u32::from(page_size));

ListingSpecification { offset, page_size }
ListingSpecification {
search: request.search.clone(),
offset,
page_size,
}
}
}

Expand Down Expand Up @@ -504,7 +510,7 @@ impl DbUserProfileRepository {
/// It returns an error if there is a database error.
pub async fn generate_listing(&self, specification: &ListingSpecification) -> Result<UserProfilesResponse, Error> {
self.database
.get_user_profiles_paginated(specification.offset, specification.page_size)
.get_user_profiles_search_paginated(&specification.search, specification.offset, specification.page_size)
.await
}
}
Expand Down
Loading