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
use vit_servicing_station_lib::db::models::proposals::ChallengeType;
use vit_servicing_station_lib::v0::endpoints::search::requests::{
    Column, Constraint, OrderBy, SearchCountQuery, SearchQuery, Table,
};

pub struct SearchRequestBuilder {
    query: SearchQuery,
}

#[allow(clippy::from_over_into)]
impl Into<SearchQuery> for SearchRequestBuilder {
    fn into(self) -> SearchQuery {
        self.query
    }
}

impl Default for SearchRequestBuilder {
    fn default() -> Self {
        Self {
            query: SearchQuery {
                query: SearchCountQuery {
                    table: Table::Challenges,
                    filter: vec![],
                    order_by: vec![],
                },
                limit: None,
                offset: None,
            },
        }
    }
}

impl SearchRequestBuilder {
    pub fn on_challenges(self) -> Self {
        self.on(Table::Challenges)
    }

    pub fn on_proposals(self) -> Self {
        self.on(Table::Proposals)
    }

    pub fn on(mut self, table: Table) -> Self {
        self.query.query.table = table;
        self
    }

    pub fn by_funds_exact(self, funds: i64) -> Self {
        self.by_funds(Some(funds), Some(funds))
    }

    pub fn by_funds(self, lower: Option<i64>, upper: Option<i64>) -> Self {
        self.by_range(lower, upper, Column::Funds)
    }

    pub fn by_impact_score(self, impact_score: i64) -> Self {
        self.by_range(Some(impact_score), Some(impact_score), Column::ImpactScore)
    }

    pub fn by_author(self, author: impl Into<String>) -> Self {
        self.by_text(author, Column::Author)
    }

    pub fn offset(mut self, offset: u64) -> Self {
        self.query.offset = Some(offset);
        self
    }

    pub fn limit(mut self, limit: u64) -> Self {
        self.query.limit = Some(limit);
        self
    }

    pub fn by_body(self, body: impl Into<String>) -> Self {
        self.by_text(body, Column::Desc)
    }

    pub fn by_title(self, title: impl Into<String>) -> Self {
        self.by_text(title.into(), Column::Title)
    }

    pub fn by_type(self, challenge_type: &ChallengeType) -> Self {
        self.by_text(challenge_type.to_string(), Column::Type)
    }

    pub fn by_text(mut self, phrase: impl Into<String>, column: Column) -> Self {
        self.query.query.filter.push(Constraint::Text {
            search: phrase.into(),
            column,
        });
        self
    }

    pub fn by_range(mut self, lower: Option<i64>, upper: Option<i64>, column: Column) -> Self {
        self.query.query.filter.push(Constraint::Range {
            upper,
            lower,
            column,
        });
        self
    }

    pub fn order_by_title(self) -> Self {
        self.order_by_desc(Column::Title)
    }

    pub fn order_by_asc(mut self, column: Column) -> Self {
        self.query.query.order_by.push(OrderBy::Column {
            column,
            descending: false,
        });
        self
    }

    pub fn order_by_desc(mut self, column: Column) -> Self {
        self.query.query.order_by.push(OrderBy::Column {
            column,
            descending: true,
        });
        self
    }

    pub fn order_by_random(mut self) -> Self {
        self.query.query.order_by.push(OrderBy::Random);
        self
    }
}