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

相关推荐
疯狂的程序猴1 分钟前
移动端网页调试实战,网络请求延迟与超时问题全链路排查指南
后端
咕噜分发企业签名APP加固彭于晏7 分钟前
白嫖价值千元的EO
前端·javascript
前端开发爱好者9 分钟前
首个「完整级」WebSocket 调试神器来了!
前端·javascript·vue.js
_杨瀚博11 分钟前
Springboot构建包使用BOOT-INF中的class覆盖依赖包中的class
后端
前端Hardy12 分钟前
HTML&CSS&JS:高颜值登录注册页面—建议收藏
前端·javascript·css
Ice__Cai14 分钟前
Python 基础详解:数据类型(Data Types)—— 程序的“数据基石”
开发语言·后端·python·数据类型
Ali酱14 分钟前
远程这两年,我才真正感受到——工作,原来可以不必吞噬生活。
前端·面试·远程工作
金金金__19 分钟前
优化前端性能必读:浏览器渲染流程原理全揭秘
前端·浏览器
determination20 分钟前
AI Agent 概览
后端
Data_Adventure24 分钟前
Vue 3 手机外观组件库
前端·github copilot