Skip to content

Commit

Permalink
remove fo file and folder reference filed
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahao6635 committed Jan 21, 2024
1 parent d9ca75a commit 4e37bf8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 67 deletions.
37 changes: 13 additions & 24 deletions src/handlers/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ use crate::services::FileService;

/// Returns a file's content.
#[utoipa::path(
get, path = "/v1/playbooks/{id}/files/{reference}/{path}",
get, path = "/v1/playbooks/{id}/files/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag."),
("path" = String, description = "The file path relative to the root of the repository."),
),
responses(
Expand All @@ -45,17 +44,16 @@ use crate::services::FileService;
)]
pub async fn get(
State(ctx): State<Arc<Context>>,
Path((id, reference, path)): Path<(Uuid, String, String)>,
Path((id, path)): Path<(Uuid, String)>,
) -> Result<impl IntoResponse> {
Ok(Json(FileService::get(ctx, id, reference, path).await?))
Ok(Json(FileService::get(ctx, id, path).await?))
}

/// Create a file
#[utoipa::path(
post, path = "/v1/playbooks/{id}/files/{reference}/{path}",
post, path = "/v1/playbooks/{id}/files/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag."),
("path" = String, description = "The file path relative to the root of the repository."),
),
request_body(
Expand All @@ -74,20 +72,18 @@ pub async fn create(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<FileRequest>,
) -> Result<impl IntoResponse> {
Ok((StatusCode::CREATED, Json(FileService::create(ctx, id, reference, path, req.content).await?)))
Ok((StatusCode::CREATED, Json(FileService::create(ctx, id, path, req.content).await?)))
}

/// Update a file
#[utoipa::path(
put, path = "/v1/playbooks/{id}/files/{reference}/{path}",
put, path = "/v1/playbooks/{id}/files/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."),
("path" = String, description = "The file path relative to the root of the repository."),
),
request_body(
Expand All @@ -107,20 +103,18 @@ pub async fn update(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<FileRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(FileService::update(ctx, id, reference, path, req.content).await?))
Ok(Json(FileService::update(ctx, id, path, req.content).await?))
}

/// Delete a file
#[utoipa::path(
delete, path = "/v1/playbooks/{id}/files/{reference}/{path}",
delete, path = "/v1/playbooks/{id}/files/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag."),
("path" = String, description = "The file path relative to the root of the repository."),
),
responses(
Expand All @@ -135,20 +129,18 @@ pub async fn delete(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
) -> Result<impl IntoResponse> {
FileService::delete(ctx, id, reference, path).await?;
FileService::delete(ctx, id, path).await?;

Ok(StatusCode::NO_CONTENT)
}

/// Copy a file
#[utoipa::path(
post, path = "/v1/playbooks/{id}/files/{reference}/{path}/actions/copy",
post, path = "/v1/playbooks/{id}/files/{path}/actions/copy",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."),
("path" = String, description = "The file path relative to the root of the repository."),
),
request_body(
Expand All @@ -168,20 +160,18 @@ pub async fn copy(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(FileService::copy(ctx, id, reference, path, req.destination).await?))
Ok(Json(FileService::copy(ctx, id, path, req.destination).await?))
}

/// Move a file
#[utoipa::path(
post, path = "/v1/playbooks/{id}/files/{reference}/{path}/actions/move",
post, path = "/v1/playbooks/{id}/files/{path}/actions/move",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."),
("path" = String, description = "The file path relative to the root of the repository."),
),
request_body(
Expand All @@ -201,10 +191,9 @@ pub async fn rename(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(FileService::rename(ctx, id, reference, path, req.destination).await?))
Ok(Json(FileService::rename(ctx, id, path, req.destination).await?))
}
31 changes: 11 additions & 20 deletions src/handlers/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ use crate::services::FolderService;

/// Gets the file list of a directory in a repository.
#[utoipa::path(
get, path = "/v1/playbooks/{id}/folders/{reference}/{path}",
get, path = "/v1/playbooks/{id}/folders/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag."),
("path" = String, description = "The file path relative to the root of the repository."),
),
responses(
Expand All @@ -46,9 +45,9 @@ use crate::services::FolderService;
)]
pub async fn get(
State(ctx): State<Arc<Context>>,
Path((id, reference, path)): Path<(Uuid, String, Option<String>)>,
Path((id, path)): Path<(Uuid, Option<String>)>,
) -> Result<impl IntoResponse> {
Ok(Json(FolderService::get(ctx, id, reference, path).await?))
Ok(Json(FolderService::get(ctx, id, path).await?))
}

