forked from Courseography/courseography
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse.hs
More file actions
37 lines (33 loc) · 1.38 KB
/
Course.hs
File metadata and controls
37 lines (33 loc) · 1.38 KB
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
module Controllers.Course
(retrieveCourse, index, courseInfo) where
import Config (runDb)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Text as T (Text, unlines)
import Database.Persist (Entity)
import Database.Persist.Sqlite (SqlPersistM, entityVal, selectList)
import Database.Tables as Tables (Courses, coursesCode)
import Happstack.Server (Response, ServerPart, lookText', notFound, ok, toResponse)
import Models.Course (getDeptCourses, returnCourse)
import Util.Happstack (createJSONResponse)
-- | Takes a course code (e.g. \"CSC108H1\") and sends a JSON representation
-- of the course as a response.
retrieveCourse :: ServerPart Response
retrieveCourse = do
name <- lookText' "name"
courses <- liftIO $ returnCourse name
case courses of
Just x -> ok $ createJSONResponse x
Nothing -> notFound $ toResponse ("Course not found" :: String)
-- | Builds a list of all course codes in the database.
index :: ServerPart Response
index = do
response <- liftIO $ runDb $ do
coursesList :: [Entity Courses] <- selectList [] []
let codes = map (coursesCode . entityVal) coursesList
return $ T.unlines codes :: SqlPersistM T.Text
return $ toResponse response
-- | Returns all course info for a given department.
courseInfo :: ServerPart Response
courseInfo = do
dept <- lookText' "dept"
fmap createJSONResponse (getDeptCourses dept)