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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use sea_orm::{prelude::*, DatabaseConnection, IntoActiveModel, Set};

use crate::app_ui::async_task_channel::{Response, TaskResponse};
use crate::app_ui::widgets::notification::WidgetName;
use crate::entities::{QuestionModel, TopicTagEntity};
use crate::graphql::editor_data::Query as QuestionEditorDataQuery;
use crate::graphql::question_content::Query as QuestionGQLQuery;
use crate::graphql::run_code::RunCode;
use crate::graphql::{self, GQLLeetcodeQuery, RunOrSubmitCode};

pub async fn get_question_details(
    request_id: String,
    widget_name: WidgetName,
    slug: String,
    client: &reqwest::Client,
    _conn: &DatabaseConnection,
) -> TaskResponse {
    match QuestionGQLQuery::new(slug).send(client).await {
        Ok(resp) => {
            let query_response = resp;
            TaskResponse::QuestionDetail(Response {
                request_id,
                content: query_response.data.question,
                widget_name,
            })
        }
        Err(e) => TaskResponse::Error(Response {
            request_id,
            content: e.to_string(),
            widget_name,
        }),
    }
}

pub async fn get_editor_data(
    request_id: String,
    widget_name: WidgetName,
    slug: String,
    client: &reqwest::Client,
    _conn: &DatabaseConnection,
) -> TaskResponse {
    match QuestionEditorDataQuery::new(slug).send(client).await {
        Ok(data) => TaskResponse::QuestionEditorData(Response {
            request_id,
            content: data.data.question,
            widget_name,
        }),
        Err(e) => TaskResponse::Error(Response {
            request_id,
            content: e.to_string(),
            widget_name,
        }),
    }
}

pub async fn get_all_questions(
    request_id: String,
    widget_name: WidgetName,
    _content: (),
    _client: &reqwest::Client,
    conn: &DatabaseConnection,
) -> TaskResponse {
    match TopicTagEntity::get_all_topic_questions_map(conn).await {
        Ok(map) => TaskResponse::GetAllQuestionsMap(Response {
            request_id,
            content: map,
            widget_name,
        }),
        Err(e) => TaskResponse::Error(Response {
            request_id,
            content: e.to_string(),
            widget_name,
        }),
    }
}

pub async fn get_all_topic_tags(
    request_id: String,
    widget_name: WidgetName,
    _content: (),
    _client: &reqwest::Client,
    conn: &DatabaseConnection,
) -> TaskResponse {
    match TopicTagEntity::get_all_topics(conn).await {
        Ok(t_tags) => TaskResponse::AllTopicTags(Response {
            request_id,
            content: t_tags,
            widget_name,
        }),
        Err(e) => TaskResponse::Error(Response {
            request_id,
            content: e.to_string(),
            widget_name,
        }),
    }
}

pub async fn run_or_submit_question(
    request_id: String,
    widget_name: WidgetName,
    mut run_or_submit_code: graphql::RunOrSubmitCode,
    client: &reqwest::Client,
    _conn: &DatabaseConnection,
) -> TaskResponse {
    if let RunOrSubmitCode::Run(RunCode {
        test_cases_stdin,
        slug,
        ..
    }) = &mut run_or_submit_code
    {
        match graphql::console_panel_config::Query::new(slug.clone())
            .send(client)
            .await
        {
            Ok(resp) => {
                *test_cases_stdin = Some(resp.data.question.example_testcase_list.join("\n"));
            }
            Err(e) => {
                return TaskResponse::Error(Response {
                    request_id,
                    content: e.to_string(),
                    widget_name,
                })
            }
        }
    }

    match run_or_submit_code.post(client).await {
        Ok(run_response_body) => TaskResponse::RunResponseData(Response {
            request_id,
            content: run_response_body,
            widget_name,
        }),
        Err(e) => TaskResponse::Error(Response {
            request_id,
            content: e.to_string(),
            widget_name,
        }),
    }
}

pub async fn update_status_to_accepted(
    request_id: String,
    widget_name: WidgetName,
    question: QuestionModel,
    _client: &reqwest::Client,
    db: &DatabaseConnection,
) -> TaskResponse {
    let mut am = question.into_active_model();
    am.status = Set(Some("ac".to_string()));
    match am.update(db).await {
        Ok(_) => TaskResponse::DbUpdateStatus(Response {
            request_id,
            content: (),
            widget_name,
        }),
        Err(e) => TaskResponse::Error(Response {
            request_id,
            content: e.to_string(),
            widget_name,
        }),
    }
}