From 930dbda334666da128562379c939c32ef511dd35 Mon Sep 17 00:00:00 2001 From: soul-walker <31162815+soul-walker@users.noreply.github.com> Date: Fri, 20 Sep 2024 15:45:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=91=E5=B8=83=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=94=AF=E4=B8=80=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/rtss_api/src/apis/release_data.rs | 5 ++- crates/rtss_db/src/db_access/release_data.rs | 35 ++++++++++++++------ crates/rtss_dto/src/lib.rs | 23 ------------- migrations/20240830095636_init.up.sql | 5 +-- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/crates/rtss_api/src/apis/release_data.rs b/crates/rtss_api/src/apis/release_data.rs index 05089eb..ee683c2 100644 --- a/crates/rtss_api/src/apis/release_data.rs +++ b/crates/rtss_api/src/apis/release_data.rs @@ -68,10 +68,13 @@ impl ReleaseDataQuery { async fn is_release_data_name_exists( &self, ctx: &Context<'_>, + data_type: DataType, name: String, ) -> async_graphql::Result { let db_accessor = ctx.data::()?; - let result = db_accessor.is_release_data_name_exist(&name).await?; + let result = db_accessor + .is_release_data_name_exist(data_type, &name) + .await?; Ok(result) } diff --git a/crates/rtss_db/src/db_access/release_data.rs b/crates/rtss_db/src/db_access/release_data.rs index d5f6e27..2a5895a 100644 --- a/crates/rtss_db/src/db_access/release_data.rs +++ b/crates/rtss_db/src/db_access/release_data.rs @@ -34,7 +34,11 @@ pub trait ReleaseDataAccessor { page: PageQuery, ) -> Result, DbAccessError>; /// 检查name是否存在 - async fn is_release_data_name_exist(&self, name: &str) -> Result; + async fn is_release_data_name_exist( + &self, + data_type: DataType, + name: &str, + ) -> Result; /// 查询发布数据 async fn query_release_data_by_id( &self, @@ -213,17 +217,20 @@ impl ReleaseDataAccessor for RtssDbAccessor { name: &str, description: &str, ) -> Result<(ReleaseDataModel, ReleaseDataVersionModel), DbAccessError> { - // 判断发布数据名称是否已存在 - if self.is_release_data_name_exist(name).await? { - return Err(DbAccessError::DataError("发布数据名称已存在".to_string())); - } - // 开启事务 - let mut tx = self.pool.begin().await?; // 查询草稿数据 let draft = self.query_draft_data_by_id(draft_id).await?; if draft.data.is_none() { return Err(DbAccessError::DataError("草稿数据为空".to_string())); } + // 判断发布数据名称是否已存在 + if self + .is_release_data_name_exist(DataType::try_from(draft.data_type).unwrap(), name) + .await? + { + return Err(DbAccessError::DataError("发布数据名称已存在".to_string())); + } + // 开启事务 + let mut tx = self.pool.begin().await?; // 创建发布数据 let rd_table = ReleaseDataColumn::Table.name(); let rd_insert_columns = format!( @@ -355,16 +362,22 @@ impl ReleaseDataAccessor for RtssDbAccessor { Ok(PageResult::new(total, data)) } - async fn is_release_data_name_exist(&self, name: &str) -> Result { + async fn is_release_data_name_exist( + &self, + data_type: DataType, + name: &str, + ) -> Result { let rd_table = ReleaseDataColumn::Table.name(); let rd_name = ReleaseDataColumn::Name.name(); + let rd_data_type = ReleaseDataColumn::DataType.name(); let rd_query_clause = format!( - "SELECT COUNT(*) FROM {rd_table} WHERE {rd_name} = $1", + "SELECT COUNT(*) FROM {rd_table} WHERE {rd_name} = $1 AND {rd_data_type} = $2", rd_table = rd_table, rd_name = rd_name ); let count: i64 = sqlx::query_scalar(&rd_query_clause) .bind(name) + .bind(data_type as i32) .fetch_one(&self.pool) .await?; Ok(count > 0) @@ -685,7 +698,9 @@ mod tests { println!("发布新版本测试成功"); // name重复检查 - let exist = accessor.is_release_data_name_exist(name).await?; + let exist = accessor + .is_release_data_name_exist(rtss_dto::common::DataType::Iscs, name) + .await?; assert_eq!(exist, true); // 修改草稿数据 diff --git a/crates/rtss_dto/src/lib.rs b/crates/rtss_dto/src/lib.rs index cf2b39c..4f41e23 100644 --- a/crates/rtss_dto/src/lib.rs +++ b/crates/rtss_dto/src/lib.rs @@ -1,26 +1,3 @@ mod pb; pub use pb::*; - -pub fn add(left: u64, right: u64) -> u64 { - left + right -} - -#[cfg(test)] -mod tests { - use prost::Message; - - use super::*; - - #[test] - fn test_encode_decode() { - let point = common::Point { x: 1.0, y: 2.0 }; - let encoded = point.encode_to_vec(); - let decoded = common::Point::decode(encoded.as_ref()).unwrap(); - assert_eq!(point, decoded); - println!( - "point: {:?}, encoded: {:?}, decoded: {:?}", - point, encoded, decoded - ); - } -} diff --git a/migrations/20240830095636_init.up.sql b/migrations/20240830095636_init.up.sql index 4478cda..b8798ad 100644 --- a/migrations/20240830095636_init.up.sql +++ b/migrations/20240830095636_init.up.sql @@ -52,14 +52,15 @@ COMMENT ON COLUMN rtss.draft_data.updated_at IS '更新时间'; CREATE TABLE rtss.release_data ( id SERIAL PRIMARY KEY, -- id 自增主键 - name VARCHAR(128) NOT NULL UNIQUE, -- 发布数据名称(数据唯一标识) + name VARCHAR(128) NOT NULL, -- 发布数据名称(数据唯一标识) data_type INT NOT NULL, -- 数据类型 options JSONB NULL, -- 数据相关的参数项或配置项 used_version_id INT NULL, -- 使用的版本数据id user_id INT NOT NULL, -- 发布/更新用户id is_published BOOLEAN NOT NULL DEFAULT TRUE, -- 是否上架 created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建时间 - updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP -- 更新时间 + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 更新时间 + UNIQUE(data_type, name) -- 数据类型和名称唯一 ); -- 注释发布数据表