Refactor database error handling across repositories to use IntoDbResult for improved error management
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m30s
test / unit (pull_request) Successful in 16m10s
test / integration (pull_request) Failing after 16m44s
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m30s
test / unit (pull_request) Successful in 16m10s
test / integration (pull_request) Failing after 16m44s
- Updated PgNotificationRepository to utilize IntoDbResult for error handling in various methods. - Refactored PgRemoteActorRepository to replace manual error mapping with IntoDbResult. - Modified PgRemoteActorConnectionRepository to implement IntoDbResult for error handling. - Adjusted PgTagRepository to use IntoDbResult for consistent error management. - Introduced test_helpers module for seeding users and thoughts in tests. - Enhanced PgThoughtRepository to leverage IntoDbResult for error handling. - Updated PgTopFriendRepository to utilize IntoDbResult for error management. - Refactored PgUserRepository to implement IntoDbResult for error handling. - Added constants for pagination defaults in requests. - Introduced MAX_TOP_FRIENDS constant for top friends validation. - Refactored JWT expiration time to use a constant. - Improved rate limiter configuration with constants for better readability. - Added utility methods for FollowState and Visibility enums for string conversions. - Introduced maximum length constants for Username, Email, and Content value objects. - Cleaned up test modules by removing redundant code and utilizing a shared testing state.
This commit is contained in:
@@ -1,31 +1,12 @@
|
||||
use crate::db_error::IntoDbResult;
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
fn visibility_from_str(s: &str) -> domain::models::thought::Visibility {
|
||||
use domain::models::thought::Visibility;
|
||||
match s {
|
||||
"followers" => Visibility::Followers,
|
||||
"unlisted" => Visibility::Unlisted,
|
||||
"direct" => Visibility::Direct,
|
||||
_ => Visibility::Public,
|
||||
}
|
||||
}
|
||||
|
||||
fn visibility_as_str(v: &domain::models::thought::Visibility) -> &'static str {
|
||||
use domain::models::thought::Visibility;
|
||||
match v {
|
||||
Visibility::Public => "public",
|
||||
Visibility::Followers => "followers",
|
||||
Visibility::Unlisted => "unlisted",
|
||||
Visibility::Direct => "direct",
|
||||
}
|
||||
}
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
feed::{PageParams, Paginated},
|
||||
thought::Thought,
|
||||
thought::{Thought, Visibility},
|
||||
},
|
||||
ports::ThoughtRepository,
|
||||
value_objects::{Content, ThoughtId, UserId},
|
||||
@@ -66,7 +47,7 @@ impl From<ThoughtRow> for Thought {
|
||||
in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid),
|
||||
in_reply_to_url: r.in_reply_to_url,
|
||||
ap_id: r.ap_id,
|
||||
visibility: visibility_from_str(&r.visibility),
|
||||
visibility: Visibility::from_db_str(&r.visibility),
|
||||
content_warning: r.content_warning,
|
||||
sensitive: r.sensitive,
|
||||
local: r.local,
|
||||
@@ -93,14 +74,14 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.bind(t.in_reply_to_id.as_ref().map(|x| x.as_uuid()))
|
||||
.bind(&t.in_reply_to_url)
|
||||
.bind(&t.ap_id)
|
||||
.bind(visibility_as_str(&t.visibility))
|
||||
.bind(t.visibility.as_str())
|
||||
.bind(&t.content_warning)
|
||||
.bind(t.sensitive)
|
||||
.bind(t.local)
|
||||
.bind(t.created_at)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))
|
||||
.into_domain()
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
@@ -109,7 +90,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.bind(id.as_uuid())
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))
|
||||
.into_domain()
|
||||
.map(|o| o.map(Thought::from))
|
||||
}
|
||||
|
||||
@@ -119,7 +100,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.bind(user_id.as_uuid())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.into_domain()?;
|
||||
if r.rows_affected() == 0 {
|
||||
return Err(DomainError::NotFound);
|
||||
}
|
||||
@@ -132,7 +113,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.bind(content.as_str())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))
|
||||
.into_domain()
|
||||
.map(|_| ())
|
||||
}
|
||||
|
||||
@@ -153,7 +134,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.bind(id.as_uuid())
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))
|
||||
.into_domain()
|
||||
.map(|rows| rows.into_iter().map(Thought::from).collect())
|
||||
}
|
||||
|
||||
@@ -167,7 +148,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.bind(uid)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.into_domain()?;
|
||||
|
||||
let rows = sqlx::query_as::<_, ThoughtRow>(&format!(
|
||||
"{THOUGHT_SELECT} WHERE user_id=$1 ORDER BY created_at DESC LIMIT $2 OFFSET $3"
|
||||
@@ -177,7 +158,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.bind(page.offset())
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))?;
|
||||
.into_domain()?;
|
||||
|
||||
Ok(Paginated {
|
||||
items: rows.into_iter().map(Thought::from).collect(),
|
||||
@@ -191,28 +172,12 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::user::PgUserRepository;
|
||||
use domain::ports::UserRepository;
|
||||
use crate::test_helpers::seed_user;
|
||||
use domain::{
|
||||
models::{
|
||||
thought::{Thought, Visibility},
|
||||
user::User,
|
||||
},
|
||||
models::thought::{Thought, Visibility},
|
||||
value_objects::*,
|
||||
};
|
||||
|
||||
async fn seed_user(pool: &sqlx::PgPool, username: &str, email: &str) -> User {
|
||||
let repo = PgUserRepository::new(pool.clone());
|
||||
let u = User::new_local(
|
||||
UserId::new(),
|
||||
Username::new(username).unwrap(),
|
||||
Email::new(email).unwrap(),
|
||||
PasswordHash("h".into()),
|
||||
);
|
||||
repo.save(&u).await.unwrap();
|
||||
u
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn save_and_find_thought(pool: sqlx::PgPool) {
|
||||
let user = seed_user(&pool, "alice", "alice@ex.com").await;
|
||||
|
||||
Reference in New Issue
Block a user