feat(service): expand service layer with new domain operations
- Add IM service modules: audit, channel roles, custom emojis, forum tags, integrations, invitations, repo links, slash commands, stages, voice, webhooks - Add PR service modules: review requests, templates - Add repo service modules: contributors, release assets, git extras (archive, branch rename, commit extras, diff/merge, tag, tree) - Add user service: social (follow/block) - Add internal auth service - Update existing service modules with expanded functionality - Remove deleted IM modules: articles, delivery trace, drafts, follows, messages, polls, presence, reactions, threads
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Role;
|
||||
use crate::models::prs::PrTemplate;
|
||||
use crate::service::PrService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, merge_optional_text, required_text};
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreatePrTemplateParams {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub title_template: Option<String>,
|
||||
pub body_template: String,
|
||||
pub labels: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdatePrTemplateParams {
|
||||
pub name: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub title_template: Option<String>,
|
||||
pub body_template: Option<String>,
|
||||
pub labels: Option<Vec<String>>,
|
||||
pub active: Option<bool>,
|
||||
}
|
||||
|
||||
impl PrService {
|
||||
pub async fn pr_templates(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<PrTemplate>, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_readable(user_uid, &repo).await?;
|
||||
let (limit, offset) = clamp_limit_offset(limit, offset);
|
||||
sqlx::query_as::<_, PrTemplate>(
|
||||
"SELECT * FROM pr_template WHERE repo_id = $1 AND active = true ORDER BY name ASC LIMIT $2 OFFSET $3",
|
||||
)
|
||||
.bind(repo.id)
|
||||
.bind(limit)
|
||||
.bind(offset)
|
||||
.fetch_all(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_create_template(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
params: CreatePrTemplateParams,
|
||||
) -> Result<PrTemplate, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Member)
|
||||
.await?;
|
||||
let name = required_text(params.name, "name")?;
|
||||
sqlx::query_as::<_, PrTemplate>(
|
||||
"INSERT INTO pr_template (id, repo_id, name, description, title_template, body_template, labels, created_by, created_at, updated_at) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), NOW()) RETURNING *",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(repo.id)
|
||||
.bind(&name)
|
||||
.bind(params.description.as_deref())
|
||||
.bind(params.title_template.as_deref())
|
||||
.bind(¶ms.body_template)
|
||||
.bind(¶ms.labels)
|
||||
.bind(user_uid)
|
||||
.fetch_one(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_update_template(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
template_id: Uuid,
|
||||
params: UpdatePrTemplateParams,
|
||||
) -> Result<PrTemplate, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Admin)
|
||||
.await?;
|
||||
let current: PrTemplate =
|
||||
sqlx::query_as("SELECT * FROM pr_template WHERE id = $1 AND repo_id = $2")
|
||||
.bind(template_id)
|
||||
.bind(repo.id)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("template not found".into()))?;
|
||||
|
||||
let name =
|
||||
merge_optional_text(params.name, Some(current.name.clone())).unwrap_or(current.name);
|
||||
let description = merge_optional_text(params.description, current.description);
|
||||
let title_template = merge_optional_text(params.title_template, current.title_template);
|
||||
let body_template = merge_optional_text(params.body_template, Some(current.body_template))
|
||||
.unwrap_or_default();
|
||||
let labels = params.labels.unwrap_or(current.labels);
|
||||
let active = params.active.unwrap_or(current.active);
|
||||
|
||||
sqlx::query_as::<_, PrTemplate>(
|
||||
"UPDATE pr_template SET name = $1, description = $2, title_template = $3, body_template = $4, \
|
||||
labels = $5, active = $6, updated_at = NOW() WHERE id = $7 AND repo_id = $8 RETURNING *",
|
||||
)
|
||||
.bind(&name)
|
||||
.bind(&description)
|
||||
.bind(&title_template)
|
||||
.bind(&body_template)
|
||||
.bind(&labels)
|
||||
.bind(active)
|
||||
.bind(template_id)
|
||||
.bind(repo.id)
|
||||
.fetch_one(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn pr_delete_template(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
template_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo = self.resolve_repo(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role_at_least(user_uid, &repo, Role::Admin)
|
||||
.await?;
|
||||
sqlx::query("DELETE FROM pr_template WHERE id = $1 AND repo_id = $2")
|
||||
.bind(template_id)
|
||||
.bind(repo.id)
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user