@@ -10,6 +10,8 @@ and serve the information back to the client.
10
10
11
11
module Database.CourseQueries
12
12
(retrievePost ,
13
+ returnPost ,
14
+ reqsForPost ,
13
15
returnCourse ,
14
16
prereqsForCourse ,
15
17
returnMeeting ,
@@ -23,9 +25,10 @@ module Database.CourseQueries
23
25
import Config (runDb )
24
26
import Control.Monad.IO.Class (MonadIO , liftIO )
25
27
import Data.Aeson (object , toJSON , Value )
28
+ import Data.Char (isAlphaNum , isPunctuation , isAlpha , isDigit )
26
29
import Data.List (partition )
27
30
import Data.Maybe (fromJust , fromMaybe )
28
- import qualified Data.Text as T (Text , append , tail , isPrefixOf , toUpper , filter , snoc , take )
31
+ import qualified Data.Text as T (Text , append , tail , isPrefixOf , toUpper , filter , snoc , take , unpack )
29
32
import Database.DataType ( ShapeType ( Node ) , ShapeType ( Hybrid ), ShapeType ( BoolNode ))
30
33
import Database.Persist.Sqlite (Entity , PersistEntity , SqlPersistM , PersistValue ( PersistInt64 ), selectList ,
31
34
entityKey , entityVal , selectFirst , (==.) , (<-.) , get , keyToValues , PersistValue ( PersistText ),
@@ -90,6 +93,24 @@ returnPost code = runDb $ do
90
93
Nothing -> return Nothing
91
94
Just post -> return $ Just $ entityVal post
92
95
96
+ -- | Retrieves the course requirements for a Post as a list of course codes
97
+ reqsForPost :: Post -> [String ]
98
+ reqsForPost post = do
99
+ let requirementsText = T. unpack $ postRequirements post
100
+ cleaned = filter (`notElem` (" <>" :: String )) $ filter (not . isPunctuation) requirementsText
101
+ potentialCodes = words cleaned
102
+ filter isCourseCode potentialCodes
103
+ where
104
+ -- | TODO: change function to use a regex
105
+ isCourseCode :: String -> Bool
106
+ isCourseCode codeStr =
107
+ length codeStr == 8 &&
108
+ all isAlphaNum codeStr &&
109
+ all isAlpha (take 3 codeStr) &&
110
+ all isDigit (take 3 (drop 3 codeStr)) &&
111
+ isAlpha (codeStr !! 6 ) &&
112
+ isDigit (codeStr !! 7 )
113
+
93
114
-- | Queries the database for all information regarding a specific meeting for
94
115
-- a @course@, returns a Meeting.
95
116
returnMeeting :: T. Text -> T. Text -> T. Text -> SqlPersistM (Entity Meeting )
0 commit comments