algohub_server/models/
solution.rs

1use serde::{Deserialize, Serialize};
2use surrealdb::sql::Thing;
3
4#[derive(Debug, Serialize, Deserialize)]
5pub struct Solution {
6    pub id: Option<Thing>,
7
8    pub problem: Thing,
9    pub creator: Thing,
10    pub title: String,
11    pub content: String,
12
13    pub created_at: chrono::NaiveDateTime,
14    pub updated_at: chrono::NaiveDateTime,
15}
16
17#[derive(Debug, Serialize, Deserialize)]
18#[serde(crate = "rocket::serde")]
19pub struct SolutionData<'r> {
20    pub title: &'r str,
21    pub content: &'r str,
22    pub problem: &'r str,
23}
24
25#[derive(Debug, Serialize, Deserialize)]
26#[serde(crate = "rocket::serde")]
27pub struct CreateSolution<'r> {
28    pub id: &'r str,
29    pub token: &'r str,
30    pub data: SolutionData<'r>,
31}
32
33#[derive(Debug, Serialize, Deserialize)]
34pub struct UserSolution {
35    pub id: String,
36
37    pub problem: String,
38    pub title: String,
39    pub content: String,
40    pub creator: String,
41
42    pub created_at: chrono::NaiveDateTime,
43    pub updated_at: chrono::NaiveDateTime,
44}
45
46impl From<Solution> for UserSolution {
47    fn from(value: Solution) -> Self {
48        UserSolution {
49            id: value.id.unwrap().id.to_string(),
50            problem: value.problem.id.to_string(),
51            creator: value.creator.id.to_string(),
52            title: value.title,
53            content: value.content,
54
55            created_at: value.created_at,
56            updated_at: value.updated_at,
57        }
58    }
59}
60
61impl From<CreateSolution<'_>> for Solution {
62    fn from(val: CreateSolution<'_>) -> Self {
63        Solution {
64            id: None,
65            title: val.data.title.to_string(),
66            content: val.data.content.to_string(),
67            problem: ("problem", val.data.problem).into(),
68            creator: ("account", val.id).into(),
69
70            created_at: chrono::Local::now().naive_local(),
71            updated_at: chrono::Local::now().naive_local(),
72        }
73    }
74}
75
76#[derive(Debug, Serialize, Deserialize)]
77pub struct ListSolutions {
78    pub problem: String,
79}