use serde::{Deserialize, Serialize}; use uuid::Uuid; use crate::error::AppError; use crate::models::channels::ChannelRepoLink; use crate::service::ImService; use super::session::ImSession; #[derive(Deserialize, Serialize, Clone, Debug, utoipa::ToSchema)] pub struct CreateRepoLinkParams { pub repo_id: Uuid, pub link_type: String, pub notify_events: Vec, } impl ImService { pub async fn repo_link_list( &self, _ctx: &ImSession, channel_id: Uuid, ) -> Result, AppError> { sqlx::query_as::<_, ChannelRepoLink>( "SELECT id, channel_id, repo_id, link_type, notify_events, active, \ created_by, created_at, updated_at \ FROM channel_repo_link WHERE channel_id = $1 ORDER BY created_at", ) .bind(channel_id) .fetch_all(self.ctx.db.reader()) .await .map_err(AppError::Database) } pub async fn repo_link_create( &self, ctx: &ImSession, channel_id: Uuid, params: CreateRepoLinkParams, ) -> Result { let now = chrono::Utc::now(); sqlx::query_as::<_, ChannelRepoLink>( "INSERT INTO channel_repo_link \ (id, channel_id, repo_id, link_type, notify_events, active, \ created_by, created_at, updated_at) \ VALUES ($1, $2, $3, $4::link_type, $5, true, $6, $7, $7) \ RETURNING id, channel_id, repo_id, link_type, notify_events, active, \ created_by, created_at, updated_at", ) .bind(Uuid::now_v7()) .bind(channel_id) .bind(params.repo_id) .bind(¶ms.link_type) .bind(¶ms.notify_events) .bind(ctx.user) .bind(now) .fetch_one(self.ctx.db.writer()) .await .map_err(AppError::Database) } pub async fn repo_link_delete( &self, _ctx: &ImSession, link_id: Uuid, ) -> Result<(), AppError> { sqlx::query("DELETE FROM channel_repo_link WHERE id = $1") .bind(link_id) .execute(self.ctx.db.writer()) .await .map_err(AppError::Database)?; Ok(()) } }