Skip to content

Commit

Permalink
feat: New folders handler and service
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jan 18, 2024
1 parent 2355bd1 commit 3c29f7a
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 1 deletion.
173 changes: 173 additions & 0 deletions src/handlers/folder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;

use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
use uuid::Uuid;

use crate::context::Context;
use crate::errors::Result;
use crate::requests::file::DestinationRequest;
use crate::services::FolderService;

// The Folders Service Handlers.

/// Returns a folder's tree.
#[utoipa::path(
get, path = "/v1/playbooks/{id}/folders/{reference}/{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(
(status = 200, description = "The folder tree", body = Tree),
(status = 404, description = "Playbook not found"),
(status = 404, description = "Folder not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Folders"
)]
pub async fn get(
State(ctx): State<Arc<Context>>,

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

/// Create a folder
#[utoipa::path(
post, path = "/v1/playbooks/{id}/folders/{reference}/{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(
(status = 201, description = "The folder created successfully", body = Tree),
(status = 404, description = "Playbook not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Folders"
)]
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?)))
}

/// Delete a folder
#[utoipa::path(
delete, path = "/v1/playbooks/{id}/folders/{reference}/{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(
(status = 204, description = "The folder deleted successfully"),
(status = 404, description = "Playbook not found"),
(status = 404, description = "Folder not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Folders"
)]
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?;

Ok(StatusCode::NO_CONTENT)
}

/// Copy a folder
#[utoipa::path(
post, path = "/v1/playbooks/{id}/folders/{reference}/{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(
content = inline(DestinationRequest),
description = "The destination request",
content_type = "application/json"
),
responses(
(status = 200, description = "The folder copied successfully", body = Content),
(status = 404, description = "Playbook not found"),
(status = 404, description = "Folder not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Folders"
)]
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?))
}

/// Move a folder
#[utoipa::path(
post, path = "/v1/playbooks/{id}/folders/{reference}/{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(
content = inline(DestinationRequest),
description = "The destination request",
content_type = "application/json"
),
responses(
(status = 200, description = "The folder moved successfully", body = Content),
(status = 404, description = "Playbook not found"),
(status = 404, description = "Folder not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Folders"
)]
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?))
}
1 change: 1 addition & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
// limitations under the License.

pub mod file;
pub mod folder;
pub mod logger;
pub mod playbook;
9 changes: 8 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use axum::routing::{delete, get, post, put};
use axum::Router;

use crate::context::Context;
use crate::handlers::{file, logger, playbook};
use crate::handlers::{file, folder, logger, playbook};

pub fn build() -> Router<Arc<Context>> {
Router::new()
Expand All @@ -37,4 +37,11 @@ pub fn build() -> Router<Arc<Context>> {
.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))
//
// folders
.route("/v1/playbooks/:id/folders/:reference/:path", get(folder::get))
.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))
}
57 changes: 57 additions & 0 deletions src/services/folder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;
use uuid::Uuid;

use amp_common::scm::content::Content;

use crate::context::Context;
use crate::errors::Result;

pub struct FolderService;

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

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

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

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

pub async fn rename(
_ctx: Arc<Context>,
_id: Uuid,
_reference: String,
_path: String,
_destination: String,
) -> Result<Content> {
todo!()
}
}
3 changes: 3 additions & 0 deletions src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
mod file;
pub use file::FileService;

mod folder;
pub use folder::FolderService;

mod logger;
pub use logger::LoggerService;

Expand Down
6 changes: 6 additions & 0 deletions src/swagger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ use crate::{handlers, requests};
handlers::file::delete,
handlers::file::copy,
handlers::file::rename,
handlers::folder::get,
handlers::folder::create,
handlers::folder::delete,
handlers::folder::copy,
handlers::folder::rename,
),
components(
schemas(
Expand Down

0 comments on commit 3c29f7a

Please sign in to comment.