Model search options
This commit is contained in:
parent
9fcaf3f20d
commit
3e04e4f2cf
3 changed files with 39 additions and 26 deletions
|
@ -14,7 +14,7 @@ import Database.SQLite.Simple.Internal
|
|||
import Database.SQLite.Simple.Ok
|
||||
import Database.SQLite.Simple.QQ
|
||||
import Tutor.Card
|
||||
import Tutor.Search
|
||||
import qualified Tutor.Search as Search
|
||||
import Tutor.SearchParser
|
||||
import Tutor.Select
|
||||
( Select (..),
|
||||
|
@ -247,28 +247,28 @@ searchCards dbFile q sortBy inCollection =
|
|||
_ -> Just $ join "copies ON (cards.scryfall_id = copies.scryfall_id)"
|
||||
]
|
||||
conditions = case parseQuery (fromMaybe "" q) of
|
||||
Right (Search xs) -> mconcat $ [fromNamedCriteria p x | (p, x) <- zip whereParams xs]
|
||||
Right (Search.Search xs) -> mconcat $ [fromNamedCriteria p x | (p, x) <- zip whereParams xs]
|
||||
Left _ -> mempty
|
||||
|
||||
whereParams :: [T.Text]
|
||||
whereParams = [":where_" <> T.pack (show n) | n <- [1 ..]]
|
||||
|
||||
fromNamedCriteria :: T.Text -> Criteria -> Select Query
|
||||
fromNamedCriteria param (NameContains txt) =
|
||||
fromNamedCriteria :: T.Text -> Search.Criteria -> Select Query
|
||||
fromNamedCriteria param (Search.NameContains txt) =
|
||||
whereNamedParam "cards.name" "LIKE" param ("%" <> txt <> "%")
|
||||
fromNamedCriteria param (ExpansionCodeIs txt) =
|
||||
fromNamedCriteria param (Search.ExpansionCodeIs txt) =
|
||||
whereNamedParam "cards.set_code" "LIKE" param txt
|
||||
fromNamedCriteria param (CardTypeContains txt) =
|
||||
fromNamedCriteria param (Search.CardTypeContains txt) =
|
||||
whereNamedParam "cards.type_line" "LIKE" param ("%" <> txt <> "%")
|
||||
fromNamedCriteria param (OracleTextContains txt) =
|
||||
fromNamedCriteria param (Search.OracleTextContains txt) =
|
||||
whereNamedParam "cards.oracle_text" "LIKE" param ("%" <> txt <> "%")
|
||||
fromNamedCriteria param (ColorIdentityIs colors) =
|
||||
fromNamedCriteria param (Search.ColorIdentityIs colors) =
|
||||
whereNamedParam
|
||||
"cards.color_identity"
|
||||
"LIKE"
|
||||
param
|
||||
(mconcat $ fromColors colors)
|
||||
fromNamedCriteria param (ColorIdentityLTE colors) =
|
||||
fromNamedCriteria param (Search.ColorIdentityLTE colors) =
|
||||
if List.null colorList
|
||||
then mempty
|
||||
else
|
||||
|
@ -294,30 +294,30 @@ searchCards dbFile q sortBy inCollection =
|
|||
where
|
||||
colorList = fromColors colors
|
||||
colorParams = [param <> "_" <> color | color <- colorList]
|
||||
fromNamedCriteria param (ColorIdentityGTE colors) =
|
||||
fromNamedCriteria param (Search.ColorIdentityGTE colors) =
|
||||
whereNamedParam
|
||||
"cards.color_identity"
|
||||
"LIKE"
|
||||
param
|
||||
("%" <> mconcat (fromColors colors) <> "%")
|
||||
fromNamedCriteria param (RarityIs rarity) =
|
||||
fromNamedCriteria param (Search.RarityIs rarity) =
|
||||
whereNamedParam "cards.rarity" "LIKE" param (rarityName rarity)
|
||||
fromNamedCriteria param (RarityLTE rarity) =
|
||||
fromNamedCriteria param (Search.RarityLTE rarity) =
|
||||
whereNamedParam "rarities.rarity_ord" "<=" param (rarityValue rarity)
|
||||
fromNamedCriteria param (RarityGTE rarity) =
|
||||
fromNamedCriteria param (Search.RarityGTE rarity) =
|
||||
whereNamedParam "rarities.rarity_ord" ">=" param (rarityValue rarity)
|
||||
|
||||
fromColors :: Colors -> [T.Text]
|
||||
fromColors (Colors colors) = map (T.pack . show) (Set.toList colors)
|
||||
fromColors :: Search.Colors -> [T.Text]
|
||||
fromColors (Search.Colors colors) = map (T.pack . show) (Set.toList colors)
|
||||
|
||||
rarityName :: Rarity -> T.Text
|
||||
rarityName Common = "common"
|
||||
rarityName Uncommon = "uncommon"
|
||||
rarityName Rare = "rare"
|
||||
rarityName Mythic = "mythic"
|
||||
rarityName :: Search.Rarity -> T.Text
|
||||
rarityName Search.Common = "common"
|
||||
rarityName Search.Uncommon = "uncommon"
|
||||
rarityName Search.Rare = "rare"
|
||||
rarityName Search.Mythic = "mythic"
|
||||
|
||||
rarityValue :: Rarity -> Int
|
||||
rarityValue Common = 1
|
||||
rarityValue Uncommon = 2
|
||||
rarityValue Rare = 3
|
||||
rarityValue Mythic = 5
|
||||
rarityValue :: Search.Rarity -> Int
|
||||
rarityValue Search.Common = 1
|
||||
rarityValue Search.Uncommon = 2
|
||||
rarityValue Search.Rare = 3
|
||||
rarityValue Search.Mythic = 5
|
||||
|
|
|
@ -6,6 +6,19 @@ import qualified Data.Text as T
|
|||
|
||||
newtype Search = Search [Criteria] deriving (Eq, Show)
|
||||
|
||||
data SearchOptions = SearchOptions
|
||||
{ inCollection :: Bool,
|
||||
orderBy :: OrderBy
|
||||
}
|
||||
|
||||
data OrderBy
|
||||
= Rarity SearchOrder
|
||||
| Price SearchOrder
|
||||
|
||||
data SearchOrder
|
||||
= Ascending
|
||||
| Descending
|
||||
|
||||
data Criteria
|
||||
= NameContains T.Text
|
||||
| ExpansionCodeIs T.Text
|
||||
|
|
|
@ -27,7 +27,7 @@ import Data.String
|
|||
import Data.Text
|
||||
import Database.SQLite.Simple
|
||||
import Database.SQLite.Simple.Ok
|
||||
import Tutor.Search
|
||||
|
||||
|
||||
data Select a = Select
|
||||
{ selectColumns :: [a],
|
||||
|
|
Loading…
Reference in a new issue