feat: 为数据库模块添加异步获取项目名方法及对应路由处理函数
- 为数据库模块添加获取所有项目名称的异步方法并实现初步查询逻辑。 - 新增获取所有项目名的路由及其处理函数以支持项目信息查询功能。
This commit is contained in:
18
src/db.rs
18
src/db.rs
@@ -4,7 +4,6 @@ use std::{fs, path::Path};
|
||||
use welds::{connections::sqlite::SqliteClient, prelude::*};
|
||||
|
||||
use crate::InsertArgs;
|
||||
|
||||
#[derive(WeldsModel, Clone, Serialize)]
|
||||
#[welds(table = "authorize")]
|
||||
pub struct Authorize {
|
||||
@@ -186,4 +185,21 @@ impl Db {
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_all_project(
|
||||
&self,
|
||||
) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let result = Authorize::select(|a| a.project)
|
||||
.group_by(|a| a.project)
|
||||
.run(&self.client)
|
||||
.await?;
|
||||
|
||||
let rows: Vec<Authorize> = result.collect_into()?;
|
||||
let projects: Vec<String> = Vec::new();
|
||||
for row in rows {
|
||||
println!("{row:?}");
|
||||
}
|
||||
|
||||
Ok(projects)
|
||||
}
|
||||
}
|
||||
|
||||
22
src/main.rs
22
src/main.rs
@@ -35,6 +35,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.route("/info", get(get_token_info))
|
||||
.route("/reset", get(update_token_status))
|
||||
.route("/renewal", get(renewal_token))
|
||||
.route("/project", get(get_projects))
|
||||
.with_state(state);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3009").await.unwrap();
|
||||
@@ -214,6 +215,21 @@ async fn renewal_token(
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取所有的项目名
|
||||
async fn get_projects(
|
||||
State(state): State<AppState>,
|
||||
) -> (StatusCode, Json<ProjectResponse>) {
|
||||
let projects = state.db.get_all_project().await.unwrap();
|
||||
|
||||
(
|
||||
StatusCode::OK,
|
||||
Json(ProjectResponse {
|
||||
code: 404,
|
||||
projects: projects,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_current_datetime() -> String {
|
||||
Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
}
|
||||
@@ -312,3 +328,9 @@ struct UpdateTokenStatus {
|
||||
token: String,
|
||||
enable: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct ProjectResponse {
|
||||
code: i32,
|
||||
projects: Vec<String>,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user