/// Returns a folder's tree.
Expand Down Expand Up @@ -76,10 +75,9 @@ pub async fn tree(

/// Create a folder
#[utoipa::path(
post, path = "/v1/playbooks/{id}/folders/{reference}/{path}",
post, path = "/v1/playbooks/{id}/folders/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag."),
("path" = String, description = "The file path relative to the root of the repository."),
),
responses(
Expand All @@ -93,18 +91,16 @@ pub async fn create(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
) -> Result<impl IntoResponse> {
Ok((StatusCode::CREATED, Json(FolderService::create(ctx, id, reference, path).await?)))
Ok((StatusCode::CREATED, Json(FolderService::create(ctx, id, path).await?)))
}

/// Delete a folder
#[utoipa::path(
delete, path = "/v1/playbooks/{id}/folders/{reference}/{path}",
delete, path = "/v1/playbooks/{id}/folders/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag."),
("path" = String, description = "The file path relative to the root of the repository."),
),
responses(
Expand All @@ -119,20 +115,18 @@ pub async fn delete(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,
) -> Result<impl IntoResponse> {
FolderService::delete(ctx, id, reference, path).await?;
FolderService::delete(ctx, id, path).await?;

Ok(StatusCode::NO_CONTENT)
}

/// Copy a folder
#[utoipa::path(
post, path = "/v1/playbooks/{id}/folders/{reference}/{path}/actions/copy",
post, path = "/v1/playbooks/{id}/folders/{path}/actions/copy",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."),
("path" = String, description = "The file path relative to the root of the repository."),
),
request_body(
Expand All @@ -152,20 +146,18 @@ pub async fn copy(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(FolderService::copy(ctx, id, reference, path, req.destination).await?))
Ok(Json(FolderService::copy(ctx, id, path, req.destination).await?))
}

