From 1bda15623ef50d77d99667161ba0b928133ffe78 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Thu, 21 Nov 2024 18:00:08 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=97=A2=E8=AA=AD=E7=8A=B6=E6=85=8B?= =?UTF-8?q?=E3=81=AE=E6=9B=B4=E6=96=B0=E3=81=AB=E5=A4=B1=E6=95=97=E3=81=99?= =?UTF-8?q?=E3=82=8B=E4=B8=8D=E5=85=B7=E5=90=88=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resource/src/database/notification.rs | 39 +++++++++++++------ .../src/m20220101_000001_create_table.rs | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/server/infra/resource/src/database/notification.rs b/server/infra/resource/src/database/notification.rs index 9f97781f..3551e65b 100644 --- a/server/infra/resource/src/database/notification.rs +++ b/server/infra/resource/src/database/notification.rs @@ -6,13 +6,14 @@ use domain::{ user::models::Role, }; use errors::infra::InfraError; +use sea_orm::Value; use types::Id; use uuid::Uuid; use crate::{ database::{ components::NotificationDatabase, - connection::{batch_insert, execute_and_values, query_all_and_values, ConnectionPool}, + connection::{execute_and_values, query_all_and_values, ConnectionPool}, }, dto::{NotificationDto, NotificationSourceInformationDto, NotificationSourceTypeDto, UserDto}, }; @@ -55,7 +56,7 @@ impl NotificationDatabase for ConnectionPool { FROM notifications INNER JOIN users ON notifications.recipient_id = users.id WHERE recipient_id = ?", - [recipient_id.into()], + [recipient_id.to_string().into()], txn, ).await?; @@ -93,14 +94,14 @@ impl NotificationDatabase for ConnectionPool { WHERE notifications.id IN (?{})", ", ?".repeat(notification_ids.len() - 1) ) - .as_str(), + .as_str(), notification_ids .into_iter() .map(Id::into_inner) .map(Into::into), txn, ) - .await?; + .await?; rs.into_iter() .map(|rs| { @@ -129,8 +130,8 @@ impl NotificationDatabase for ConnectionPool { .collect::, _>>() }) }) - .await - .map_err(Into::into) + .await + .map_err(Into::into) } async fn update_read_status( @@ -139,14 +140,28 @@ impl NotificationDatabase for ConnectionPool { ) -> Result<(), InfraError> { self.read_write_transaction(|txn| { Box::pin(async move { - batch_insert( - r"INSERT INTO notifications (id, is_read) VALUES (?, ?) ON DUPLICATE KEY UPDATE is_read = VALUES(is_read)", - notification_id_with_is_read.into_iter().flat_map(|(id, is_read)| [id.to_string().into(), is_read.into()]), - txn, - ).await?; + let placeholders = vec!["?"; notification_id_with_is_read.len()].join(", "); + + let query = format!( + r"UPDATE notifications + SET is_read = ELT(FIELD(id, {placeholders}), {placeholders}) + WHERE id IN ({placeholders})" + ); + + let (notification_ids, is_reads): (Vec, Vec) = + notification_id_with_is_read + .into_iter() + .map(|(id, is_read)| (id.into_inner().to_string().into(), is_read.into())) + .unzip(); + + let params = [notification_ids.to_owned(), is_reads, notification_ids].concat(); + + execute_and_values(&query, params, txn).await?; Ok::<_, InfraError>(()) }) - }).await.map_err(Into::into) + }) + .await + .map_err(Into::into) } } diff --git a/server/migration/src/m20220101_000001_create_table.rs b/server/migration/src/m20220101_000001_create_table.rs index c1867507..f3edeaa6 100644 --- a/server/migration/src/m20220101_000001_create_table.rs +++ b/server/migration/src/m20220101_000001_create_table.rs @@ -215,8 +215,8 @@ impl MigrationTrait for Migration { r"CREATE TABLE IF NOT EXISTS notifications( id UUID NOT NULL PRIMARY KEY, source_type ENUM('MESSAGE') NOT NULL, + source_id UUID NOT NULL, recipient_id CHAR(36) NOT NULL, - related_id UUID NOT NULL, is_read BOOL DEFAULT FALSE NOT NULL, FOREIGN KEY fk_notification_recipient_id(recipient_id) REFERENCES users(id) )",