1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::prelude::*;

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct TeacherGradingDecision {
    pub id: Uuid,
    pub user_exercise_state_id: Uuid,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deleted_at: Option<DateTime<Utc>>,
    pub score_given: f32,
    pub teacher_decision: TeacherDecisionType,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone, Copy, sqlx::Type)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
#[sqlx(type_name = "teacher_decision_type", rename_all = "kebab-case")]
pub enum TeacherDecisionType {
    FullPoints,
    ZeroPoints,
    CustomPoints,
    SuspectedPlagiarism,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[cfg_attr(feature = "ts_rs", derive(TS))]
pub struct NewTeacherGradingDecision {
    pub user_exercise_state_id: Uuid,
    pub exercise_id: Uuid,
    pub action: TeacherDecisionType,
    pub manual_points: Option<f32>,
}

pub async fn add_teacher_grading_decision(
    conn: &mut PgConnection,
    user_exercise_state_id: Uuid,
    action: TeacherDecisionType,
    score_given: f32,
    decision_maker_user_id: Option<Uuid>,
) -> ModelResult<TeacherGradingDecision> {
    let res = sqlx::query_as!(
        TeacherGradingDecision,
        r#"
INSERT INTO teacher_grading_decisions (
    user_exercise_state_id,
    teacher_decision,
    score_given,
    user_id
  )
VALUES ($1, $2, $3, $4)
RETURNING id,
  user_exercise_state_id,
  created_at,
  updated_at,
  deleted_at,
  score_given,
  teacher_decision AS "teacher_decision: _";
      "#,
        user_exercise_state_id,
        action as TeacherDecisionType,
        score_given,
        decision_maker_user_id
    )
    .fetch_one(conn)
    .await?;
    Ok(res)
}

pub async fn try_to_get_latest_grading_decision_by_user_exercise_state_id(
    conn: &mut PgConnection,
    user_exercise_state_id: Uuid,
) -> ModelResult<Option<TeacherGradingDecision>> {
    let res = sqlx::query_as!(
        TeacherGradingDecision,
        r#"
SELECT id,
  user_exercise_state_id,
  created_at,
  updated_at,
  deleted_at,
  score_given,
  teacher_decision AS "teacher_decision: _"
FROM teacher_grading_decisions
WHERE user_exercise_state_id = $1
  AND deleted_at IS NULL
ORDER BY created_at DESC
LIMIT 1
      "#,
        user_exercise_state_id,
    )
    .fetch_optional(conn)
    .await?;
    Ok(res)
}

pub async fn get_all_latest_grading_decisions_by_user_id_and_course_instance_id(
    conn: &mut PgConnection,
    user_id: Uuid,
    course_instance_id: Uuid,
) -> ModelResult<Vec<TeacherGradingDecision>> {
    let res = sqlx::query_as!(
        TeacherGradingDecision,
        r#"
SELECT DISTINCT ON (user_exercise_state_id)
  id,
  user_exercise_state_id,
  created_at,
  updated_at,
  deleted_at,
  score_given,
  teacher_decision AS "teacher_decision: _"
FROM teacher_grading_decisions
WHERE user_exercise_state_id IN (
    SELECT user_exercise_states.id
    FROM user_exercise_states
    WHERE user_exercise_states.user_id = $1
      AND user_exercise_states.course_instance_id = $2
      AND user_exercise_states.deleted_at IS NULL
  )
  AND deleted_at IS NULL
  ORDER BY user_exercise_state_id, created_at DESC
      "#,
        user_id,
        course_instance_id,
    )
    .fetch_all(conn)
    .await?;
    Ok(res)
}