diff --git a/app/Main.hs b/app/Main.hs index 204fd47..4582179 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,12 +1,13 @@ module Main (main) where import System.Environment - +import Site.Host import Lib + main :: IO () main = do args <- getArgs case args of ["--seed"] -> seedData - _ -> putStrLn "Run the main app" + _ -> host diff --git a/mtgsearch.cabal b/mtgsearch.cabal index e668509..0ba1e29 100644 --- a/mtgsearch.cabal +++ b/mtgsearch.cabal @@ -25,8 +25,10 @@ source-repository head library exposed-modules: + Algorithm.Search Config Lib + Site.Host other-modules: Paths_mtgsearch hs-source-dirs: @@ -36,6 +38,8 @@ library aeson , base >=4.7 && <5 , bytestring + , directory + , scotty , sqlite-simple , text default-language: Haskell2010 @@ -51,7 +55,9 @@ executable mtgsearch-exe aeson , base >=4.7 && <5 , bytestring + , directory , mtgsearch + , scotty , sqlite-simple , text default-language: Haskell2010 @@ -68,7 +74,9 @@ test-suite mtgsearch-test aeson , base >=4.7 && <5 , bytestring + , directory , mtgsearch + , scotty , sqlite-simple , text default-language: Haskell2010 diff --git a/package.yaml b/package.yaml index afcf169..885e990 100644 --- a/package.yaml +++ b/package.yaml @@ -25,7 +25,8 @@ dependencies: - text - bytestring - sqlite-simple - +- scotty +- directory ghc-options: - -Wall diff --git a/src/Algorithm/Search.hs b/src/Algorithm/Search.hs new file mode 100644 index 0000000..df51343 --- /dev/null +++ b/src/Algorithm/Search.hs @@ -0,0 +1,29 @@ +{-# LANGUAGE OverloadedStrings #-} +module Algorithm.Search + ( search + ) where +import Database.SQLite.Simple +import Config (getDbPath) +import Web.Scotty.Internal.Types +import Control.Monad +import qualified Data.Text as T + + +data TestField = TestField Int T.Text deriving (Show) + +instance FromRow TestField where + fromRow = TestField <$> field <*> field + +instance ToRow TestField where + toRow (TestField id_ str) = toRow (id_, str) + +search :: String -> IO String +search q = do + simpleSearch q + +simpleSearch :: String -> IO String +simpleSearch q = do + dbPath <- getDbPath + conn <- open dbPath + res <- queryNamed conn "select id, name from card where name like :name" [":name" := ("%"++q++"%")] :: IO [TestField] + return (show res) \ No newline at end of file diff --git a/src/Lib.hs b/src/Lib.hs index 834a6b5..22a5e5a 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -52,10 +52,7 @@ seedData = do conn <- open dbPath execute_ conn "CREATE TABLE IF NOT EXISTS card (id INTEGER PRIMARY KEY, scryfall_id TEXT, lang TEXT, name TEXT, oracle_text TEXT)" - -- If d is Left, the JSON was malformed. - -- In that case, we report the error. - -- Otherwise, we perform the operation of - -- our choice. In this case, just print it. + case d of Left err -> putStrLn err Right ps -> insertCards conn ps diff --git a/src/Site/Host.hs b/src/Site/Host.hs new file mode 100644 index 0000000..6fe7eb9 --- /dev/null +++ b/src/Site/Host.hs @@ -0,0 +1,18 @@ +{-# LANGUAGE DeriveGeneric, OverloadedStrings #-} +module Site.Host + ( host + ) where +import Web.Scotty + +import Algorithm.Search +import Control.Monad.IO.Class (MonadIO(liftIO)) +import Data.Text.Lazy (pack) + +host = scotty 3000 $ do + post "/api/req" $ do + query <- param "query" + result <- liftIO (search query) + html $ mconcat ["

", pack result, "

"] + get "/" $ file "src/Site/Static/index.html" + + diff --git a/src/Site/Static/index.html b/src/Site/Static/index.html new file mode 100644 index 0000000..ad76060 --- /dev/null +++ b/src/Site/Static/index.html @@ -0,0 +1,18 @@ + + + + +

This is a heading

+ +

This site will be to search for magic the gathering cards using a custom sort of DSL!

+ + +
+ + +
+ + + + +