refactor(domain): move DB string conversions out of domain enums
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use domain::models::thought::Visibility;
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
@@ -81,7 +91,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
|
||||
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.t_ap_id,
|
||||
visibility: Visibility::from_db_str(&r.visibility),
|
||||
visibility: visibility_from_str(&r.visibility),
|
||||
content_warning: r.content_warning,
|
||||
sensitive: r.sensitive,
|
||||
local: r.t_local,
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
use domain::models::thought::Visibility;
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
@@ -95,7 +105,7 @@ fn row_to_entry(r: FeedRow) -> FeedEntry {
|
||||
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.t_ap_id,
|
||||
visibility: Visibility::from_db_str(&r.visibility),
|
||||
visibility: visibility_from_str(&r.visibility),
|
||||
content_warning: r.content_warning,
|
||||
sensitive: r.sensitive,
|
||||
local: r.t_local,
|
||||
|
||||
@@ -1,5 +1,24 @@
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
fn follow_state_from_str(s: &str) -> domain::models::social::FollowState {
|
||||
use domain::models::social::FollowState;
|
||||
match s {
|
||||
"pending" => FollowState::Pending,
|
||||
"rejected" => FollowState::Rejected,
|
||||
_ => FollowState::Accepted,
|
||||
}
|
||||
}
|
||||
|
||||
fn follow_state_as_str(state: &domain::models::social::FollowState) -> &'static str {
|
||||
use domain::models::social::FollowState;
|
||||
match state {
|
||||
FollowState::Pending => "pending",
|
||||
FollowState::Accepted => "accepted",
|
||||
FollowState::Rejected => "rejected",
|
||||
}
|
||||
}
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
@@ -31,7 +50,7 @@ impl FollowRepository for PgFollowRepository {
|
||||
)
|
||||
.bind(f.follower_id.as_uuid())
|
||||
.bind(f.following_id.as_uuid())
|
||||
.bind(f.state.as_str())
|
||||
.bind(follow_state_as_str(&f.state))
|
||||
.bind(&f.ap_id)
|
||||
.bind(f.created_at)
|
||||
.execute(&self.pool)
|
||||
@@ -77,7 +96,7 @@ impl FollowRepository for PgFollowRepository {
|
||||
.map(|o| o.map(|r| Follow {
|
||||
follower_id: UserId::from_uuid(r.follower_id),
|
||||
following_id: UserId::from_uuid(r.following_id),
|
||||
state: FollowState::from_db_str(&r.state),
|
||||
state: follow_state_from_str(&r.state),
|
||||
ap_id: r.ap_id,
|
||||
created_at: r.created_at,
|
||||
}))
|
||||
@@ -92,7 +111,7 @@ impl FollowRepository for PgFollowRepository {
|
||||
sqlx::query("UPDATE follows SET state=$3 WHERE follower_id=$1 AND following_id=$2")
|
||||
.bind(follower_id.as_uuid())
|
||||
.bind(following_id.as_uuid())
|
||||
.bind(state.as_str())
|
||||
.bind(follow_state_as_str(state))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DomainError::Internal(e.to_string()))
|
||||
|
||||
@@ -1,10 +1,33 @@
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Utc};
|
||||
|
||||
fn notif_type_from_str(s: &str) -> domain::models::notification::NotificationType {
|
||||
use domain::models::notification::NotificationType;
|
||||
match s {
|
||||
"like" => NotificationType::Like,
|
||||
"boost" => NotificationType::Boost,
|
||||
"follow" => NotificationType::Follow,
|
||||
"mention" => NotificationType::Mention,
|
||||
_ => NotificationType::Reply,
|
||||
}
|
||||
}
|
||||
|
||||
fn notif_type_as_str(t: &domain::models::notification::NotificationType) -> &'static str {
|
||||
use domain::models::notification::NotificationType;
|
||||
match t {
|
||||
NotificationType::Like => "like",
|
||||
NotificationType::Boost => "boost",
|
||||
NotificationType::Follow => "follow",
|
||||
NotificationType::Mention => "mention",
|
||||
NotificationType::Reply => "reply",
|
||||
}
|
||||
}
|
||||
|
||||
use domain::{
|
||||
errors::DomainError,
|
||||
models::{
|
||||
feed::{PageParams, Paginated},
|
||||
notification::{Notification, NotificationType},
|
||||
notification::Notification,
|
||||
},
|
||||
ports::NotificationRepository,
|
||||
value_objects::{NotificationId, ThoughtId, UserId},
|
||||
@@ -26,7 +49,7 @@ impl NotificationRepository for PgNotificationRepository {
|
||||
sqlx::query(
|
||||
"INSERT INTO notifications(id,user_id,type,from_user_id,thought_id,read,created_at) VALUES($1,$2,$3,$4,$5,$6,$7)"
|
||||
)
|
||||
.bind(n.id.as_uuid()).bind(n.user_id.as_uuid()).bind(n.notification_type.as_str())
|
||||
.bind(n.id.as_uuid()).bind(n.user_id.as_uuid()).bind(notif_type_as_str(&n.notification_type))
|
||||
.bind(n.from_user_id.as_ref().map(|u| u.as_uuid()))
|
||||
.bind(n.thought_id.as_ref().map(|t| t.as_uuid()))
|
||||
.bind(n.read).bind(n.created_at)
|
||||
@@ -62,7 +85,7 @@ impl NotificationRepository for PgNotificationRepository {
|
||||
.map(|r| Notification {
|
||||
id: NotificationId::from_uuid(r.id),
|
||||
user_id: UserId::from_uuid(r.user_id),
|
||||
notification_type: NotificationType::from_db_str(&r.r#type),
|
||||
notification_type: notif_type_from_str(&r.r#type),
|
||||
from_user_id: r.from_user_id.map(UserId::from_uuid),
|
||||
thought_id: r.thought_id.map(ThoughtId::from_uuid),
|
||||
read: r.read,
|
||||
|
||||
@@ -1,10 +1,31 @@
|
||||
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, Visibility},
|
||||
thought::Thought,
|
||||
},
|
||||
ports::ThoughtRepository,
|
||||
value_objects::{Content, ThoughtId, UserId},
|
||||
@@ -45,7 +66,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_db_str(&r.visibility),
|
||||
visibility: visibility_from_str(&r.visibility),
|
||||
content_warning: r.content_warning,
|
||||
sensitive: r.sensitive,
|
||||
local: r.local,
|
||||
@@ -72,7 +93,7 @@ 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(t.visibility.as_str())
|
||||
.bind(visibility_as_str(&t.visibility))
|
||||
.bind(&t.content_warning)
|
||||
.bind(t.sensitive)
|
||||
.bind(t.local)
|
||||
|
||||
Reference in New Issue
Block a user