fix(domain): from_db_str returns Result — unknown DB values are errors not silent defaults
This commit is contained in:
@@ -36,20 +36,21 @@ pub(crate) struct ThoughtRow {
|
||||
pub updated_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
impl From<ThoughtRow> for Thought {
|
||||
fn from(r: ThoughtRow) -> Self {
|
||||
Thought {
|
||||
impl TryFrom<ThoughtRow> for Thought {
|
||||
type Error = DomainError;
|
||||
fn try_from(r: ThoughtRow) -> Result<Self, DomainError> {
|
||||
Ok(Thought {
|
||||
id: ThoughtId::from_uuid(r.id),
|
||||
user_id: UserId::from_uuid(r.user_id),
|
||||
content: Content::new_remote(r.content),
|
||||
in_reply_to_id: r.in_reply_to_id.map(ThoughtId::from_uuid),
|
||||
visibility: Visibility::from_db_str(&r.visibility),
|
||||
visibility: Visibility::from_db_str(&r.visibility)?,
|
||||
content_warning: r.content_warning,
|
||||
sensitive: r.sensitive,
|
||||
local: r.local,
|
||||
created_at: r.created_at,
|
||||
updated_at: r.updated_at,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.into_domain()
|
||||
.map(|o| o.map(Thought::from))
|
||||
.and_then(|o| o.map(Thought::try_from).transpose())
|
||||
}
|
||||
|
||||
async fn delete(&self, id: &ThoughtId, user_id: &UserId) -> Result<(), DomainError> {
|
||||
@@ -129,7 +130,7 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.into_domain()
|
||||
.map(|rows| rows.into_iter().map(Thought::from).collect())
|
||||
.and_then(|rows| rows.into_iter().map(Thought::try_from).collect())
|
||||
}
|
||||
|
||||
async fn list_by_user(
|
||||
@@ -155,7 +156,10 @@ impl ThoughtRepository for PgThoughtRepository {
|
||||
.into_domain()?;
|
||||
|
||||
Ok(Paginated {
|
||||
items: rows.into_iter().map(Thought::from).collect(),
|
||||
items: rows
|
||||
.into_iter()
|
||||
.map(Thought::try_from)
|
||||
.collect::<Result<Vec<_>, _>>()?,
|
||||
total,
|
||||
page: page.page,
|
||||
per_page: page.per_page,
|
||||
|
||||
Reference in New Issue
Block a user