Skip to main content

tdm_server_rust/service/
evaluation_service.rs

1//! 评价业务服务 (Evaluation Service)
2//!
3//! 组员互评/审稿评价的增删改查。
4
5use crate::{
6    app::AppState,
7    common::PageBean,
8    entity::evaluation::Evaluation,
9    error::{ApiResult, AppError},
10    repository::evaluation_repo::EvaluationRepository,
11    utils::page::paginate,
12};
13use chrono::{DateTime, Utc};
14
15/// 评价服务
16pub struct EvaluationService;
17
18impl EvaluationService {
19    /// 新增评价
20    #[tracing::instrument(skip_all, level = "debug")]
21    pub async fn add_evaluation(state: &AppState, eval: Evaluation) -> ApiResult<()> {
22        EvaluationRepository::new(state.db.clone())
23            .insert(&eval)
24            .await?;
25        Ok(())
26    }
27
28    /// 分页查询评价
29    #[tracing::instrument(skip_all, level = "debug")]
30    pub async fn get_evaluations(
31        state: &AppState,
32        page: i32,
33        page_size: i32,
34        member_id: Option<i32>,
35        evaluator_id: Option<i32>,
36        start_time: Option<DateTime<Utc>>,
37        end_time: Option<DateTime<Utc>>,
38    ) -> ApiResult<PageBean<Evaluation>> {
39        let repo = EvaluationRepository::new(state.db.clone());
40        let all = repo
41            .select_by_filter(member_id, evaluator_id, start_time, end_time)
42            .await?;
43        Ok(paginate(all, page, page_size))
44    }
45
46    /// 按 ID 查询评价
47    #[tracing::instrument(skip_all, level = "debug")]
48    pub async fn get_evaluation(state: &AppState, id: i64) -> ApiResult<Evaluation> {
49        EvaluationRepository::new(state.db.clone())
50            .get_by_id(id)
51            .await?
52            .ok_or_else(|| AppError::business("评价不存在喵"))
53    }
54
55    /// 删除评价
56    #[tracing::instrument(skip_all, level = "debug")]
57    pub async fn remove_by_id(state: &AppState, id: i64) -> ApiResult<()> {
58        EvaluationRepository::new(state.db.clone())
59            .delete_by_id(id)
60            .await
61    }
62
63    /// 更新评价
64    #[tracing::instrument(skip_all, level = "debug")]
65    pub async fn update_evaluation(state: &AppState, eval: Evaluation) -> ApiResult<()> {
66        EvaluationRepository::new(state.db.clone())
67            .update(&eval)
68            .await
69    }
70}