From 2f5332eb28dd674d9fb4965d3cd4c40c209853b0 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:43:04 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20Notification=20=E3=81=8B=E3=82=89?= =?UTF-8?q?=20NotificationResponse=20=E3=81=AB=E5=A4=89=E6=8F=9B=E3=81=99?= =?UTF-8?q?=E3=82=8B=E9=96=A2=E6=95=B0=E3=82=92=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/src/notification_handler.rs | 38 ++++--------------- .../notification_response_schemas.rs | 19 +++++++++- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/server/presentation/src/notification_handler.rs b/server/presentation/src/notification_handler.rs index 7e2404be..fc8d6b86 100644 --- a/server/presentation/src/notification_handler.rs +++ b/server/presentation/src/notification_handler.rs @@ -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; @@ -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::, _>>() .map_err(Into::into) @@ -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::, _>>() .map_err(Into::into) diff --git a/server/presentation/src/schemas/notification/notification_response_schemas.rs b/server/presentation/src/schemas/notification/notification_response_schemas.rs index 9dac444f..fd5e81fc 100644 --- a/server/presentation/src/schemas/notification/notification_response_schemas.rs +++ b/server/presentation/src/schemas/notification/notification_response_schemas.rs @@ -1,4 +1,4 @@ -use domain::notification::models::NotificationId; +use domain::notification::models::{Notification, NotificationId, NotificationSource}; use serde::Serialize; #[derive(Serialize, Debug)] @@ -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(), + } + } +}