@@ -101,27 +101,6 @@ pub fn get_all_unapproved_query() -> String {
101101 format ! ( "SELECT {} FROM iqps WHERE approve_status = false and is_deleted=false ORDER BY upload_timestamp ASC" , ADMIN_DASHBOARD_QP_FIELDS )
102102}
103103
104- /// An enum representing the exam filter for the search query
105- pub enum ExamFilter {
106- Exam ( Exam ) , // Match an exact exam or use `ct` substring match
107- Any , // Match anything
108- MidEnd , // Midsem or endsem
109- }
110-
111- impl TryFrom < & String > for ExamFilter {
112- type Error = color_eyre:: eyre:: Error ;
113-
114- fn try_from ( value : & String ) -> Result < Self , Self :: Error > {
115- if value. is_empty ( ) {
116- Ok ( ExamFilter :: Any )
117- } else if value == "midend" {
118- Ok ( ExamFilter :: MidEnd )
119- } else {
120- Ok ( ExamFilter :: Exam ( Exam :: try_from ( value) ?) )
121- }
122- }
123- }
124-
125104/// Returns the query for searching question papers. It is mostly voodoo, see [blog post](https://rajivharlalka.in/posts/iqps-search-development/).
126105///
127106/// Optionally, the `exam` argument can be used to also add a clause to match the exam field.
@@ -131,21 +110,26 @@ impl TryFrom<&String> for ExamFilter {
131110/// $2 - Exam filter string (can be midsem, endsem, midend, or ct)
132111///
133112/// Returns the query and a boolean representing whether the second argument is required
134- pub fn get_qp_search_query ( exam_filter : ExamFilter ) -> ( String , bool ) {
135- let ( exam_filter, use_exam_arg) = match exam_filter {
136- ExamFilter :: Any => ( "" , false ) ,
137- ExamFilter :: MidEnd => (
138- "WHERE (exam = 'midsem' OR exam = 'endsem' OR exam = '')" ,
139- false ,
140- ) ,
141- ExamFilter :: Exam ( exam) => match exam {
142- Exam :: CT ( _) => ( "WHERE (exam LIKE 'ct%' OR exam = '')" , false ) ,
143- _ => ( "WHERE (exam = $2 OR exam = '')" , true ) ,
144- } ,
113+ pub fn get_qp_search_query ( exam_filter : Vec < Exam > ) -> String {
114+ let exam_filter_clause = exam_filter
115+ . iter ( )
116+ . map ( |& exam| {
117+ if let Exam :: CT ( _) = exam {
118+ "exam LIKE 'ct%'" . into ( )
119+ } else {
120+ format ! ( "exam = '{}'" , String :: from( exam) )
121+ }
122+ } )
123+ . collect :: < Vec < String > > ( )
124+ . join ( " OR " ) ;
125+
126+ let exam_clause_str = if exam_filter_clause. is_empty ( ) {
127+ "" . into ( )
128+ } else {
129+ format ! ( "WHERE ({} OR exam = '')" , exam_filter_clause)
145130 } ;
146131
147- (
148- format ! ( "
132+ format ! ( "
149133 WITH filtered AS (
150134 SELECT * from iqps {exam_filter} ORDER BY year DESC
151135 ),
@@ -189,11 +173,9 @@ pub fn get_qp_search_query(exam_filter: ExamFilter) -> (String, bool) {
189173 ) SELECT {search_qp_fields} FROM result" ,
190174 search_qp_fields = SEARCH_QP_FIELDS ,
191175 to_tsquery = "to_tsquery('simple', websearch_to_tsquery('simple', $1)::text || ':*')" ,
192- exam_filter = exam_filter ,
176+ exam_filter = exam_clause_str ,
193177 intermediate_fields = ADMIN_DASHBOARD_QP_FIELDS . split( ", " ) . map( |field| format!( "filtered.{}" , field) ) . collect:: <Vec <String >>( ) . join( ", " )
194- ) ,
195- use_exam_arg
196- )
178+ )
197179}
198180
199181/// List of fields in the [`crate::db::models::DBAdminDashboardQP`] to be used with SELECT clauses
0 commit comments