-
Notifications
You must be signed in to change notification settings - Fork 74
Refactor Building functions into Model #1715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AngelsandDevsLOL
wants to merge
4
commits into
Courseography:master
Choose a base branch
from
AngelsandDevsLOL:refactor-building-into-Model
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
185a8e3
Refactor building-related functions into Models/Building
AngelsandDevsLOL 7e109cc
Update changelog
AngelsandDevsLOL 6fbc319
Update cabal to include Models.Building
AngelsandDevsLOL 77c4fb7
refactor imports for backend tests involving buildTimes and Building
AngelsandDevsLOL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| module Models.Building | ||
| (buildingsCSV, | ||
| parseBuildings, | ||
| getBuildingsFromCSV, | ||
| getBuilding, | ||
| buildTime, | ||
| buildTimes) where | ||
|
|
||
| import Config (runDb) | ||
| import Control.Monad.IO.Class (liftIO) | ||
| import Data.CSV | ||
| import qualified Data.Text as T | ||
| import Database.Persist.Sqlite (Filter, SqlPersistM, deleteWhere, entityVal, insertMany_, | ||
| selectFirst, (==.)) | ||
| import Database.Tables | ||
| import Filesystem.Path.CurrentOS as Path | ||
| import System.Directory (getCurrentDirectory) | ||
| import Text.ParserCombinators.Parsec (parseFromFile) | ||
| import Util.Helpers | ||
|
|
||
| buildingsCSV :: IO Prelude.FilePath | ||
| buildingsCSV = do | ||
| curDir <- getCurrentDirectory | ||
| return $ Path.encodeString $ Path.append (Path.decodeString curDir) $ Path.append (Path.decodeString "db") (Path.decodeString "building.csv") | ||
|
|
||
| parseBuildings :: IO () | ||
| parseBuildings = do | ||
| buildingInfo <- getBuildingsFromCSV =<< buildingsCSV | ||
| runDb $ do | ||
| liftIO $ putStrLn "Inserting buildings" | ||
| deleteWhere ([] :: [Filter Building]) :: SqlPersistM () | ||
| insertMany_ buildingInfo :: SqlPersistM () | ||
|
|
||
| -- | Extract building names, codes, addresses, postal codes, latitude and longitude from csv file | ||
| getBuildingsFromCSV :: String -> IO [Building] | ||
| getBuildingsFromCSV buildingCSVFile = do | ||
| buildingCSVData <- parseFromFile csvFile buildingCSVFile | ||
| case buildingCSVData of | ||
| Left _ -> error "csv parse error" | ||
| Right buildingData -> | ||
| return $ map (\b -> Building (T.pack $ safeHead "" b) | ||
| (T.pack (b !! 1)) | ||
| (T.pack (b !! 2)) | ||
| (T.pack (b !! 3)) | ||
| (read (b !! 4) :: Double) | ||
| (read (b !! 5) :: Double)) $ drop 1 buildingData | ||
|
|
||
| -- | Given a building code, get the persistent Building associated with it | ||
| getBuilding :: Maybe T.Text -> SqlPersistM (Maybe Building) | ||
| getBuilding rm = | ||
| case rm of | ||
| Nothing -> return Nothing | ||
| Just r -> do | ||
| maybeEntityBuilding <- selectFirst [BuildingCode ==. T.take 2 r] [] | ||
| case maybeEntityBuilding of | ||
| Nothing -> return Nothing | ||
| Just entBuilding -> return $ Just (entityVal entBuilding) | ||
|
|
||
| -- | Convert a Times record into a Time by resolving room codes to Buildings | ||
| buildTime :: Times -> SqlPersistM Time | ||
| buildTime t = do | ||
| room1 <- getBuilding (timesFirstRoom t) | ||
| room2 <- getBuilding (timesSecondRoom t) | ||
| return $ Time (timesWeekDay t) | ||
| (timesStartHour t) | ||
| (timesEndHour t) | ||
| room1 | ||
| room2 | ||
|
|
||
| buildTimes :: Key Meeting -> Time' -> Times | ||
| buildTimes meetingKey t = | ||
| Times (weekDay' t) | ||
| (startHour' t) | ||
| (endHour' t) | ||
| meetingKey | ||
| (firstLocation' t) | ||
| (secondLocation' t) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave
buildTimeandbuildTimesin theTables.hsmodule. You're correct that there's noTimemodel, which is something we can improve on later.