feat(project): init
This commit is contained in:
250
src/application/services/mod.rs
Normal file
250
src/application/services/mod.rs
Normal file
@@ -0,0 +1,250 @@
|
||||
use crate::domain::models::{Article, ArticleVersion, Column, Media, Tag};
|
||||
use crate::infrastructure::repositories;
|
||||
use common_telemetry::AppError;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CmsServices {
|
||||
pool: PgPool,
|
||||
}
|
||||
|
||||
impl CmsServices {
|
||||
pub fn new(pool: PgPool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
|
||||
pub async fn create_column(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
name: String,
|
||||
slug: String,
|
||||
description: Option<String>,
|
||||
parent_id: Option<Uuid>,
|
||||
sort_order: i32,
|
||||
) -> Result<Column, AppError> {
|
||||
repositories::column::create_column(
|
||||
&self.pool,
|
||||
tenant_id,
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
parent_id,
|
||||
sort_order,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list_columns(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
q: repositories::column::ListColumnsQuery,
|
||||
) -> Result<repositories::column::Paged<Column>, AppError> {
|
||||
repositories::column::list_columns(&self.pool, tenant_id, q).await
|
||||
}
|
||||
|
||||
pub async fn get_column(&self, tenant_id: Uuid, id: Uuid) -> Result<Column, AppError> {
|
||||
repositories::column::get_column(&self.pool, tenant_id, id).await
|
||||
}
|
||||
|
||||
pub async fn update_column(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
name: Option<String>,
|
||||
slug: Option<String>,
|
||||
description: Option<Option<String>>,
|
||||
parent_id: Option<Option<Uuid>>,
|
||||
sort_order: Option<i32>,
|
||||
) -> Result<Column, AppError> {
|
||||
repositories::column::update_column(
|
||||
&self.pool,
|
||||
tenant_id,
|
||||
id,
|
||||
name,
|
||||
slug,
|
||||
description,
|
||||
parent_id,
|
||||
sort_order,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn delete_column(&self, tenant_id: Uuid, id: Uuid) -> Result<(), AppError> {
|
||||
repositories::column::delete_column(&self.pool, tenant_id, id).await
|
||||
}
|
||||
|
||||
pub async fn create_tag(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
kind: String,
|
||||
name: String,
|
||||
slug: String,
|
||||
) -> Result<Tag, AppError> {
|
||||
repositories::tag::create_tag(&self.pool, tenant_id, kind, name, slug).await
|
||||
}
|
||||
|
||||
pub async fn list_tags(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
q: repositories::tag::ListTagsQuery,
|
||||
) -> Result<repositories::column::Paged<Tag>, AppError> {
|
||||
repositories::tag::list_tags(&self.pool, tenant_id, q).await
|
||||
}
|
||||
|
||||
pub async fn get_tag(&self, tenant_id: Uuid, id: Uuid) -> Result<Tag, AppError> {
|
||||
repositories::tag::get_tag(&self.pool, tenant_id, id).await
|
||||
}
|
||||
|
||||
pub async fn update_tag(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
name: Option<String>,
|
||||
slug: Option<String>,
|
||||
) -> Result<Tag, AppError> {
|
||||
repositories::tag::update_tag(&self.pool, tenant_id, id, name, slug).await
|
||||
}
|
||||
|
||||
pub async fn delete_tag(&self, tenant_id: Uuid, id: Uuid) -> Result<(), AppError> {
|
||||
repositories::tag::delete_tag(&self.pool, tenant_id, id).await
|
||||
}
|
||||
|
||||
pub async fn create_media(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
url: String,
|
||||
mime_type: Option<String>,
|
||||
size_bytes: Option<i64>,
|
||||
width: Option<i32>,
|
||||
height: Option<i32>,
|
||||
created_by: Option<Uuid>,
|
||||
) -> Result<Media, AppError> {
|
||||
repositories::media::create_media(
|
||||
&self.pool,
|
||||
tenant_id,
|
||||
url,
|
||||
mime_type,
|
||||
size_bytes,
|
||||
width,
|
||||
height,
|
||||
created_by,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list_media(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
q: repositories::media::ListMediaQuery,
|
||||
) -> Result<repositories::column::Paged<Media>, AppError> {
|
||||
repositories::media::list_media(&self.pool, tenant_id, q).await
|
||||
}
|
||||
|
||||
pub async fn get_media(&self, tenant_id: Uuid, id: Uuid) -> Result<Media, AppError> {
|
||||
repositories::media::get_media(&self.pool, tenant_id, id).await
|
||||
}
|
||||
|
||||
pub async fn delete_media(&self, tenant_id: Uuid, id: Uuid) -> Result<(), AppError> {
|
||||
repositories::media::delete_media(&self.pool, tenant_id, id).await
|
||||
}
|
||||
|
||||
pub async fn create_article(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
column_id: Option<Uuid>,
|
||||
title: String,
|
||||
slug: String,
|
||||
summary: Option<String>,
|
||||
content: String,
|
||||
tag_ids: Vec<Uuid>,
|
||||
created_by: Option<Uuid>,
|
||||
) -> Result<repositories::article::ArticleWithTags, AppError> {
|
||||
repositories::article::create_article(
|
||||
&self.pool,
|
||||
tenant_id,
|
||||
column_id,
|
||||
title,
|
||||
slug,
|
||||
summary,
|
||||
content,
|
||||
tag_ids,
|
||||
created_by,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_article(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
) -> Result<repositories::article::ArticleWithTags, AppError> {
|
||||
repositories::article::get_article(&self.pool, tenant_id, id).await
|
||||
}
|
||||
|
||||
pub async fn list_articles(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
q: repositories::article::ListArticlesQuery,
|
||||
) -> Result<repositories::column::Paged<Article>, AppError> {
|
||||
repositories::article::list_articles(&self.pool, tenant_id, q).await
|
||||
}
|
||||
|
||||
pub async fn update_article(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
column_id: Option<Option<Uuid>>,
|
||||
title: Option<String>,
|
||||
slug: Option<String>,
|
||||
summary: Option<Option<String>>,
|
||||
content: Option<String>,
|
||||
tag_ids: Option<Vec<Uuid>>,
|
||||
updated_by: Option<Uuid>,
|
||||
) -> Result<repositories::article::ArticleWithTags, AppError> {
|
||||
repositories::article::update_article(
|
||||
&self.pool,
|
||||
tenant_id,
|
||||
id,
|
||||
column_id,
|
||||
title,
|
||||
slug,
|
||||
summary,
|
||||
content,
|
||||
tag_ids,
|
||||
updated_by,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn publish_article(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
user_id: Option<Uuid>,
|
||||
) -> Result<Article, AppError> {
|
||||
repositories::article::publish_article(&self.pool, tenant_id, id, user_id).await
|
||||
}
|
||||
|
||||
pub async fn rollback_article(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
id: Uuid,
|
||||
to_version: i32,
|
||||
user_id: Option<Uuid>,
|
||||
) -> Result<Article, AppError> {
|
||||
repositories::article::rollback_article(&self.pool, tenant_id, id, to_version, user_id)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn list_versions(
|
||||
&self,
|
||||
tenant_id: Uuid,
|
||||
article_id: Uuid,
|
||||
page: u32,
|
||||
page_size: u32,
|
||||
) -> Result<repositories::column::Paged<ArticleVersion>, AppError> {
|
||||
repositories::article::list_versions(&self.pool, tenant_id, article_id, page, page_size)
|
||||
.await
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user