Skip to content

Commit

Permalink
refactor/nanocld: cleaner namespace services with file separation
Browse files Browse the repository at this point in the history
  • Loading branch information
leon3s committed Oct 24, 2024
1 parent 66be35e commit fc19da8
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 207 deletions.
207 changes: 0 additions & 207 deletions bin/nanocld/src/services/namespace.rs

This file was deleted.

32 changes: 32 additions & 0 deletions bin/nanocld/src/services/namespace/count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use ntex::web;

use nanocl_error::http::HttpResult;
use nanocl_stubs::generic::{GenericCount, GenericListQuery};

use crate::{
models::{NamespaceDb, SystemState},
repositories::generic::*,
utils,
};

/// Count namespaces
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "Namespaces",
path = "/namespaces/count",
params(
("filter" = Option<String>, Query, description = "Generic filter", example = "{ \"filter\": { \"where\": { \"name\": { \"eq\": \"global\" } } } }"),
),
responses(
(status = 200, description = "Count result", body = GenericCount),
),
))]
#[web::get("/namespaces/count")]
pub async fn count_namespace(
state: web::types::State<SystemState>,
qs: web::types::Query<GenericListQuery>,
) -> HttpResult<web::HttpResponse> {
let filter = utils::query_string::parse_qs_filter(&qs)?;
let count = NamespaceDb::count_by(&filter, &state.inner.pool).await?;
Ok(web::HttpResponse::Ok().json(&GenericCount { count }))
}

Check warning on line 32 in bin/nanocld/src/services/namespace/count.rs

View check run for this annotation

Codecov / codecov/patch

bin/nanocld/src/services/namespace/count.rs#L25-L32

Added lines #L25 - L32 were not covered by tests
29 changes: 29 additions & 0 deletions bin/nanocld/src/services/namespace/create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use ntex::web;

use nanocl_error::http::HttpResult;
use nanocl_stubs::namespace::NamespacePartial;

use crate::{
models::{NamespaceDb, SystemState},
objects::generic::*,
};

/// Create a new namespace
#[cfg_attr(feature = "dev", utoipa::path(
post,
request_body = NamespacePartial,
tag = "Namespaces",
path = "/namespaces",
responses(
(status = 200, description = "The created namespace", body = Namespace),
(status = 409, description = "Namespace already exist", body = ApiError),
),
))]
#[web::post("/namespaces")]
pub async fn create_namespace(
state: web::types::State<SystemState>,
payload: web::types::Json<NamespacePartial>,
) -> HttpResult<web::HttpResponse> {
let item = NamespaceDb::create_obj(&payload, &state).await?;
Ok(web::HttpResponse::Created().json(&item))
}
30 changes: 30 additions & 0 deletions bin/nanocld/src/services/namespace/delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ntex::web;

use nanocl_error::http::HttpResult;

use crate::{
models::{NamespaceDb, SystemState},
objects::generic::*,
};

/// Delete a namespace
#[cfg_attr(feature = "dev", utoipa::path(
delete,
tag = "Namespaces",
path = "/namespaces/{name}",
params(
("name" = String, Path, description = "Name of the namespace to delete")
),
responses(
(status = 202, description = "Namespace have been deleted"),
(status = 404, description = "Namespace is not existing", body = ApiError),
),
))]
#[web::delete("/namespaces/{name}")]
pub async fn delete_namespace(
state: web::types::State<SystemState>,
path: web::types::Path<(String, String)>,
) -> HttpResult<web::HttpResponse> {
NamespaceDb::del_obj_by_pk(&path.1, &(), &state).await?;
Ok(web::HttpResponse::Accepted().into())
}
30 changes: 30 additions & 0 deletions bin/nanocld/src/services/namespace/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use ntex::web;

use nanocl_error::http::HttpResult;

use crate::{
models::{NamespaceDb, SystemState},
objects::generic::*,
};

/// Get detailed information about a namespace
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "Namespaces",
path = "/namespaces/{name}/inspect",
params(
("name" = String, Path, description = "The namespace name to inspect")
),
responses(
(status = 200, description = "Detailed information about a namespace", body = [NamespaceInspect]),
(status = 404, description = "Namespace is not existing", body = ApiError),
),
))]
#[web::get("/namespaces/{name}/inspect")]
pub async fn inspect_namespace(
state: web::types::State<SystemState>,
path: web::types::Path<(String, String)>,
) -> HttpResult<web::HttpResponse> {
let namespace = NamespaceDb::inspect_obj_by_pk(&path.1, &state).await?;
Ok(web::HttpResponse::Ok().json(&namespace))
}
31 changes: 31 additions & 0 deletions bin/nanocld/src/services/namespace/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use ntex::web;

use nanocl_error::http::HttpResult;
use nanocl_stubs::generic::GenericListQuery;

use crate::{
models::{NamespaceDb, SystemState},
utils,
};

/// List namespaces with optional filter
#[cfg_attr(feature = "dev", utoipa::path(
get,
tag = "Namespaces",
path = "/namespaces",
params(
("filter" = Option<String>, Query, description = "Generic filter", example = "{ \"filter\": { \"where\": { \"name\": { \"eq\": \"test\" } } } }"),
),
responses(
(status = 200, description = "List of namespace", body = [NamespaceSummary]),
),
))]
#[web::get("/namespaces")]
pub async fn list_namespace(
state: web::types::State<SystemState>,
qs: web::types::Query<GenericListQuery>,
) -> HttpResult<web::HttpResponse> {
let filter = utils::query_string::parse_qs_filter(&qs)?;
let items = NamespaceDb::list(&filter, &state).await?;
Ok(web::HttpResponse::Ok().json(&items))
}
Loading

0 comments on commit fc19da8

Please sign in to comment.