Skip to content

Commit

Permalink
refactor: Notification から NotificationResponse に変換する関数を定義
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Nov 23, 2024
1 parent 09e3481 commit 2f5332e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
38 changes: 7 additions & 31 deletions server/presentation/src/notification_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use axum::{extract::State, http::StatusCode, response::IntoResponse, Extension, Json};
use domain::{
notification::models::NotificationSource, repository::Repositories, user::models::User,
};
use domain::{repository::Repositories, user::models::User};
use itertools::Itertools;
use resource::repository::RealInfrastructureRepository;
use serde_json::json;
Expand Down Expand Up @@ -30,20 +28,9 @@ pub async fn fetch_by_request_user(
notifications
.into_iter()
.map(|notification| {
notification.try_read(&user).map(|notification| {
let (source_type, source_id) = match notification.source() {
NotificationSource::Message(message_id) => {
("MESSAGE".to_string(), message_id.to_string())
}
};

NotificationResponse {
id: notification.id().to_owned(),
source_type,
source_id,
is_read: notification.is_read().to_owned(),
}
})
notification
.try_read(&user)
.map(NotificationResponse::from_notification_ref)
})
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
Expand Down Expand Up @@ -78,20 +65,9 @@ pub async fn update_read_state(
updated_notifications
.into_iter()
.map(|notification| {
notification.try_read(&user).map(|notification| {
let (source_type, source_id) = match notification.source() {
NotificationSource::Message(message_id) => {
("MESSAGE".to_string(), message_id.to_string())
}
};

NotificationResponse {
id: notification.id().to_owned(),
source_type,
source_id,
is_read: notification.is_read().to_owned(),
}
})
notification
.try_read(&user)
.map(NotificationResponse::from_notification_ref)
})
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use domain::notification::models::NotificationId;
use domain::notification::models::{Notification, NotificationId, NotificationSource};
use serde::Serialize;

#[derive(Serialize, Debug)]
Expand All @@ -8,3 +8,20 @@ pub struct NotificationResponse {
pub source_id: String,
pub is_read: bool,
}

impl NotificationResponse {
pub fn from_notification_ref(notification: &Notification) -> Self {
let (source_type, source_id) = match notification.source() {
NotificationSource::Message(message_id) => {
("MESSAGE".to_string(), message_id.to_string())
}
};

Self {
id: notification.id().to_owned(),
source_type,
source_id,
is_read: notification.is_read().to_owned(),
}
}
}

0 comments on commit 2f5332e

Please sign in to comment.