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
//! Database models.
//!
//! These can be (should be) converted to the structs in [`crate::qp`] for sending as a response since the struct also parses the `semester` and `exam` fields and also generates the full static files URL.
//!
//! Use the [`From`] trait implementations.

use crate::qp::Semester;

use super::qp;
use sqlx::{prelude::FromRow, types::chrono};

#[derive(FromRow, Clone)]
/// The fields of a question paper sent to the search endpoint
pub struct DBSearchQP {
    id: i32,
    filelink: String,
    from_library: bool,
    course_code: String,
    course_name: String,
    year: i32,
    semester: String,
    exam: String,
}

#[derive(FromRow, Clone)]
/// The fields of a question paper sent to the admin dashboard endpoint
pub struct DBAdminDashboardQP {
    id: i32,
    filelink: String,
    from_library: bool,
    course_code: String,
    course_name: String,
    year: i32,
    semester: String,
    exam: String,
    upload_timestamp: chrono::NaiveDateTime,
    approve_status: bool,
}

impl From<DBAdminDashboardQP> for qp::AdminDashboardQP {
    fn from(value: DBAdminDashboardQP) -> Self {
        Self {
            id: value.id,
            filelink: value.filelink,
            from_library: value.from_library,
            course_code: value.course_code,
            course_name: value.course_name,
            year: value.year,
            semester: (&value.semester).try_into().unwrap_or(Semester::Unknown),
            exam: (&value.exam).try_into().unwrap_or(qp::Exam::Unknown),
            upload_timestamp: value.upload_timestamp.to_string(),
            approve_status: value.approve_status,
        }
    }
}

impl From<DBSearchQP> for qp::SearchQP {
    fn from(value: DBSearchQP) -> Self {
        Self {
            id: value.id,
            filelink: value.filelink,
            from_library: value.from_library,
            course_code: value.course_code,
            course_name: value.course_name,
            year: value.year,
            semester: (&value.semester).try_into().unwrap_or(Semester::Unknown),
            exam: (&value.exam).try_into().unwrap_or(qp::Exam::Unknown),
        }
    }
}