Skip to content

Commit

Permalink
More fixes to the structure
Browse files Browse the repository at this point in the history
  • Loading branch information
beingfranklin committed Dec 10, 2020
1 parent 5f1719f commit 95c7c4d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 39 deletions.
50 changes: 19 additions & 31 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Parse

main :: IO ()
main = do
putStrLn "[CountryCode] - Country Codes available\n\
putStrLn "\n[CountryCode] - Country Codes available\n\
\AU Australia\n\
\BR Brazil\n\
\CA Canada\n\
Expand All @@ -17,37 +17,28 @@ main = do
\GB United Kingdom\n\
\Input Country Code - \n"
countryCodeInput <- getLine
putStrLn "[Year] - Country Codes available\n\
putStrLn "\n[Year] - Country Codes available\n\
\2019 Calendar year 2019\n\
\2020 Calendar year 2020\n\
\2021 Calendar year 2021\n\
\Input Year Code - \n"
yearInput <- getLine
let url = "https://date.nager.at/api/v2/publicholidays/"++yearInput++"/"++countryCodeInput
print "Downloading..."
putStrLn "\nDownloading..."
json <- download url
print "Parsing..."
putStrLn "\nParsing..."
case parse json of
Left err -> print err
Right recs -> do
-- print recs
putStrLn "Saving on DB..."
-- putStrLn "\n****************"
putStrLn "\nSaving on DB...\n"
conn <- initialiseDB
insertDB conn recs
insertSB conn recs
insertLB conn recs
putStrLn "Done!"
putStrLn "\n****************"
res <- queryDB conn countryCodeInput
putStrLn "\n****************"
-- putStrLn "Before Formatting Query"
-- putStr $ show res
-- putStrLn "\n****************"
putStrLn $ "\nList of Holidays in " ++ countryCodeInput
putStrLn $ "\nTotal List of Holidays in " ++ countryCodeInput ++ " in the calendar year "++ yearInput
let converRes = sqlRowToString res
mapM_ putStrLn converRes
putStrLn "\n****************"
putStrLn "\nSee the holidays between specific dates... "
putStrLn "\nInput the start date [DD-MMM-YY] "
startDate <- getLine
Expand All @@ -56,35 +47,32 @@ main = do
dateSort <- selectHolidaysInDateRange conn startDate endDate
putStrLn $ "\nHolidays between " ++startDate ++ " and " ++ endDate ++ " are : \n"
-- TODO Format the dates
print (concat dateSort)
putStrLn "\n****************"
putStrLn "\nDo you want to see Global (or Local) holidays? Type Y for global and N for local"
mapM_ putStrLn $ dateSort
putStrLn $ "\nDo you want to see International or Local holidays in " ++ countryCodeInput ++" ?\nType Y for global and N for local -"
inputGlobal <- getChar
putStrLn "\nGlobal/Local Holidays are : \n"
case inputGlobal of
'Y' -> do
putStrLn "\nInternational Holidays are : \n"
names<- getLocalNames conn True
print $ show names
putStrLn $ unlines names
'y' -> do
putStrLn "\nInternational Holidays are : \n"
names<- getLocalNames conn True
print $ show names
putStrLn $ unlines names
'N' -> do
putStrLn "\nLocal Holidays are : \n"
names<- getLocalNames conn False
print $ show names
putStrLn $ unlines names
'n' -> do
putStrLn "\nLocal Holidays are : \n"
names<- getLocalNames conn False
print $ show names
putStrLn $ unlines names
_ -> syntaxErrorForIsGlobal
putStrLn "\n****************"
putStrLn $ "Number of rows in Table is " ++ show (length res)
putStrLn "\n****************"
-- | JSON Conversion code is below
putStrLn $ "\nPreparing to write into the JSON file "
putStrLn "\nWriting ..."
jsonValue <- convertToJSON conn
print $ show jsonValue
-- | JSON Conversion -> writing into file should be added below
writeFile "DB.json" (jsonValue)
print "Done!"
putStrLn "\n****************"
putStrLn "\nFinished Writing!"


syntaxErrorForIsGlobal :: IO ()
Expand Down
5 changes: 1 addition & 4 deletions src/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ insertDB :: Connection -> [HolidayRecord] -> IO ()
insertDB conn records = do
let xs = records
stmt <- prepare conn "INSERT INTO holidays (date,localName,name) VALUES (?,?,?)" -- this fuction use to insert the values into the parameters under the holidays table
putStrLn "Adding" -- this fuction to Type for s string with newline character at the end
executeMany stmt (map (\x -> [toSql (date x), toSql (localName x), toSql (name x)]) xs) -- this function use for prepares a database operation and executes it against the parameters sequences
commit conn -- this fuction to commit a transaction to the underlying database

Expand All @@ -74,7 +73,6 @@ insertLB :: Connection -> [HolidayRecord] -> IO ()
insertLB conn records = do
let xs = records
stmt <- prepare conn "INSERT INTO countries (countryCode,global,fixed) VALUES (?,?,?)" -- this fuction use to insert the values into the parameters under the country table
putStrLn "Adding" -- this fuction to Type for s string with newline character at the end
executeMany stmt (map (\x -> [toSql (countryCode x), toSql (global x), toSql (fixed x)]) xs) -- this function use for prepares a database operation and executes it against the parameters sequences
commit conn -- this fuction to commit a transaction to the underlying database

Expand All @@ -83,9 +81,8 @@ insertSB :: Connection -> [HolidayRecord] -> IO ()
insertSB conn records = do
let xs = records
stmt <- prepare conn "INSERT INTO country_holidays (countryCode,localName) VALUES (?,?)" -- this fuction use to insert the values into the parameters under the country_holidays
putStrLn "Adding" -- this fuction to Type for s string with newline character at the end
executeMany stmt (map (\x -> [toSql (countryCode x), toSql (localName x)]) xs) -- this function use for prepares a database operation and executes it against the parameters sequences
commit conn -- this fuction to commit a transaction to the underlying database
commit conn -- this function to commit a transaction to the underlying database

-- | This function will select all the holidays of a given country
queryDB :: Connection -> String -> IO [[SqlValue]]
Expand Down
4 changes: 2 additions & 2 deletions src/Http.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- |HTTP module header
-- | HTTP module header
module Http
( download
) where

import qualified Data.ByteString.Lazy.Char8 as L8
import Network.HTTP.Simple
( parseRequest, getResponseBody, httpLBS )
-- |Download function is for converting JSON from URL
-- | This function is for converting JSON from URL
download :: String -> IO L8.ByteString
download url = do
request <- parseRequest url
Expand Down
4 changes: 2 additions & 2 deletions src/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import GHC.Generics (Generic)
-- }
-- ]

-- |HolidayRecord
-- |HolidayRecord constructor
data HolidayRecord = HolidayRecord
{ date :: String,
localName :: String,
Expand All @@ -32,6 +32,6 @@ data HolidayRecord = HolidayRecord
instance FromJSON HolidayRecord
instance ToJSON HolidayRecord

-- |Parse function for converting JSON to text
-- | This function is for converting JSON to text
parse :: L8.ByteString -> Either String [HolidayRecord]
parse json = eitherDecode json :: Either String [HolidayRecord]

0 comments on commit 95c7c4d

Please sign in to comment.