Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

工具友链新增 #186

Merged
merged 11 commits into from
May 22, 2024
2 changes: 1 addition & 1 deletion libs/status-err/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { workspace = true }
axum = { workspace = true }
axum = { workspace = true, features = ["multipart"] }
http = "1"
http_02 = {package = "http",version = "0.2"}
jwt = "0.16.0"
Expand Down
6 changes: 5 additions & 1 deletion logic/ceobe_operation_logic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ thiserror.workspace = true
status-err = { path = "../../libs/status-err" }
persistence = { workspace = true, features = ["model-ceobe-operate"] }
page_size = {path = "../../libs/page_size"}
futures.workspace = true
futures.workspace = true
url.workspace = true

[dependencies.checker]
path = "../../libs/checker"
9 changes: 8 additions & 1 deletion logic/ceobe_operation_logic/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::convert::Infallible;

use persistence::ceobe_operate::tool_link::OperateError as ToolLinkIOperateError;
use persistence::ceobe_operate::{
models::tool_link::CheckError,
tool_link::OperateError as ToolLinkIOperateError,
};
use status_err::StatusErr;
use thiserror::Error;

Expand All @@ -12,6 +15,10 @@ pub enum LogicError {
#[error(transparent)]
#[status_err(err = "transparent")]
ToolLinkIOperateError(#[from] ToolLinkIOperateError),

#[error(transparent)]
#[status_err(err = "transparent")]
ToolLinkCheckError(#[from] CheckError),
}

impl From<Infallible> for LogicError {
Expand Down
31 changes: 22 additions & 9 deletions logic/ceobe_operation_logic/src/impletements/tool_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ use page_size::{
};
use persistence::{
ceobe_operate::{
models::tool_link::{
checkers::tool_link_data::CeobeOperationToolLink,
models::model_tool_link::{self, FrontendToolLink},
},
models::tool_link::checkers::tool_link_data::CeobeOperationToolLink,
ToCeobeOperation,
},
ceobe_user::ToCeobe,
mysql::SqlDatabaseOperate,
};

use super::CeobeOperateLogic;
use crate::error::LogicResult;
use crate::{error::LogicResult, view::ToolLinkResp};

impl CeobeOperateLogic {
pub async fn create_tool_link(
Expand Down Expand Up @@ -50,7 +47,7 @@ impl CeobeOperateLogic {

pub async fn find_tool_link_list_with_paginator(
sql: SqlDatabaseOperate, page_size: Paginator,
) -> LogicResult<ListWithPageInfo<model_tool_link::Model>> {
) -> LogicResult<ListWithPageInfo<ToolLinkResp>> {
// 获取数据源列表
// 获取数据源数量
// 异步获取
Expand All @@ -66,14 +63,30 @@ impl CeobeOperateLogic {
)
.await;

let resp = tool_list?.with_page_info(page_size, count?);
let tool_list = tool_list?;
let mut tool_links =
Vec::<ToolLinkResp>::with_capacity(tool_list.len());
for tool in tool_list {
tool_links.push(tool.try_into()?)
}

let resp = tool_links.with_page_info(page_size, count?);

Ok(resp)
}

pub async fn find_tool_link_list(
sql: SqlDatabaseOperate,
) -> LogicResult<Vec<FrontendToolLink>> {
Ok(sql.ceobe().operation().tool_link().find_list().await?)
) -> LogicResult<Vec<ToolLinkResp>> {
let tool_list =
sql.ceobe().operation().tool_link().find_list().await?;

let mut tool_links =
Vec::<ToolLinkResp>::with_capacity(tool_list.len());
for tool in tool_list {
tool_links.push(tool.try_into()?)
}

Ok(tool_links)
}
}
51 changes: 50 additions & 1 deletion logic/ceobe_operation_logic/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
use persistence::ceobe_operate::models::tool_link::{
self, models::model_tool_link::FrontendToolLink,
};
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

#[derive(Debug, Clone, Serialize, Deserialize, TypedBuilder)]
use crate::error::LogicError;

#[derive(Debug, Clone, Deserialize, TypedBuilder)]
pub struct DeleteOneToolLinkReq {
pub id: i32,
}

#[derive(Debug, Clone, Serialize, TypedBuilder)]
pub struct ToolLinkResp {
#[builder(default)]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<i32>,
pub nickname: String,
pub avatar: String,
pub jump_url: String,
pub slogan: String,
pub description: String,
pub tags: Vec<String>,
}

impl TryInto<ToolLinkResp> for tool_link::Model {
type Error = LogicError;

fn try_into(self) -> Result<ToolLinkResp, Self::Error> {
Ok(ToolLinkResp::builder()
.id(Some(self.id))
.avatar(self.avatar)
.nickname(self.nickname)
.jump_url(self.jump_url)
.slogan(self.slogan)
.description(self.description)
.tags(serde_json::from_str::<Vec<String>>(&self.tags)?)
.build())
}
}

impl TryInto<ToolLinkResp> for FrontendToolLink {
type Error = LogicError;

fn try_into(self) -> Result<ToolLinkResp, Self::Error> {
Ok(ToolLinkResp::builder()
.avatar(self.avatar)
.nickname(self.nickname)
.jump_url(self.jump_url)
.slogan(self.slogan)
.description(self.description)
.tags(serde_json::from_str::<Vec<String>>(&self.tags)?)
.build())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use sea_orm_migration::{
prelude::*,
sea_orm::{DeriveActiveEnum, EnumIter},
};
use sql_models::admin_user::{AuthLevel, Column::Auth};

use super::m20220722_082735_change_user_table_name::AdminUser;

pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str { "m20240519_011405_admin_alter_auth" }
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let mut al = sea_query::Table::alter();
al.table(AdminUser::Table).modify_column(
ColumnDef::new_with_type(Auth, AuthLevel::column_type())
.not_null(),
);
manager.alter_table(al).await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// 这边回滚需要删除一下authlevel中的Outsourcing
phidiaLam marked this conversation as resolved.
Show resolved Hide resolved
let mut al = sea_query::Table::alter();
al.table(AdminUser::Table).modify_column(
ColumnDef::new_with_type(Auth, OldAuthLevel::column_type())
.not_null(),
);
manager.alter_table(al).await?;

Ok(())
}
}

#[derive(EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "auth")]
pub enum OldAuthLevel {
#[sea_orm(string_value = "chef")]
Chef,
#[sea_orm(string_value = "cooker")]
Cooker,
#[sea_orm(string_value = "architect")]
Architect,
}
1 change: 1 addition & 0 deletions persistence/migrate/sql-migration/src/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod m20220429_142528_alter_user;
pub mod m20220429_230336_alter_user;
pub mod m20220722_082735_change_user_table_name;
pub mod m20221218_001732_charset_and_collate;
pub mod m20240519_011405_alter_auth;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use sea_orm_migration::prelude::*;

use super::CeobeOperationToolLink;

pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m20240519_004238_ceobe_operation_tool_link_add_infos"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let mut al = sea_query::Table::alter();
al.table(CeobeOperationToolLink::Table)
.add_column(
ColumnDef::new(CeobeOperationToolLink::Slogan)
.string_len(16)
.not_null()
.default(""),
)
.add_column(
ColumnDef::new(CeobeOperationToolLink::Description)
.string_len(128)
.not_null()
.default(""),
)
.add_column(
ColumnDef::new(CeobeOperationToolLink::Tags)
.string_len(128)
.not_null()
.default("[]"),
);
manager.alter_table(al).await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let mut al = sea_query::Table::alter();
al.table(CeobeOperationToolLink::Table)
.drop_column(CeobeOperationToolLink::Slogan)
.drop_column(CeobeOperationToolLink::Description)
.drop_column(CeobeOperationToolLink::Tags);
manager.alter_table(al).await?;

Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ enum CeobeOperationToolLink {
Nickname,
Avatar,
JumpUrl,
Slogan,
Tags,
Description,
}
pub mod m20240519_004238_add_infos;
2 changes: 2 additions & 0 deletions persistence/migrate/sql-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl MigratorTrait for Migrator {
ceobe_operation_desktop_version::m20230729_211229_create::Migration
ceobe_operation_app_version::m20230729_212539_add_download_link::Migration
ceobe_operation_tool_link::m20231018_162927_create::Migration
ceobe_operation_tool_link::m20240519_004238_add_infos::Migration
admin::m20240519_011405_alter_auth::Migration
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ pub enum AuthLevel {
#[serde(rename = "architect")]
#[sea_orm(string_value = "architect")]
Architect,
#[serde(rename = "outsourcing")]
#[sea_orm(string_value = "outsourcing")]
Outsourcing,
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod tags;
use std::convert::Infallible;

use status_err::{ErrPrefix, StatusErr};
Expand All @@ -17,6 +18,10 @@ pub enum CheckError {
#[error("Datasource Unique key[{0:?}] 未找到")]
#[status_err(err(prefix = "ErrPrefix::CHECKER", err_code = 0x0017u16))]
UniqueKeyInvalid(String),

#[error(transparent)]
#[status_err(err = "transparent")]
Json(#[from] serde_json::Error),
}

impl From<Infallible> for CheckError {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use checker::Checker;
use futures::future::{ready, Ready};

use super::CheckError;

pub struct TagSerializeChecker;

impl Checker for TagSerializeChecker {
type Args = ();
type Checked = String;
type Err = CheckError;
type Fut = Ready<Result<String, Self::Err>>;
type Unchecked = Vec<String>;

fn check(_: Self::Args, uncheck: Self::Unchecked) -> Self::Fut {
ready(serde_json::to_string(&uncheck).map_err(Into::into))
}
}
Loading
Loading