assignmentlib/dto/
answer_request.rsuse chrono::NaiveDateTime;
use common::utils::serialize_option_naive_date;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::jsonb::match_selected::MatchSelected;
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 selected: Option<Vec<Uuid>>,
pub match_selected: Option<Vec<MatchSelected>>,
#[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>,
pub option_group : Uuid,
}
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,
selected: a.selected.clone(),
match_selected: a.match_selected.clone(),
end_time: a.end_time,
duration: a.duration,
skipped: a.skipped,
option_group: a.option_group
}
}
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.selected == other.selected;
eq &= self.end_time == other.end_time;
eq &= self.duration == other.duration;
eq &= self.skipped == other.skipped;
eq &= self.option_group == other.option_group;
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,
selected: self.selected.clone(),
match_selected: self.match_selected.clone(),
end_time: self.end_time,
duration: self.duration,
skipped: self.skipped,
option_group: self.option_group.clone(),
}
}
}