/// Move a folder
#[utoipa::path(
post, path = "/v1/playbooks/{id}/folders/{reference}/{path}/actions/move",
post, path = "/v1/playbooks/{id}/folders/{path}/actions/move",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: default branch."),
("path" = String, description = "The file path relative to the root of the repository."),
),
request_body(
Expand All @@ -185,10 +177,9 @@ pub async fn rename(
State(ctx): State<Arc<Context>>,

Path(id): Path<Uuid>,
Path(reference): Path<String>,
Path(path): Path<String>,

Json(req): Json<DestinationRequest>,
) -> Result<impl IntoResponse> {
Ok(Json(FolderService::rename(ctx, id, reference, path, req.destination).await?))
Ok(Json(FolderService::rename(ctx, id, path, req.destination).await?))
}
22 changes: 11 additions & 11 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ pub fn build() -> Router<Arc<Context>> {
.route("/v1/playbooks/:id/logs", get(logger::logs))
//
// files
.route("/v1/playbooks/:id/files/:reference/:path", get(file::get))
.route("/v1/playbooks/:id/files/:reference/:path", post(file::create))
.route("/v1/playbooks/:id/files/:reference/:path", put(file::update))
.route("/v1/playbooks/:id/files/:reference/:path", delete(file::delete))
.route("/v1/playbooks/:id/files/:reference/:path/actions/copy", post(file::copy))
.route("/v1/playbooks/:id/files/:reference/:path/actions/move", post(file::rename))
.route("/v1/playbooks/:id/files/:path", get(file::get))
.route("/v1/playbooks/:id/files/:path", post(file::create))
.route("/v1/playbooks/:id/files/:path", put(file::update))
.route("/v1/playbooks/:id/files/:path", delete(file::delete))
.route("/v1/playbooks/:id/files/:path/actions/copy", post(file::copy))
.route("/v1/playbooks/:id/files/:path/actions/move", post(file::rename))
//
// folders
.route("/v1/playbooks/:id/folders/:reference/:path", get(folder::get))
.route("/v1/playbooks/:id/folders/:path", get(folder::get))
.route("/v1/playbooks/:id/tree", get(folder::tree))
.route("/v1/playbooks/:id/folders/:reference/:path", post(folder::create))
.route("/v1/playbooks/:id/folders/:reference/:path", delete(folder::delete))
.route("/v1/playbooks/:id/folders/:reference/:path/actions/copy", post(folder::copy))
.route("/v1/playbooks/:id/folders/:reference/:path/actions/move", post(folder::rename))
.route("/v1/playbooks/:id/folders/:path", post(folder::create))
.route("/v1/playbooks/:id/folders/:path", delete(folder::delete))
.route("/v1/playbooks/:id/folders/:path/actions/copy", post(folder::copy))
.route("/v1/playbooks/:id/folders/:path/actions/move", post(folder::rename))
}
10 changes: 3 additions & 7 deletions src/services/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ pub struct FileService;

impl FileService {
/// Get a file content from the remote git repository.
pub async fn get(ctx: Arc<Context>, id: Uuid, reference: String, path: String) -> Result<Content> {
pub async fn get(ctx: Arc<Context>, id: Uuid, path: String) -> Result<Content> {
let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?;
let source = playbook.preface.repository.unwrap();

ctx.github_client
.contents()
.find(&utils::repo(&source.repo)?, &path, &reference)
.find(&utils::repo(&source.repo)?, &path, &source.branch.unwrap_or_default())
.map_err(|e| ApiError::NotFoundContent(e.to_string()))
}

/// Create a file to the workspace.
pub async fn create(
ctx: Arc<Context>,
id: Uuid,
_reference: String,
path: String,
content: String,
) -> Result<Content> {
Expand All @@ -64,7 +63,6 @@ impl FileService {
pub async fn update(
_ctx: Arc<Context>,
_id: Uuid,
_reference: String,
_path: String,
_content: String,
) -> Result<Content> {
Expand All @@ -73,7 +71,7 @@ impl FileService {
}

/// Delete a file from the workspace.
pub async fn delete(_ctx: Arc<Context>, _id: Uuid, _reference: String, _path: String) -> Result<()> {
pub async fn delete(_ctx: Arc<Context>, _id: Uuid, _path: String) -> Result<()> {
// refer to create() method.
todo!()
}
Expand All @@ -82,7 +80,6 @@ impl FileService {
pub async fn copy(
_ctx: Arc<Context>,
_id: Uuid,
_reference: String,
_path: String,
_destination: String,
) -> Result<Content> {
Expand All @@ -94,7 +91,6 @@ impl FileService {
pub async fn rename(
_ctx: Arc<Context>,
_id: Uuid,
_reference: String,
_path: String,
_destination: String,
) -> Result<Content> {
Expand Down
7 changes: 2 additions & 5 deletions src/services/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ impl FolderService {
pub async fn get(
ctx: Arc<Context>,
id: Uuid,
_reference: String,
path: Option<String>,
) -> Result<Vec<File>, ApiError> {
let playbook = ctx.client.playbooks().get(&id.to_string()).map_err(ApiError::NotFoundPlaybook)?;
Expand All @@ -55,18 +54,17 @@ impl FolderService {
.ok_or(ApiError::NotFoundFolder("The folder is none".to_string()))
}

pub async fn create(_ctx: Arc<Context>, _id: Uuid, _reference: String, _path: String) -> Result<Content> {
pub async fn create(_ctx: Arc<Context>, _id: Uuid, _path: String) -> Result<Content> {
todo!()
}

pub async fn delete(_ctx: Arc<Context>, _id: Uuid, _reference: String, _path: String) -> Result<()> {
pub async fn delete(_ctx: Arc<Context>, _id: Uuid, _path: String) -> Result<()> {
todo!()
}

pub async fn copy(
_ctx: Arc<Context>,
_id: Uuid,
_reference: String,
_path: String,
_destination: String,
) -> Result<Content> {
Expand All @@ -76,7 +74,6 @@ impl FolderService {
pub async fn rename(
_ctx: Arc<Context>,
_id: Uuid,
_reference: String,
_path: String,
_destination: String,
) -> Result<Content> {
Expand Down

0 comments on commit 4e37bf8

Please sign in to comment.