alter category to optional

This commit is contained in:
Lucy Cifferello
2021-09-08 15:39:29 -06:00
parent 42a8adebf0
commit c38ea1480b
3 changed files with 18 additions and 12 deletions

View File

@@ -13,8 +13,8 @@ import Data.HashMap.Strict
import Data.Version import Data.Version
import Data.Aeson import Data.Aeson
searchServices :: MonadIO m => CategoryTitle -> Int64 -> Int64 -> Text -> ReaderT SqlBackend m [P.Entity SApp] searchServices :: MonadIO m => Maybe CategoryTitle -> Int64 -> Int64 -> Text -> ReaderT SqlBackend m [P.Entity SApp]
searchServices ANY pageItems offset' query = select $ do searchServices Nothing pageItems offset' query = select $ do
service <- from $ table @SApp service <- from $ table @SApp
where_ ((service ^. SAppDescShort `ilike` (%) ++. val query ++. (%)) where_ ((service ^. SAppDescShort `ilike` (%) ++. val query ++. (%))
||. (service ^. SAppDescLong `ilike` (%) ++. val query ++. (%)) ||. (service ^. SAppDescLong `ilike` (%) ++. val query ++. (%))
@@ -24,7 +24,7 @@ searchServices ANY pageItems offset' query = select $ do
limit pageItems limit pageItems
offset offset' offset offset'
pure service pure service
searchServices category pageItems offset' query = select $ do searchServices (Just category) pageItems offset' query = select $ do
services <- from services <- from
(do (do
(service :& sc) <- (service :& sc) <-

View File

@@ -142,7 +142,7 @@ data ServiceListDefaults = ServiceListDefaults
{ serviceListOrder :: OrderArrangement { serviceListOrder :: OrderArrangement
, serviceListPageLimit :: Int64 -- the number of items per page , serviceListPageLimit :: Int64 -- the number of items per page
, serviceListPageNumber :: Int64 -- the page you are on , serviceListPageNumber :: Int64 -- the page you are on
, serviceListCategory :: CategoryTitle , serviceListCategory :: Maybe CategoryTitle
, serviceListQuery :: Text , serviceListQuery :: Text
} }
deriving (Eq, Show, Read) deriving (Eq, Show, Read)
@@ -224,7 +224,7 @@ getPackageListR = do
{ serviceListOrder = DESC { serviceListOrder = DESC
, serviceListPageLimit = 20 , serviceListPageLimit = 20
, serviceListPageNumber = 1 , serviceListPageNumber = 1
, serviceListCategory = ANY , serviceListCategory = Nothing
, serviceListQuery = "" , serviceListQuery = ""
} }
case lookup "ids" getParameters of case lookup "ids" getParameters of
@@ -236,7 +236,7 @@ getPackageListR = do
Nothing -> do Nothing -> do
$logInfo c $logInfo c
sendResponseStatus status400 ("could not read category" :: Text) sendResponseStatus status400 ("could not read category" :: Text)
Just t -> pure t Just t -> pure $ Just t
page <- case lookup "page" getParameters of page <- case lookup "page" getParameters of
Nothing -> pure $ serviceListPageNumber defaults Nothing -> pure $ serviceListPageNumber defaults
Just p -> case readMaybe p of Just p -> case readMaybe p of

View File

@@ -8,15 +8,14 @@ import Database.Persist.Postgresql
import Data.Aeson import Data.Aeson
import Control.Monad import Control.Monad
import Yesod.Core import Yesod.Core
import qualified Data.Text as T
data CategoryTitle = FEATURED data CategoryTitle = FEATURED
| BITCOIN | BITCOIN
| LIGHTNING | LIGHTNING
| DATA | DATA
| MESSAGING | MESSAGING
| SOCIAL | SOCIAL
| NONE | ALTCOIN
| ANY
deriving (Eq, Enum, Show, Read) deriving (Eq, Enum, Show, Read)
instance PersistField CategoryTitle where instance PersistField CategoryTitle where
fromPersistValue = fromPersistValueJSON fromPersistValue = fromPersistValueJSON
@@ -24,7 +23,15 @@ instance PersistField CategoryTitle where
instance PersistFieldSql CategoryTitle where instance PersistFieldSql CategoryTitle where
sqlType _ = SqlString sqlType _ = SqlString
instance ToJSON CategoryTitle where instance ToJSON CategoryTitle where
toJSON = String . T.toLower . show -- toJSON = String . T.toLower . show
toJSON = \case
FEATURED -> "featured"
BITCOIN -> "bitcoin"
LIGHTNING -> "lightning"
DATA -> "data"
MESSAGING -> "messaging"
SOCIAL -> "social"
ALTCOIN -> "alt coin"
instance FromJSON CategoryTitle where instance FromJSON CategoryTitle where
parseJSON = withText "CategoryTitle" $ \case parseJSON = withText "CategoryTitle" $ \case
"featured" -> pure FEATURED "featured" -> pure FEATURED
@@ -33,8 +40,7 @@ instance FromJSON CategoryTitle where
"data" -> pure DATA "data" -> pure DATA
"messaging" -> pure MESSAGING "messaging" -> pure MESSAGING
"social" -> pure SOCIAL "social" -> pure SOCIAL
"none" -> pure NONE "alt coin" -> pure ALTCOIN
"any" -> pure ANY
_ -> fail "unknown category title" _ -> fail "unknown category title"
instance ToContent CategoryTitle where instance ToContent CategoryTitle where
toContent = toContent . toJSON toContent = toContent . toJSON