修改发布数据唯一查询接口
All checks were successful
build / build-rust (push) Successful in 2m27s

This commit is contained in:
soul-walker 2024-09-20 15:45:10 +08:00
parent e8fa4d119c
commit 930dbda334
4 changed files with 32 additions and 36 deletions

View File

@ -68,10 +68,13 @@ impl ReleaseDataQuery {
async fn is_release_data_name_exists(
&self,
ctx: &Context<'_>,
data_type: DataType,
name: String,
) -> async_graphql::Result<bool> {
let db_accessor = ctx.data::<RtssDbAccessor>()?;
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)
}

View File

@ -34,7 +34,11 @@ pub trait ReleaseDataAccessor {
page: PageQuery,
) -> Result<PageResult<ReleaseDataModel>, DbAccessError>;
/// 检查name是否存在
async fn is_release_data_name_exist(&self, name: &str) -> Result<bool, DbAccessError>;
async fn is_release_data_name_exist(
&self,
data_type: DataType,
name: &str,
) -> Result<bool, DbAccessError>;
/// 查询发布数据
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<bool, DbAccessError> {
async fn is_release_data_name_exist(
&self,
data_type: DataType,
name: &str,
) -> Result<bool, DbAccessError> {
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);
// 修改草稿数据

View File

@ -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
);
}
}

View File

@ -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) -- 数据类型和名称唯一
);
-- 注释发布数据表