testlib/dto/
answer_request.rs

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
use chrono::NaiveDateTime;
use common::utils::serialize_option_naive_date;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::jsonb::answer::Answer;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct AnswerRequest {
    pub id: Option<Uuid>,
    pub text: Option<String>,
    pub file_path: Option<String>,
    pub answer_order: Option<i32>,
    pub attempt: Option<Uuid>,
    pub question: Option<Uuid>,
    pub selected: Option<Vec<Uuid>>,
    #[serde(default)]
    #[serde(with = "serialize_option_naive_date")]
    pub end_time: Option<NaiveDateTime>,
    #[serde(default)]
    #[serde(with = "serialize_option_naive_date")]
    pub duration: Option<NaiveDateTime>,
    pub skipped: Option<i32>,
}

impl AnswerRequest {
    pub fn from_answer(a: &Answer) -> AnswerRequest {
        AnswerRequest {
            id: a.id,
            text: a.text.clone(),
            file_path: a.file_path.clone(),
            answer_order: a.answer_order,
            attempt: a.attempt,
            question: a.question,
            selected: a.selected.clone(),
            end_time: a.end_time,
            duration: a.duration,
            skipped: a.skipped,
        }
    }

    pub fn vec_equal(vec1: &Vec<AnswerRequest>, vec2: &Vec<AnswerRequest>) -> bool {
        let mut eq = true;
        for i in 0..vec1.len() {
            eq &= vec1.get(i).unwrap().equal(vec2.get(i).unwrap());
        }
        eq
    }

    pub fn equal(&self, other: &AnswerRequest) -> bool {
        let mut eq = true;
        eq &= self.id == other.id;
        eq &= self.text == other.text;
        eq &= self.file_path == other.file_path;
        eq &= self.answer_order == other.answer_order;
        eq &= self.attempt == other.attempt;
        eq &= self.question == other.question;
        eq &= self.selected == other.selected;
        eq &= self.end_time == other.end_time;
        eq &= self.duration == other.duration;
        eq &= self.skipped == other.skipped;
        eq
    }

    pub fn to_answer(&self) -> Answer {
        Answer {
            id: Option::from(if self.id.is_none() {
                Uuid::new_v4()
            } else {
                self.id.unwrap()
            }),
            text: self.text.clone(),
            file_path: self.file_path.clone(),
            answer_order: self.answer_order,
            attempt: self.attempt,
            question: self.question,
            selected: self.selected.clone(),
            end_time: self.end_time,
            duration: self.duration,
            skipped: self.skipped,
        }
    }
}