Skip to content

Commit

Permalink
refactor: Returns a PlaybookSpec instead of a PlaybookResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
wangeguo committed Jan 18, 2024
1 parent ce30d62 commit 097fbb7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ use tracing::error;
pub enum ApiError {
#[error("Internal Server Error")]
InternalServerError,

#[error("Not Found")]
NotFound,

#[error("Not Found Playbook: {0}")]
NotFoundPlaybook(HTTPError),

#[error("Failed to create playbook: {0}")]
FailedToCreatePlaybook(HTTPError),
}

impl IntoResponse for ApiError {
Expand All @@ -37,7 +42,9 @@ impl IntoResponse for ApiError {
Self::InternalServerError => (StatusCode::INTERNAL_SERVER_ERROR, self.to_string()),
Self::NotFound => (StatusCode::NOT_FOUND, self.to_string()),
Self::NotFoundPlaybook(e) => (StatusCode::NOT_FOUND, e.to_string()),
Self::FailedToCreatePlaybook(e) => (StatusCode::BAD_REQUEST, e.to_string()),
};

error!("{} - {}", status, message);
(status, Json(json!({ "message": message }))).into_response()
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::services::playbook::PlaybookService;
content_type = "application/json"
),
responses(
(status = 201, description = "Playbook created successfully", body = PlaybookResponse)
(status = 201, description = "Playbook created successfully", body = PlaybookSpec)
),
tag = "Playbooks"
)]
Expand Down
15 changes: 0 additions & 15 deletions src/responses/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,9 @@

use amp_common::scm::content::Content;
use amp_common::scm::git::Tree;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Serialize, Deserialize, ToSchema)]
pub struct PlaybookResponse {
/// The playbook ID in Amphitheatre.
pub id: String,
/// The title of the playbook.
pub title: String,
/// The description of the playbook.
pub description: String,
/// When the playbook was created in Amphitheatre.
pub created_at: DateTime<Utc>,
/// When the playbook was last updated in Amphitheatre.
pub updated_at: DateTime<Utc>,
}

#[derive(Serialize, Deserialize, ToSchema)]
pub struct FilesResponse {
/// The file details.
Expand Down
4 changes: 2 additions & 2 deletions src/services/playbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ impl PlaybookService {
}

pub async fn create(ctx: Arc<Context>, req: &CreatePlaybookRequest) -> Result<PlaybookSpec> {
let playbook_payload = PlaybookPayload {
let payload = PlaybookPayload {
title: req.title.clone(),
description: req.description.clone().unwrap_or_default(),
preface: req.preface.clone(),
};
ctx.client.playbooks().create(playbook_payload).map_err(ApiError::NotFoundPlaybook)
ctx.client.playbooks().create(payload).map_err(ApiError::FailedToCreatePlaybook)
}

pub async fn update(ctx: Arc<Context>, id: Uuid, req: Synchronization) -> Result<u16> {
Expand Down
3 changes: 1 addition & 2 deletions src/swagger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use utoipa::OpenApi;
use utoipa_swagger_ui::SwaggerUi;

use crate::{handlers, requests, responses};
use crate::{handlers, requests};

#[derive(OpenApi)]
#[openapi(
Expand All @@ -31,7 +31,6 @@ use crate::{handlers, requests, responses};
schemas(
requests::playbook::CreatePlaybookRequest,
requests::playbook::UpdatePlaybookRequest,
responses::playbook::PlaybookResponse,
amp_common::resource::ActorSpec,
amp_common::resource::CharacterSpec,
Expand Down

0 comments on commit 097fbb7

Please sign in to comment.