From 2d9a135987caedcac0d6ce017e0c2deed0a6355f Mon Sep 17 00:00:00 2001 From: ZhenYi <434836402@qq.com> Date: Thu, 12 Dec 2024 18:08:51 +0800 Subject: [PATCH] 1212 Evening: add some api --- src/api/dto/repo.rs | 15 +++ src/api/handler/grand/mod.rs | 3 +- src/api/handler/grand/repo_access.rs | 8 +- src/api/handler/grand/repo_owner.rs | 25 ++++ src/api/handler/group/info.rs | 3 + src/api/handler/group/member.rs | 5 +- src/api/handler/group/repo.rs | 3 + src/api/handler/group/team.rs | 3 + src/api/handler/repo/branchs/branch.rs | 3 + src/api/handler/repo/branchs/conflicts.rs | 3 + src/api/handler/repo/branchs/del.rs | 3 + src/api/handler/repo/branchs/merge.rs | 3 + src/api/handler/repo/branchs/new.rs | 3 + src/api/handler/repo/branchs/protect.rs | 3 + src/api/handler/repo/branchs/rename.rs | 10 +- src/api/handler/repo/commits/history.rs | 43 +++++++ src/api/handler/repo/commits/mod.rs | 1 + src/api/handler/repo/info.rs | 3 + src/api/handler/repo/mod.rs | 7 +- src/api/handler/repo/objects/mod.rs | 2 + src/api/handler/repo/{ => objects}/object.rs | 8 +- src/api/handler/repo/objects/once.rs | 45 +++++++ src/api/handler/repo/rename.rs | 73 +++++++++++ src/api/handler/repo/topic.rs | 120 ++++++++++++++++++ src/api/handler/users/email.rs | 4 +- src/api/handler/users/keys.rs | 3 + src/api/handler/version.rs | 8 ++ src/api/routes.rs | 23 +++- src/api/scalar/mod.rs | 18 ++- src/api/service/groups/member.rs | 4 +- src/api/service/repos/branch.rs | 1 + src/api/service/repos/commit.rs | 14 ++ src/api/service/repos/info.rs | 52 ++++++++ src/api/service/repos/mod.rs | 1 + src/api/service/repos/object.rs | 17 +++ src/metadata/transaction/repos/object_tree.rs | 17 +-- 36 files changed, 525 insertions(+), 32 deletions(-) create mode 100644 src/api/handler/grand/repo_owner.rs create mode 100644 src/api/handler/repo/commits/history.rs create mode 100644 src/api/handler/repo/commits/mod.rs create mode 100644 src/api/handler/repo/objects/mod.rs rename src/api/handler/repo/{ => objects}/object.rs (81%) create mode 100644 src/api/handler/repo/objects/once.rs create mode 100644 src/api/handler/repo/rename.rs create mode 100644 src/api/handler/repo/topic.rs create mode 100644 src/api/service/repos/object.rs diff --git a/src/api/dto/repo.rs b/src/api/dto/repo.rs index a707be9..c6cf982 100644 --- a/src/api/dto/repo.rs +++ b/src/api/dto/repo.rs @@ -43,4 +43,19 @@ pub struct RepoBranchProtect{ pub struct RepoBranchMerge{ pub branch: String, pub target: String, +} + +#[derive(Deserialize,Serialize, ToSchema)] +pub struct RepoFilePath{ + pub path: String, +} + +#[derive(Deserialize,Serialize, ToSchema)] +pub struct RepoRename{ + pub name: String, +} + +#[derive(Deserialize,Serialize, ToSchema)] +pub struct RepoTopic{ + pub topic: String } \ No newline at end of file diff --git a/src/api/handler/grand/mod.rs b/src/api/handler/grand/mod.rs index 4bfd4a3..54218a9 100644 --- a/src/api/handler/grand/mod.rs +++ b/src/api/handler/grand/mod.rs @@ -1 +1,2 @@ -pub mod repo_access; \ No newline at end of file +pub mod repo_access; +pub mod repo_owner; \ No newline at end of file diff --git a/src/api/handler/grand/repo_access.rs b/src/api/handler/grand/repo_access.rs index 767b03d..3322efd 100644 --- a/src/api/handler/grand/repo_access.rs +++ b/src/api/handler/grand/repo_access.rs @@ -4,9 +4,9 @@ use crate::api::service::Service; pub async fn check_repo_access(service: &Service, uid: Uuid, repo_id: Uuid) -> anyhow::Result{ let repo = service.repo.owner(uid).await?; if repo.iter().any(|x| x.uid == repo_id){ - return Ok(true) + Ok(true) }else { - let groups = service.group.check_member(repo_id).await?; + let groups = service.group.check_member(repo_id,1).await?; let group_ids = groups .iter() .map(|x| x.uid) @@ -17,9 +17,9 @@ pub async fn check_repo_access(service: &Service, uid: Uuid, repo_id: Uuid) -> a repos.extend(repo); } if repos.iter().any(|x| x.uid == repo_id){ - return Ok(true) + Ok(true) }else{ - return Ok(false) + Ok(false) } } } \ No newline at end of file diff --git a/src/api/handler/grand/repo_owner.rs b/src/api/handler/grand/repo_owner.rs new file mode 100644 index 0000000..9af6287 --- /dev/null +++ b/src/api/handler/grand/repo_owner.rs @@ -0,0 +1,25 @@ +use uuid::Uuid; +use crate::api::service::Service; + +pub async fn check_repo_owner(service: &Service, uid: Uuid, repo_id: Uuid) -> anyhow::Result{ + let repo = service.repo.owner(uid).await?; + if repo.iter().any(|x| x.uid == repo_id){ + Ok(true) + }else { + let groups = service.group.check_member(repo_id,2).await?; + let group_ids = groups + .iter() + .map(|x| x.uid) + .collect::>(); + let mut repos = vec![]; + for group_id in group_ids { + let repo = service.repo.repo_by_group(group_id).await?; + repos.extend(repo); + } + if repos.iter().any(|x| x.uid == repo_id){ + Ok(true) + }else{ + Ok(false) + } + } +} \ No newline at end of file diff --git a/src/api/handler/group/info.rs b/src/api/handler/group/info.rs index f56ca8a..661af23 100644 --- a/src/api/handler/group/info.rs +++ b/src/api/handler/group/info.rs @@ -7,6 +7,9 @@ use crate::utils::r::R; get, tag = "group", path = "/api/v1/group/{group}/info", + params( + ("group" = Uuid, description = "group Uid"), + ), responses( (status = 200, description = "Group found successfully"), (status = 400, description = "Group Not Found"), diff --git a/src/api/handler/group/member.rs b/src/api/handler/group/member.rs index a47ab3e..8804e93 100644 --- a/src/api/handler/group/member.rs +++ b/src/api/handler/group/member.rs @@ -6,7 +6,10 @@ use crate::utils::r::R; #[utoipa::path( get, tag = "group", - path = "/api/v1/group/{group}/member", + path = "/api/v1/group/{group}/member", + params( + ("group" = Uuid, description = "group Uid"), + ), responses( (status = 200, description = "Group found successfully"), (status = 400, description = "Group Not Found"), diff --git a/src/api/handler/group/repo.rs b/src/api/handler/group/repo.rs index 5cdd01e..acea6ba 100644 --- a/src/api/handler/group/repo.rs +++ b/src/api/handler/group/repo.rs @@ -5,6 +5,9 @@ use uuid::Uuid; get, tag = "group", path = "/api/v1/group/{group}/repo", + params( + ("group" = Uuid, description = "group Uid"), + ), responses( (status = 200, description = "Group found successfully"), (status = 400, description = "Group Not Found"), diff --git a/src/api/handler/group/team.rs b/src/api/handler/group/team.rs index 9eee916..531fb55 100644 --- a/src/api/handler/group/team.rs +++ b/src/api/handler/group/team.rs @@ -6,6 +6,9 @@ use crate::utils::r::R; get, tag = "group", path = "/api/v1/group/{group}/team", + params( + ("group" = Uuid, description = "group Uid"), + ), responses( (status = 200, description = "Group found successfully"), (status = 400, description = "Group Not Found"), diff --git a/src/api/handler/repo/branchs/branch.rs b/src/api/handler/repo/branchs/branch.rs index 988ac05..7897f40 100644 --- a/src/api/handler/repo/branchs/branch.rs +++ b/src/api/handler/repo/branchs/branch.rs @@ -8,6 +8,9 @@ use crate::api::service::Service; get, tag = "repos", path = "/api/v1/repo/{repo}/branch", + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully"), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/branchs/conflicts.rs b/src/api/handler/repo/branchs/conflicts.rs index bb7caae..e97487e 100644 --- a/src/api/handler/repo/branchs/conflicts.rs +++ b/src/api/handler/repo/branchs/conflicts.rs @@ -13,6 +13,9 @@ use uuid::Uuid; tag = "repos", path = "/api/v1/repo/{repo}/branch/check_merge", request_body = RepoBranchMerge, + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully", body = Vec), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/branchs/del.rs b/src/api/handler/repo/branchs/del.rs index 187821f..65a54eb 100644 --- a/src/api/handler/repo/branchs/del.rs +++ b/src/api/handler/repo/branchs/del.rs @@ -12,6 +12,9 @@ use crate::utils::r::R; tag = "repos", path = "/api/v1/repo/{repo}/branch/del", request_body = RepoBranchDel, + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully"), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/branchs/merge.rs b/src/api/handler/repo/branchs/merge.rs index 8cb0365..68fb7e2 100644 --- a/src/api/handler/repo/branchs/merge.rs +++ b/src/api/handler/repo/branchs/merge.rs @@ -12,6 +12,9 @@ use crate::utils::r::R; tag = "repos", path = "/api/v1/repo/{repo}/branch/merge", request_body = RepoBranchMerge, + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully"), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/branchs/new.rs b/src/api/handler/repo/branchs/new.rs index a809ace..f6de4d2 100644 --- a/src/api/handler/repo/branchs/new.rs +++ b/src/api/handler/repo/branchs/new.rs @@ -12,6 +12,9 @@ use crate::utils::r::R; tag = "repos", path = "/api/v1/repo/{repo}/branch/new", request_body = RepoBranchNew, + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully"), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/branchs/protect.rs b/src/api/handler/repo/branchs/protect.rs index 2288dcd..10114af 100644 --- a/src/api/handler/repo/branchs/protect.rs +++ b/src/api/handler/repo/branchs/protect.rs @@ -13,6 +13,9 @@ use crate::utils::r::R; tag = "repos", path = "/api/v1/repo/{repo}/branch/protect", request_body = RepoBranchProtect, + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully"), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/branchs/rename.rs b/src/api/handler/repo/branchs/rename.rs index 22ae44a..8b543f5 100644 --- a/src/api/handler/repo/branchs/rename.rs +++ b/src/api/handler/repo/branchs/rename.rs @@ -1,17 +1,19 @@ -use crate::api::handler::repo::branchs::branch::__path_api_repo_branch; -use actix_session::Session; -use actix_web::{web, Responder}; -use uuid::Uuid; use crate::api::dto::repo::RepoBranchRename; use crate::api::handler::grand::repo_access::check_repo_access; use crate::api::service::Service; use crate::utils::r::R; +use actix_session::Session; +use actix_web::{web, Responder}; +use uuid::Uuid; #[utoipa::path( post, tag = "repos", path = "/api/v1/repo/{repo}/branch/rename", request_body = RepoBranchRename, + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully"), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/commits/history.rs b/src/api/handler/repo/commits/history.rs new file mode 100644 index 0000000..ef475e9 --- /dev/null +++ b/src/api/handler/repo/commits/history.rs @@ -0,0 +1,43 @@ +use crate::api::service::Service; +use crate::store::dto::CommitDto; +use crate::utils::r::R; +use actix_web::{web, Responder}; +use uuid::Uuid; + +#[utoipa::path( + get, + path = "/api/repo/{repo_id}/commit/{commit_id}/history", + tag = "repo", + params( + ("repo_id" = String, description = "repo id"), + ("commit_id" = String, description = "commit id"), + ), + responses( + (status = 200, description = "success", body = Vec), + (status = 500, description = "error", body = String) + ) +)] +pub async fn api_repo_commit_history( + path: web::Path<(Uuid,String)>, + service: web::Data +) +-> impl Responder +{ + let (repo_id, commit_id) = path.into_inner(); + match service.repo.commit_history(repo_id, commit_id){ + Ok(commits)=>{ + R::>{ + code: 200, + data: Option::from(commits), + msg: Option::from("success".to_string()) + } + }, + Err(e)=>{ + R::>{ + code: 500, + data: None, + msg: Option::from(e.to_string()) + } + } + } +} \ No newline at end of file diff --git a/src/api/handler/repo/commits/mod.rs b/src/api/handler/repo/commits/mod.rs new file mode 100644 index 0000000..aca5103 --- /dev/null +++ b/src/api/handler/repo/commits/mod.rs @@ -0,0 +1 @@ +pub mod history; \ No newline at end of file diff --git a/src/api/handler/repo/info.rs b/src/api/handler/repo/info.rs index 874eafd..2619177 100644 --- a/src/api/handler/repo/info.rs +++ b/src/api/handler/repo/info.rs @@ -8,6 +8,9 @@ use crate::utils::r::R; get, tag = "repos", path = "/api/v1/repo/{repo}/info", + params( + ("repo" = Uuid, description = "Repo Uid"), + ), responses( (status = 200, description = "Repo found successfully"), (status = 400, description = "Repo Not Found"), diff --git a/src/api/handler/repo/mod.rs b/src/api/handler/repo/mod.rs index f254038..a39c5e9 100644 --- a/src/api/handler/repo/mod.rs +++ b/src/api/handler/repo/mod.rs @@ -1,4 +1,7 @@ pub mod create; -pub mod object; pub mod info; -pub mod branchs; \ No newline at end of file +pub mod branchs; +pub mod commits; +pub mod objects; +pub mod rename; +pub mod topic; \ No newline at end of file diff --git a/src/api/handler/repo/objects/mod.rs b/src/api/handler/repo/objects/mod.rs new file mode 100644 index 0000000..ecd148d --- /dev/null +++ b/src/api/handler/repo/objects/mod.rs @@ -0,0 +1,2 @@ +pub mod object; +pub mod once; \ No newline at end of file diff --git a/src/api/handler/repo/object.rs b/src/api/handler/repo/objects/object.rs similarity index 81% rename from src/api/handler/repo/object.rs rename to src/api/handler/repo/objects/object.rs index b2b1f9a..521b077 100644 --- a/src/api/handler/repo/object.rs +++ b/src/api/handler/repo/objects/object.rs @@ -7,7 +7,11 @@ use crate::utils::r::R; #[utoipa::path( get, tag = "repos", - path = "/api/v1/repo/tree/{repo}/{branch}", + path = "/api/v1/repo/tree/{repo}/{branch}/tree", + params( + ("repo" = String, description = "repo id"), + ("branch" = String, description = "branch name"), + ), responses( (status = 200, description = "Ok", body = Vec), (status = 401, description = "Unauthorized"), @@ -16,7 +20,7 @@ use crate::utils::r::R; )] pub async fn api_repo_object_tree( service: web::Data, - rb: web::Path<(Uuid, Uuid)>, + rb: web::Path<(Uuid, String)>, ) -> impl Responder { let (repo, branch) = rb.into_inner(); diff --git a/src/api/handler/repo/objects/once.rs b/src/api/handler/repo/objects/once.rs new file mode 100644 index 0000000..d9fcc89 --- /dev/null +++ b/src/api/handler/repo/objects/once.rs @@ -0,0 +1,45 @@ +use actix_web::web; +use uuid::Uuid; +use crate::api::dto::repo::RepoFilePath; +use crate::api::service::Service; +use crate::utils::r::R; + +#[utoipa::path( + get, + tag = "repo", + path = "/api/repo/{repo_id}/object/{branch}/{ref}/once", + params( + ("repo_id" = String, description = "repo id"), + ("branch" = String, description = "branch name"), + ("hash" = String, description = "hash"), + ), + request_body = RepoFilePath, + responses( + (status = 200, description = "success", body= Vec), + (status = 500, description = "error", body = String) + ) +)] +pub async fn api_repo_object_once( + service: web::Data, + path: web::Path<(Uuid,String,String)>, + dto: web::Json +) -> impl actix_web::Responder +{ + let (repo_id, branch, hash) = path.into_inner(); + match service.repo.once_files(repo_id, branch, hash, dto.path.to_string()).await{ + Ok(byte)=>{ + R::>{ + code: 200, + data: Option::from(byte), + msg: Option::from("success".to_string()) + } + }, + Err(e)=>{ + R{ + code: 500, + data: None, + msg: Option::from(e.to_string()) + } + } + } +} diff --git a/src/api/handler/repo/rename.rs b/src/api/handler/repo/rename.rs new file mode 100644 index 0000000..ab56d47 --- /dev/null +++ b/src/api/handler/repo/rename.rs @@ -0,0 +1,73 @@ +use actix_session::Session; +use actix_web::{web, Responder}; +use uuid::Uuid; +use crate::api::dto::repo::RepoRename; +use crate::api::handler::grand::repo_owner::check_repo_owner; +use crate::api::service::Service; +use crate::utils::r::R; + +#[utoipa::path( + post, + tag = "repo", + path = "/api/repo/{repo_id}/rename", + params( + ("repo_id" = Uuid, description = "repo id"), + ), + request_body = RepoRename, + responses( + (status = 200, description = "success", body= bool), + (status = 400, description = "error", body = String) + ) +)] +pub async fn api_repo_rename( + service: web::Data, + path: web::Path, + dto: web::Json, + session: Session +) +-> impl Responder +{ + let session = service.check.check_session(session).await; + if !session.is_err(){ + return R::{ + code: 400, + data: Option::from(false), + msg: Option::from("[Error] Not Permission".to_string()) + } + } + let repo_id = path.into_inner(); + let session = session.unwrap(); + let check = check_repo_owner(&service, session.uid, repo_id).await; + if check.is_err(){ + return R::{ + code: 400, + data: Option::from(false), + msg: Option::from("[Error] Not Permission".to_string()) + } + } + if !check.unwrap(){ + return R::{ + code: 400, + data: Option::from(false), + msg: Option::from("[Error] Not Permission".to_string()) + } + } + match service.repo.rename(repo_id, dto.name.to_string()).await{ + Ok(_)=>{ + R::{ + code: 200, + data: Option::from(true), + msg: Option::from("[Ok]".to_string()) + } + }, + Err(_)=>{ + R::{ + code: 400, + data: Option::from(false), + msg: Option::from("[Error]".to_string()) + } + } + } + + +} \ No newline at end of file diff --git a/src/api/handler/repo/topic.rs b/src/api/handler/repo/topic.rs new file mode 100644 index 0000000..0c09e45 --- /dev/null +++ b/src/api/handler/repo/topic.rs @@ -0,0 +1,120 @@ +use actix_web::{web, Responder}; +use uuid::Uuid; +use crate::api::dto::repo::RepoTopic; +use crate::api::service::Service; +use crate::utils::r::R; + +#[utoipa::path( + get, + tag = "repos", + path = "/api/v1/repo/{repo}/topic", + params( + ("repo" = Uuid, description = "Repo Uid"), + ), + responses( + (status = 200, description = "Repo found successfully"), + (status = 400, description = "Repo Not Found"), + ), +)] +pub async fn api_repo_topic( + service: web::Data, + path: web::Path +) +-> impl Responder +{ + let repo_id = path.into_inner(); + match service.repo.topics(repo_id).await{ + Ok(x)=>{ + R::>{ + code: 200, + data: Option::from(x), + msg: Option::from("[Ok]".to_string()) + } + } + Err(e)=>{ + R::>{ + code: 400, + data: Option::from(vec![]), + msg: Option::from(format!("[Error]: {}",e.to_string()).to_string()) + } + } + } +} + +#[utoipa::path( + post, + tag = "repos", + path = "/api/v1/repo/{repo}/topic", + params( + ("repo" = Uuid, description = "Repo Uid"), + ), + request_body(content = RepoTopic, description = "Add Topic", content_type = "application/json"), + responses( + (status = 200, description = "Repo found successfully"), + (status = 400, description = "Repo Not Found"), + ) +)] +pub async fn api_repo_topic_add( + service: web::Data, + path: web::Path, + topic: web::Json +) +-> impl Responder +{ + let repo_id = path.into_inner(); + match service.repo.add_topic(repo_id, topic.topic.to_string()).await{ + Ok(_)=>{ + R::{ + code: 200, + data: Option::from(true), + msg: Option::from("[Ok]".to_string()) + } + } + Err(e)=>{ + R::{ + code: 400, + data: Option::from(false), + msg: Option::from(format!("[Error]: {}",e.to_string()).to_string()) + } + } + } +} + +#[utoipa::path( + delete, + tag = "repos", + path = "/api/v1/repo/{repo}/topic", + params( + ("repo" = Uuid, description = "Repo Uid"), + ), + request_body(content = RepoTopic, description = "Delete Topic", content_type = "application/json"), + responses( + (status = 200, description = "Repo found successfully"), + (status = 400, description = "Repo Not Found"), + ) +)] +pub async fn api_repo_topic_del( + service: web::Data, + path: web::Path, + topic: web::Json +) +-> impl Responder +{ + let repo_id = path.into_inner(); + match service.repo.del_topic(repo_id, topic.topic.to_string()).await{ + Ok(_)=>{ + R::{ + code: 200, + data: Option::from(true), + msg: Option::from("[Ok]".to_string()) + } + } + Err(e)=>{ + R::{ + code: 400, + data: Option::from(false), + msg: Option::from(format!("[Error]: {}",e.to_string()).to_string()) + } + } + } +} \ No newline at end of file diff --git a/src/api/handler/users/email.rs b/src/api/handler/users/email.rs index 2114d76..166e0a4 100644 --- a/src/api/handler/users/email.rs +++ b/src/api/handler/users/email.rs @@ -6,7 +6,7 @@ use crate::utils::r::R; #[utoipa::path( post, - tag = "user", + tag = "users", path = "/api/v1/user/email/bind", request_body = EmailBind, responses( @@ -51,7 +51,7 @@ pub async fn api_user_email_bind( #[utoipa::path( delete, - tag = "user", + tag = "users", path = "/api/v1/user/email/unbind", request_body = EmailBind, responses( diff --git a/src/api/handler/users/keys.rs b/src/api/handler/users/keys.rs index 310422d..fc95e82 100644 --- a/src/api/handler/users/keys.rs +++ b/src/api/handler/users/keys.rs @@ -55,6 +55,9 @@ pub async fn api_users_key_create( delete, tag = "users", path = "/api/v1/users/key/{uid}", + params( + ("uid" = Uuid, description = "key Uid"), + ), responses( (status = 200, description = "OK"), (status = 401, description = "Not Login"), diff --git a/src/api/handler/version.rs b/src/api/handler/version.rs index 3697f1b..93aba3f 100644 --- a/src/api/handler/version.rs +++ b/src/api/handler/version.rs @@ -2,6 +2,14 @@ use std::env::consts::OS; use std::time::Instant; use actix_web::{web, Responder}; +#[utoipa::path( + get, + path = "/api/version", + description = "获取服务启动时间,软件版本,运行时操作系统,进程ID", + responses( + (status = 200, description = "获取成功", body = String) + ) +)] pub async fn api_version(time: web::Data) -> impl Responder{ format!("服务启动时长: {:?}\n版本: {}\n操作系统: {}\n进程ID: {}", time.elapsed(), "0.1", OS.to_string(), std::process::id()) } \ No newline at end of file diff --git a/src/api/routes.rs b/src/api/routes.rs index 0f4bd89..f39d18d 100644 --- a/src/api/routes.rs +++ b/src/api/routes.rs @@ -32,7 +32,7 @@ use crate::api::handler::owner::star::api_owner_star; use crate::api::handler::owner::team::api_owner_team; use crate::api::handler::owner::watch::api_owner_watcher; use crate::api::handler::repo::info::api_repo_info; -use crate::api::handler::repo::object::api_repo_object_tree; +use crate::api::handler::repo::objects::object::api_repo_object_tree; use crate::api::handler::teams::byuser::api_team_by_user; use crate::api::handler::users::avatar::{api_user_avatar_delete, api_user_avatar_upload}; use crate::api::handler::users::email::{api_user_email_bind, api_user_email_unbind}; @@ -48,6 +48,10 @@ use crate::api::handler::repo::branchs::conflicts::api_repo_branch_check_merge; use crate::api::handler::repo::branchs::del::api_repo_branch_del; use crate::api::handler::repo::branchs::merge::api_repo_branch_merge; use crate::api::handler::repo::branchs::rename::api_repo_branch_rename; +use crate::api::handler::repo::commits::history::api_repo_commit_history; +use crate::api::handler::repo::objects::once::api_repo_object_once; +use crate::api::handler::repo::rename::api_repo_rename; +use crate::api::handler::repo::topic::{api_repo_topic, api_repo_topic_add, api_repo_topic_del}; pub fn routes(cfg: &mut web::ServiceConfig) { let start = std::time::Instant::now(); @@ -202,6 +206,7 @@ pub fn routes(cfg: &mut web::ServiceConfig) { .service( web::scope("/{repo}") .route("/info", get().to(api_repo_info)) + .route("/rename", post().to(api_repo_rename)) .route("/branch", get().to(api_repo_branch)) .route("/branch/new", post().to(api_repo_branch_new)) .route("/branch/delete", delete().to(api_repo_branch_del)) @@ -211,7 +216,21 @@ pub fn routes(cfg: &mut web::ServiceConfig) { .route("/branch/merge",post().to(api_repo_branch_merge)) .service( web::scope("/{branch}") - .route("/object", get().to(api_repo_object_tree)) + .route("/tree", get().to(api_repo_object_tree)) + .service( + web::scope("/commit") + .route("/history", get().to(api_repo_commit_history)) + .service( + web::scope("/{ref}") + .route("/once", get().to(api_repo_object_once)) + ) + ) + ) + .service( + web::scope("/topic") + .route("/", post().to(api_repo_topic_add)) + .route("/", delete().to(api_repo_topic_del)) + .route("/", get().to(api_repo_topic)) ) ) ) diff --git a/src/api/scalar/mod.rs b/src/api/scalar/mod.rs index 82d0ed6..d21e653 100644 --- a/src/api/scalar/mod.rs +++ b/src/api/scalar/mod.rs @@ -1,3 +1,10 @@ +use crate::api::handler::version::__path_api_version; +use crate::api::handler::repo::topic::__path_api_repo_topic; +use crate::api::handler::repo::topic::__path_api_repo_topic_del; +use crate::api::handler::repo::topic::__path_api_repo_topic_add; +use crate::api::handler::repo::rename::__path_api_repo_rename; +use crate::api::handler::repo::objects::once::__path_api_repo_object_once; +use crate::api::handler::repo::commits::history::__path_api_repo_commit_history; use crate::api::handler::repo::branchs::branch::__path_api_repo_branch; use crate::api::handler::repo::branchs::rename::__path_api_repo_branch_rename; use crate::api::handler::repo::branchs::protect::__path_api_repo_branch_protect; @@ -11,7 +18,7 @@ use crate::api::handler::group::member::__path_api_group_member; use crate::api::handler::group::repo::__path_api_group_repo_get; use crate::api::handler::users::email::__path_api_user_email_unbind; use crate::api::handler::users::email::__path_api_user_email_bind; -use crate::api::handler::repo::object::__path_api_repo_object_tree; +use crate::api::handler::repo::objects::object::__path_api_repo_object_tree; use crate::api::handler::owner::watch::__path_api_owner_watcher; use crate::api::handler::users::star::__path_api_user_star_remove; use crate::api::handler::users::star::__path_api_user_star_add; @@ -52,6 +59,8 @@ use utoipa::OpenApi; #[openapi( info(description = "GitDataAi Api description"), paths( + api_version, + api_owner_email, api_owner_follower, api_owner_group, @@ -100,6 +109,13 @@ use utoipa::OpenApi; api_repo_branch_merge, api_repo_branch_protect, api_repo_branch_rename, + api_repo_commit_history, + api_repo_object_once, + api_repo_rename, + api_repo_topic, + api_repo_topic_add, + api_repo_topic_del, + api_team_by_user, api_teams_create, diff --git a/src/api/service/groups/member.rs b/src/api/service/groups/member.rs index abe8aa7..13477e2 100644 --- a/src/api/service/groups/member.rs +++ b/src/api/service/groups/member.rs @@ -22,9 +22,10 @@ impl GroupService { } Ok(members) } - pub async fn check_member(&self, user_id: uuid::Uuid) -> anyhow::Result> { + pub async fn check_member(&self, user_id: uuid::Uuid, access: i32) -> anyhow::Result> { let teams_users = teams_user::Entity::find() .filter(teams_user::Column::UserId.eq(user_id)) + .filter(teams_user::Column::Access.gte(access)) .all(&self.db) .await? .iter() @@ -42,4 +43,5 @@ impl GroupService { .await?; Ok(groups) } + } \ No newline at end of file diff --git a/src/api/service/repos/branch.rs b/src/api/service/repos/branch.rs index 25b20eb..51cabb7 100644 --- a/src/api/service/repos/branch.rs +++ b/src/api/service/repos/branch.rs @@ -110,4 +110,5 @@ impl RepoService { repo.update(&self.db).await?; Ok(()) } + } \ No newline at end of file diff --git a/src/api/service/repos/commit.rs b/src/api/service/repos/commit.rs index e69de29..c60c495 100644 --- a/src/api/service/repos/commit.rs +++ b/src/api/service/repos/commit.rs @@ -0,0 +1,14 @@ +use uuid::Uuid; +use crate::api::service::repos::RepoService; +use crate::store::dto::CommitDto; +use crate::store::host::GitLocal; + +impl RepoService { + pub fn commit_history(&self, repo_id: Uuid, branch: String) -> anyhow::Result>{ + let store = GitLocal::init(repo_id.to_string()); + match store.commits_history(branch){ + Ok(cmxs) => Ok(cmxs), + Err(e) => Err(e) + } + } +} \ No newline at end of file diff --git a/src/api/service/repos/info.rs b/src/api/service/repos/info.rs index a351ce6..f87878f 100644 --- a/src/api/service/repos/info.rs +++ b/src/api/service/repos/info.rs @@ -1,3 +1,4 @@ +use anyhow::anyhow; use sea_orm::*; use uuid::Uuid; use crate::api::service::repos::RepoService; @@ -11,4 +12,55 @@ impl RepoService { } Ok(model.unwrap()) } + pub async fn rename(&self, uid: Uuid, name: String) -> anyhow::Result{ + let repo = self.info(uid).await?; + let mut model = repo.into_active_model(); + model.name = Set(name); + match model.update(&self.db).await { + Ok(model) => { + Ok(model) + }, + Err(e) => { + Err(anyhow!(e)) + } + } + } + pub async fn topics(&self, uid: Uuid) -> anyhow::Result>{ + let repo = self.info(uid).await?; + Ok(repo.topic) + } + pub async fn add_topic(&self, uid: Uuid, topic: String) -> anyhow::Result{ + let repo = self.info(uid).await?; + let mut topics = repo.topic.clone(); + if !topics.contains(&topic){ + topics.push(topic); + } + let mut model = repo.into_active_model(); + model.topic = Set(topics); + match model.update(&self.db).await { + Ok(model) => { + Ok(model) + }, + Err(e) => { + Err(anyhow!(e)) + } + } + } + pub async fn del_topic(&self, uid: Uuid, topic: String) -> anyhow::Result{ + let mut repo = self.info(uid).await?; + let mut topics = repo.topic.clone(); + if topics.contains(&topic){ + topics.retain(|x| x != &topic); + } + let mut model = repo.into_active_model(); + model.topic = Set(topics); + match model.update(&self.db).await { + Ok(model) => { + Ok(model) + }, + Err(e) => { + Err(anyhow!(e)) + } + } + } } \ No newline at end of file diff --git a/src/api/service/repos/mod.rs b/src/api/service/repos/mod.rs index ff0ab1e..ff83c9d 100644 --- a/src/api/service/repos/mod.rs +++ b/src/api/service/repos/mod.rs @@ -6,6 +6,7 @@ pub mod info; pub mod owner; pub mod commit; pub mod branch; +pub mod object; #[derive(Clone)] pub struct RepoService{ pub db: DatabaseConnection, diff --git a/src/api/service/repos/object.rs b/src/api/service/repos/object.rs new file mode 100644 index 0000000..41d8ad1 --- /dev/null +++ b/src/api/service/repos/object.rs @@ -0,0 +1,17 @@ +use uuid::Uuid; +use crate::api::service::repos::RepoService; +use crate::store::host::GitLocal; + +impl RepoService { + pub async fn once_files(&self, repo_id: Uuid, branch: String, hash: String ,path: String) -> anyhow::Result>{ + let store = GitLocal::init(repo_id.to_string()); + match store.object_file(branch, hash, path){ + Ok(content)=>{ + Ok(content) + } + Err(e)=>{ + Err(e) + } + } + } +} \ No newline at end of file diff --git a/src/metadata/transaction/repos/object_tree.rs b/src/metadata/transaction/repos/object_tree.rs index 38bc228..c886dae 100644 --- a/src/metadata/transaction/repos/object_tree.rs +++ b/src/metadata/transaction/repos/object_tree.rs @@ -1,28 +1,21 @@ -use sea_orm::*; -use uuid::Uuid; -use crate::metadata::model::repos::{repo, repo_branch}; +use crate::metadata::model::repos::repo; use crate::metadata::transaction::repos::RepoTransaction; use crate::store::dto::ObjectFile; use crate::store::host::GitLocal; +use sea_orm::*; +use uuid::Uuid; impl RepoTransaction { - pub async fn object_tree(&self, repo_id: Uuid, branch_id: Uuid) -> anyhow::Result>{ + pub async fn object_tree(&self, repo_id: Uuid, branch: String) -> anyhow::Result>{ let repo = repo::Entity::find_by_id(repo_id) .one(&self.db) .await?; if repo.is_none(){ return Err(anyhow::anyhow!("repo not found")) } - let branch = repo_branch::Entity::find_by_id(branch_id) - .one(&self.db) - .await?; - if branch.is_none(){ - return Err(anyhow::anyhow!("branch not found")) - } let repo = repo.unwrap(); - let branch = branch.unwrap(); let store = GitLocal::init(repo.uid.to_string()); - let tree = store.object_tree(branch.branch); + let tree = store.object_tree(branch); if tree.is_err(){ return Err(anyhow::anyhow!("tree error:{}",tree.err().unwrap())) }