feat: init
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::TargetType;
|
||||
use crate::models::issues::IssueReaction;
|
||||
use crate::service::IssueService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text};
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreateIssueReactionParams {
|
||||
pub content: String,
|
||||
pub target_type: Option<String>,
|
||||
pub target_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
impl IssueService {
|
||||
pub async fn issue_reactions(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
number: i64,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<IssueReaction>, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let issue = self.resolve_issue(wk_name, number).await?;
|
||||
self.ensure_issue_readable(user_uid, &issue).await?;
|
||||
let (limit, offset) = clamp_limit_offset(limit, offset);
|
||||
|
||||
sqlx::query_as::<_, IssueReaction>(
|
||||
"SELECT id, issue_id, user_id, content, target_type, target_id, created_at \
|
||||
FROM issue_reaction WHERE issue_id = $1 ORDER BY created_at ASC LIMIT $2 OFFSET $3",
|
||||
)
|
||||
.bind(issue.id)
|
||||
.bind(limit)
|
||||
.bind(offset)
|
||||
.fetch_all(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn issue_add_reaction(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
number: i64,
|
||||
params: CreateIssueReactionParams,
|
||||
) -> Result<IssueReaction, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let issue = self.resolve_issue(wk_name, number).await?;
|
||||
self.ensure_issue_readable(user_uid, &issue).await?;
|
||||
|
||||
let content = required_text(params.content, "content")?;
|
||||
let target_type = params
|
||||
.target_type
|
||||
.as_deref()
|
||||
.and_then(|s| s.parse::<TargetType>().ok())
|
||||
.unwrap_or(TargetType::Issue);
|
||||
if target_type == TargetType::Unknown {
|
||||
return Err(AppError::BadRequest("invalid target_type".into()));
|
||||
}
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
sqlx::query_as::<_, IssueReaction>(
|
||||
"INSERT INTO issue_reaction (id, issue_id, user_id, content, target_type, target_id, created_at) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7) \
|
||||
RETURNING id, issue_id, user_id, content, target_type, target_id, created_at",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(issue.id)
|
||||
.bind(user_uid)
|
||||
.bind(&content)
|
||||
.bind(target_type)
|
||||
.bind(params.target_id)
|
||||
.bind(now)
|
||||
.fetch_one(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn issue_remove_reaction(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
number: i64,
|
||||
reaction_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let issue = self.resolve_issue(wk_name, number).await?;
|
||||
self.ensure_issue_readable(user_uid, &issue).await?;
|
||||
|
||||
let result = sqlx::query(
|
||||
"DELETE FROM issue_reaction WHERE id = $1 AND issue_id = $2 AND user_id = $3",
|
||||
)
|
||||
.bind(reaction_id)
|
||||
.bind(issue.id)
|
||||
.bind(user_uid)
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
ensure_affected(
|
||||
result.rows_affected(),
|
||||
"reaction not found or not authored by you",
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user