Skip to main content

lnu_elytra/
course.rs

1use serde::Serialize;
2
3use crate::method::SelectCourseResponse;
4use crate::{
5    Client,
6    error::{Error, R},
7};
8
9#[cfg_attr(
10    feature = "__pyo3",
11    cfg_attr(test, pyo3_stub_gen::derive::gen_stub_pyclass),
12    derive(Clone),
13    pyo3::pyclass(get_all, from_py_object)
14)]
15#[derive(Debug, Serialize)]
16pub struct Jxb {
17    pub jxb_id: String,
18    pub do_id: String,
19    pub jsxx: String,
20    pub sksj: String,
21}
22
23#[cfg_attr(
24    feature = "__pyo3",
25    cfg_attr(test, pyo3_stub_gen::derive::gen_stub_pyclass),
26    derive(Clone),
27    pyo3::pyclass(get_all, from_py_object)
28)]
29#[derive(Debug, Serialize)]
30pub struct Course {
31    pub xkkz_id: String,
32    pub kch_id: String,
33    pub jxb: Vec<Jxb>,
34}
35
36impl Course {
37    pub async fn try_select_0(&self, i: &Client) -> R<SelectCourseResponse> {
38        let do_id = &self
39            .jxb
40            .get(0)
41            .ok_or(Error::JxbNotFound("try_select_0"))?
42            .do_id;
43
44        i.select_course(&self.kch_id, do_id).await
45    }
46}
47
48impl Course {
49    // 星期四第9-10节{9-16周}
50    // 16周
51    pub async fn try_select_by_time(&self, i: &Client, q: &str) -> R<SelectCourseResponse> {
52        let coures_id = &self.kch_id;
53
54        let do_id = &self
55            .jxb
56            .iter()
57            .filter(|x| x.sksj.contains(q))
58            .collect::<Vec<&Jxb>>();
59
60        let do_id = &do_id
61            .get(0)
62            .ok_or(Error::JxbNotFound("try_select_by_time"))?
63            .do_id;
64
65        i.select_course(coures_id, do_id).await
66    }
67}