algohub_server/utils/
submission.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
85
86
87
88
89
90
use crate::models::submission::Status;
use crate::models::submission::Submission;
use anyhow::Result;
use eval_stack::compile::Language;
use surrealdb::{engine::remote::ws::Client, sql::Thing, Surreal};

pub async fn create(
    db: &Surreal<Client>,
    account_id: &str,
    problem: &str,
    code: String,
    lang: Language,
) -> Result<Option<Submission>> {
    Ok(db
        .create("submission")
        .content(Submission {
            id: None,
            lang,
            code,
            problem: ("problem", problem).into(),
            status: Status::InQueue,

            creator: ("account", account_id).into(),
            judge_details: vec![],
            judge_result: None,

            contest: None,

            created_at: chrono::Local::now().naive_local(),
            updated_at: chrono::Local::now().naive_local(),
        })
        .await?)
}

pub async fn get_by_id(db: &Surreal<Client>, id: &str) -> Result<Option<Submission>> {
    Ok(db.select(("submission", id)).await?)
}

pub async fn list_by_user(db: &Surreal<Client>, creator: Thing) -> Result<Vec<Submission>> {
    Ok(db
        .query("SELECT * FROM submission WHERE creator = $creator")
        .bind(("creator", creator))
        .await?
        .take(0)?)
}

pub async fn list_by_contest(db: &Surreal<Client>, contest: Thing) -> Result<Vec<Submission>> {
    Ok(db
        .query("SELECT * FROM submission WHERE contest = $contest")
        .bind(("contest", contest))
        .await?
        .take(0)?)
}

pub async fn list_within_contest(
    db: &Surreal<Client>,
    contest: Thing,
    creator: Thing,
) -> Result<Vec<Submission>> {
    Ok(db
        .query("SELECT * FROM submission WHERE contest = $contest AND creator = $creator")
        .bind(("contest", contest))
        .bind(("creator", creator))
        .await?
        .take(0)?)
}

pub async fn list_by_problem(db: &Surreal<Client>, problem: Thing) -> Result<Vec<Submission>> {
    Ok(db
        .query("SELECT * FROM submission WHERE problem = $problem")
        .bind(("problem", problem))
        .await?
        .take(0)?)
}

const LIST_BY_PROBLEM_FOR_USER_QUERY: &str = r#"
SELECT * FROM submission WHERE record::id(problem) = $id AND record::id(creator) = $account_id ORDER BY created_at DESC
"#;
pub async fn list_by_problem_for_account(
    db: &Surreal<Client>,
    id: &str,
    account_id: &str,
) -> Result<Vec<Submission>> {
    Ok(db
        .query(LIST_BY_PROBLEM_FOR_USER_QUERY)
        .bind(("id", id.to_string()))
        .bind(("account_id", account_id.to_string()))
        .await?
        .take(0)?)
}