Rust-角色模块

Rust-角色模块

接下来我们完成角色模块

👉引入申明

🍎src\main.rs

先在入口引入我们的模块

javascript 复制代码
  .service(
      web::scope("/api") // 这里加上 /api 前缀
        .configure(modules::role::routes::config)
  )

🍎模块申明

javascript 复制代码
src\modules\mod.rs

pub mod role;

🍎搭建基础模块

src\modules\role下面搭建模块

mod.rs模块

javascript 复制代码
// mod.rs
pub mod handlers;
pub mod routes;

routes.rs模块

javascript 复制代码
//routes.rs
use actix_web::web;
pub fn config(cfg: &mut web::ServiceConfig) {
  cfg.route("/system/roles", web::get().to(crate::modules::role::handlers::get_list));
  cfg.route("/system/roles", web::post().to(crate::modules::role::handlers::post_add));
  cfg.route("/system/roles/{id}", web::get().to(crate::modules::role::handlers::get_detail));
  cfg.route("/system/roles", web::put().to(crate::modules::role::handlers::put_update));
  cfg.route("/system/roles/{id}", web::delete().to(crate::modules::role::handlers::del_delete));
}

handlers.rs方法逻辑

javascript 复制代码
// handlers.rs方法逻辑

use actix_web::{HttpResponse};
use crate::common::response::ApiResponse; // 导入 ApiResponse 模型

// 通用查询
pub async fn get_list() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}

// 通用新增
pub async fn post_add() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}
// 通用详情
pub async fn get_detail() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}
// 通用更新
pub async fn put_update() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}
// 通用更新
pub async fn del_delete() -> HttpResponse {
    HttpResponse::Ok().json(ApiResponse {
        code: 200,
        msg: "接口信息",
        data:  None::<()>,
    })
}

👉功能实现

🍎查询功能

这里查询我们直接引我们之前的查询部分的参数和方法

javascript 复制代码
// 通过方法
#[allow(unused_imports)]
use crate::common::apimethods::list_api_page; // 引入公共分页查询方法


// 通用查询
pub async fn get_list(
    pool: web::Data<MySqlPool>,
    query: web::Query<QueryParams>,
    filter: Option<web::Query<HashMap<String, String>>>
) -> impl Responder {
    // 调试:打印查询参数
    if let Some(ref filter_params) = filter {
        println!("查询参数: {:?}", filter_params);
    }
    // 精确查询字段(exact query)
    let exactquery = vec![
        "status".to_string(),
    ];
    // 模糊查询字段(like query) "role_name".to_string()
    let likequery = vec![
        "role_name".to_string(),
    ];

   // 调用 list_api_page 传入查询条件
   list_api_page(pool, query, filter, "sys_role", exactquery, likequery).await
}

测试功能ok

🍎新增功能

javascript 复制代码
// 新增
pub async fn post_add(
    pool: web::Data<MySqlPool>,
    form: web::Json<AddRoleRequest>,
) -> HttpResponse {
    let mut data = std::collections::HashMap::new();
    data.insert("role_name".to_string(), form.roleName.clone());
    data.insert("role_key".to_string(), form.roleKey.clone());
    data.insert("role_sort".to_string(), form.roleSort.to_string());
    data.insert("status".to_string(), form.status.clone());
    data.insert("remark".to_string(), form.remark.clone());

     // 处理布尔值字段
     data.insert("menu_check_strictly".to_string(), if form.menuCheckStrictly { "1" } else { "0" }.to_string());
     data.insert("dept_check_strictly".to_string(), if form.deptCheckStrictly { "1" } else { "0" }.to_string());

    crate::common::apimethods::create_api(
        pool.get_ref(),
        "sys_role",
        &data,
        &[ 
            "role_name".to_string(),
            "role_key".to_string()
        ],
    ).await
}

🍎详情功能

javascript 复制代码
// 通用详情
pub async fn get_detail(
    pool: web::Data<MySqlPool>,
    path: web::Path<i32>,
) -> HttpResponse {
    crate::common::apimethods::detail_api::<Role>(pool, "sys_role", "role_id", path.into_inner()).await
}

🍎删除功能

javascript 复制代码
// 通用真删除
pub async fn del_delete(
    pool: web::Data<MySqlPool>,
    id: web::Path<i32>
) -> HttpResponse {
    crate::common::apimethods::delete_api(
        pool.get_ref(),
        "sys_role",
        "role_id",
        *id,
        false, // 软删除,isDeleted=1
    ).await
}

测试功能ok

相关推荐
计算机魔术师3 分钟前
实测GPT-6 Astra后,我发现过去十年的专业门槛正在失效
前端
摇滚侠6 分钟前
《Spring Boot 3:高级与架构设计》第 1 章 BeanDefinitionRegistry 阅读笔记 3
spring boot·笔记·后端
虎头金猫6 分钟前
Pic Smaller 本地部署:压缩图片、转换格式,再搭建自己的 Web 工具
运维·前端·开源·aigc·ai编程·ai写作
hunterandroid7 分钟前
Android ANR 排查实战:从线上告警到主线程卡点定位
android·前端
图扑软件22 分钟前
下篇・换墨|主题/多语言/移动端,一套组件全覆盖
前端·javascript·ui·性能优化·数据可视化
烂蜻蜓26 分钟前
Flask入门教程(三十一):实用函数与类API——全局工具函数速查
后端·python·flask
程序员老赵27 分钟前
Docker 部署 TeX Live:轻松搭建 LaTeX 论文排版编译平台
前端·docker·latex
计算机毕设定制辅导-无忧学长27 分钟前
《基于Spring Boot传承之光非遗陶瓷烧造产品交易平台的设计与实现》
java·vue.js·spring boot·后端·毕业设计
Zane199429 分钟前
内存都要回收,为什么JVM偏要把堆分成新生代和老年代
java·后端
Zane199430 分钟前
257 is 257 为什么是 True?小整数缓存背后还藏着一个更容易被忽略的机制
后端·python