Skip to content

Commit

Permalink
refactor: New file service handler and service
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jan 18, 2024
1 parent 51f6658 commit 2355bd1
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 80 deletions.
213 changes: 213 additions & 0 deletions src/handlers/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
// 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, FileRequest};
use crate::services::FileService;

// The Files Service Handlers.

/// Returns a file's content.
#[utoipa::path(
get, path = "/v1/playbooks/{id}/files/{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 file content", body = Content),
(status = 404, description = "Playbook not found"),
(status = 404, description = "File not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Files"
)]
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(FileService::get(ctx, id, reference, path).await?))
}

/// Create a file
#[utoipa::path(
post, path = "/v1/playbooks/{id}/files/{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."),
),
request_body(
content = inline(FileRequest),
description = "Create file request",
content_type = "application/json"
),
responses(
(status = 201, description = "The file created successfully", body = Content),
(status = 404, description = "Playbook not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Files"
)]
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?)))
}

/// Update a file
#[utoipa::path(
put, path = "/v1/playbooks/{id}/files/{reference}/{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(
content = inline(FileRequest),
description = "Update file request",
content_type = "application/json"
),
responses(
(status = 200, description = "The file updated successfully", body = Content),
(status = 404, description = "Playbook not found"),
(status = 404, description = "File not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Files"
)]
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?))
}

/// Delete a file
#[utoipa::path(
delete, path = "/v1/playbooks/{id}/files/{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 file deleted successfully"),
(status = 404, description = "Playbook not found"),
(status = 404, description = "File not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Files"
)]
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?;

Ok(StatusCode::NO_CONTENT)
}

/// Copy a file
#[utoipa::path(
post, path = "/v1/playbooks/{id}/files/{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 file copied successfully", body = Content),
(status = 404, description = "Playbook not found"),
(status = 404, description = "File not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Files"
)]
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?))
}

/// Move a file
#[utoipa::path(
post, path = "/v1/playbooks/{id}/files/{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 file moved successfully", body = Content),
(status = 404, description = "Playbook not found"),
(status = 404, description = "File not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Files"
)]
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?))
}
2 changes: 1 addition & 1 deletion src/handlers/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use uuid::Uuid;

use crate::context::Context;
use crate::errors::Result;
use crate::services::logger::LoggerService;
use crate::services::LoggerService;

// The Logging Service Handlers.

Expand Down
1 change: 1 addition & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod file;
pub mod logger;
pub mod playbook;
57 changes: 5 additions & 52 deletions src/handlers/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use amp_common::sync::Synchronization;
use std::sync::Arc;

use axum::extract::{Path, State};
Expand All @@ -23,8 +22,8 @@ use uuid::Uuid;

use crate::context::Context;
use crate::errors::Result;
use crate::requests::playbook::{CreatePlaybookRequest, GetPlaybookRequest};
use crate::services::playbook::PlaybookService;
use crate::requests::playbook::CreatePlaybookRequest;
use crate::services::PlaybookService;

// The Playbooks Service Handlers.

Expand All @@ -48,53 +47,6 @@ pub async fn create(
Ok((StatusCode::CREATED, Json(PlaybookService::create(ctx, &req).await?)))
}

/// Returns a playbook detail.
#[utoipa::path(
get, path = "/v1/playbooks/{id}/files/{reference}/{path}",
params(
("id" = Uuid, description = "The id of playbook"),
("reference" = String, description = "The name of the commit/branch/tag. Default: the repository’s default branch."),
("path" = String, description = "path parameter"),
),
responses(
(status = 200, description = "Playbook found successfully", body = FilesResponse),
(status = 404, description = "Playbook not found"),
(status = 500, description = "Internal Server Error"),
),
tag = "Playbooks"
)]
pub async fn detail(
Path(params): Path<GetPlaybookRequest>,
State(ctx): State<Arc<Context>>,
) -> Result<impl IntoResponse> {
Ok(Json(PlaybookService::get(ctx, params.id, params.reference, params.path, true).await?))
}
/// Update a playbook.
#[utoipa::path(
put, path = "/v1/playbooks/{id}",
params(
("id" = Uuid, description = "The id of playbook"),
),
request_body(
content = inline(Synchronization),
description = "Update playbook request",
content_type = "application/json"
),
responses(
(status = 204, description = "Playbook updated successfully", body = PlaybookResponse),
(status = 404, description = "Playbook not found")
),
tag = "Playbooks"
)]
pub async fn update(
Path(id): Path<Uuid>,
State(ctx): State<Arc<Context>>,
Json(req): Json<Synchronization>,
) -> Result<impl IntoResponse> {
PlaybookService::update(ctx, id, req).await?;
Ok(StatusCode::NO_CONTENT)
}

/// Delete a playbook
#[utoipa::path(
delete, path = "/v1/playbooks/{id}",
Expand All @@ -108,7 +60,7 @@ pub async fn update(
),
tag = "Playbooks"
)]
pub async fn delete(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
pub async fn delete(State(ctx): State<Arc<Context>>, Path(id): Path<Uuid>) -> Result<impl IntoResponse> {
PlaybookService::delete(ctx, id).await?;

Ok(StatusCode::NO_CONTENT)
Expand All @@ -127,7 +79,8 @@ pub async fn delete(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Re
),
tag = "Playbooks"
)]
pub async fn start(Path(id): Path<Uuid>, State(ctx): State<Arc<Context>>) -> Result<impl IntoResponse> {
pub async fn start(State(ctx): State<Arc<Context>>, Path(id): Path<Uuid>) -> Result<impl IntoResponse> {
PlaybookService::start(ctx, id).await?;

Ok(StatusCode::NO_CONTENT)
}
26 changes: 26 additions & 0 deletions src/requests/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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
//
// https://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 serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct FileRequest {
pub content: String,
}

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct DestinationRequest {
pub destination: String,
}
1 change: 1 addition & 0 deletions src/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod file;
pub mod playbook;
14 changes: 0 additions & 14 deletions src/requests/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,10 @@
use amp_common::resource::Preface;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use uuid::Uuid;

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct CreatePlaybookRequest {
pub title: String,
pub description: Option<String>,
pub preface: Preface,
}

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpdatePlaybookRequest {
pub title: Option<String>,
pub description: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct GetPlaybookRequest {
pub id: Uuid,
pub reference: String,
pub path: Option<String>,
}
Loading

0 comments on commit 2355bd1

Please sign in to comment.