Skip to content

Commit

Permalink
feat(api): update project rename api
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxiaohei committed Jun 7, 2024
1 parent a8fc067 commit 35c373d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
11 changes: 11 additions & 0 deletions crates/core-service/src/vars/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ pub use envs::EnvVar;

pub mod admin;

#[derive(Debug, Default, Serialize)]
pub struct OkRespVar {
pub ok: bool,
}

impl OkRespVar {
pub fn new() -> Self {
Self { ok: true }
}
}

/// PageVars is the common variables for all pages
#[derive(Debug, Default, Serialize)]
pub struct PageVars {
Expand Down
1 change: 1 addition & 0 deletions crates/dao/src/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ pub async fn update_name(id: i32, name: String, desc: String) -> Result<()> {
.col_expr(project::Column::Name, Expr::value(name.clone()))
.col_expr(project::Column::ProdDomain, Expr::value(name))
.col_expr(project::Column::Description, Expr::value(desc))
.col_expr(project::Column::UpdatedAt, Expr::value(now_time()))
.filter(project::Column::Id.eq(id))
.exec(db)
.await?;
Expand Down
5 changes: 4 additions & 1 deletion land-apiserver/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ pub fn router() -> Result<Router> {
Ok(Router::new()
.route("/v1/token", post(tokens::create))
.route("/v1/projects", get(projects::list))
.route("/v1/projects/:project_name", get(projects::single))
.route(
"/v1/projects/:project_name",
get(projects::single).post(projects::update_names),
)
.route_layer(middleware::from_fn(clerk::middleware))
.layer(cors))
}
30 changes: 29 additions & 1 deletion land-apiserver/src/v1/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::{Extension, Json};
use land_core_service::httputil::ServerJsonError;
use land_core_service::vars::{PaginationVar, ProjectVar};
use land_core_service::vars::{OkRespVar, PaginationVar, ProjectVar};
use land_service::clerk::AuthUser;
use tracing::debug;

Expand Down Expand Up @@ -65,3 +65,31 @@ pub async fn single(
let project_var = ProjectVar::new(&p, py.as_ref()).await?;
Ok(Json(project_var))
}

#[derive(Debug, serde::Deserialize)]
pub struct UpdateNamesForm {
pub name: String,
pub description: String,
}

/// update_names updates project names
pub async fn update_names(
Extension(user): Extension<AuthUser>,
Path(project_name): Path<String>,
Json(f): Json<UpdateNamesForm>,
) -> Result<impl IntoResponse, ServerJsonError> {
debug!(
"update project names: {:?}, old_name: {:?}",
f, project_name
);
let p = land_dao::projects::get_by_name(project_name, Some(user.id)).await?;
if p.is_none() {
return Err(ServerJsonError(
StatusCode::NOT_FOUND,
anyhow::anyhow!("Project not found"),
));
}
let p = p.unwrap();
land_dao::projects::update_name(p.id, f.name, f.description).await?;
Ok(Json(OkRespVar::new()))
}

0 comments on commit 35c373d

Please sign in to comment.