aggregate query functions

This commit is contained in:
Lucy Cifferello
2021-09-21 23:51:45 -06:00
committed by Keagan McClelland
parent e2d2fb6afc
commit 7b2684acd5
11 changed files with 392 additions and 42 deletions

View File

@@ -14,6 +14,7 @@ type S9ErrT m = ExceptT S9Error m
data S9Error =
PersistentE Text
| AppMgrE Text Int
| NotFoundE Text
deriving (Show, Eq)
instance Exception S9Error
@@ -23,10 +24,12 @@ toError :: S9Error -> Error
toError = \case
PersistentE t -> Error DATABASE_ERROR t
AppMgrE cmd code -> Error APPMGR_ERROR [i|"appmgr #{cmd}" exited with #{code}|]
NotFoundE e -> Error NOT_FOUND [i|#{e}|]
data ErrorCode =
DATABASE_ERROR
| APPMGR_ERROR
| NOT_FOUND
deriving (Eq, Show)
instance ToJSON ErrorCode where
@@ -53,6 +56,7 @@ toStatus :: S9Error -> Status
toStatus = \case
PersistentE _ -> status500
AppMgrE _ _ -> status500
NotFoundE _ -> status404
handleS9ErrT :: MonadHandler m => S9ErrT m a -> m a

View File

@@ -18,12 +18,57 @@ import Lib.Types.Emver
import Orphans.Emver ( )
import System.Directory
import Lib.Registry
import Model
-- import Model
import qualified Data.Text as T
import Data.String.Interpolate.IsString
import qualified Data.ByteString.Lazy as BS
import Database.Persist.Postgresql
import Yesod
import Data.Functor.Contravariant ( Contravariant(contramap) )
import qualified GHC.Read ( Read(..) )
import qualified GHC.Show ( Show(..) )
import Database.PostgreSQL.Simple.ToField
import Database.PostgreSQL.Simple.FromField
import Data.Binary.Builder
type AppIdentifier = Text
newtype AppIdentifier = AppIdentifier { unAppIdentifier :: Text }
deriving (Eq)
instance IsString AppIdentifier where
fromString = AppIdentifier . fromString
instance Show AppIdentifier where
show = toS . unAppIdentifier
instance Read AppIdentifier where
readsPrec _ s = [(AppIdentifier $ toS s, "")]
instance Hashable AppIdentifier where
hashWithSalt n = hashWithSalt n . unAppIdentifier
instance FromJSON AppIdentifier where
parseJSON = fmap AppIdentifier . parseJSON
instance ToJSON AppIdentifier where
toJSON = toJSON . unAppIdentifier
instance FromJSONKey AppIdentifier where
fromJSONKey = fmap AppIdentifier fromJSONKey
instance ToJSONKey AppIdentifier where
toJSONKey = contramap unAppIdentifier toJSONKey
instance PersistField AppIdentifier where
toPersistValue = PersistText . show
fromPersistValue (PersistText t) = Right . AppIdentifier $ toS t
fromPersistValue other = Left $ "Invalid AppId: " <> show other
instance PersistFieldSql AppIdentifier where
sqlType _ = SqlString
instance PathPiece AppIdentifier where
fromPathPiece = fmap AppIdentifier . fromPathPiece
toPathPiece = unAppIdentifier
instance ToContent AppIdentifier where
toContent = toContent . toJSON
instance ToTypedContent AppIdentifier where
toTypedContent = toTypedContent . toJSON
instance ToField AppIdentifier where
toField a = toJSONField a
-- Escape $ BS.toStrict $ encode a
-- Plain $ inQuotes $ putStringUtf8 $ show a
-- $ fromByteString $ BS.toStrict $ encode a
instance FromField AppIdentifier where
fromField = fromJSONField
data VersionInfo = VersionInfo
{ versionInfoVersion :: Version
@@ -35,18 +80,6 @@ data VersionInfo = VersionInfo
}
deriving (Eq, Show)
mapSVersionToVersionInfo :: [SVersion] -> [VersionInfo]
mapSVersionToVersionInfo sv = do
(\v -> VersionInfo { versionInfoVersion = sVersionNumber v
, versionInfoReleaseNotes = sVersionReleaseNotes v
, versionInfoDependencies = HM.empty
, versionInfoOsRequired = sVersionOsVersionRequired v
, versionInfoOsRecommended = sVersionOsVersionRecommended v
, versionInfoInstallAlert = Nothing
}
)
<$> sv
instance Ord VersionInfo where
compare = compare `on` versionInfoVersion

View File

@@ -3,11 +3,13 @@
module Lib.Types.Category where
import Startlude
import Database.Persist.Postgresql
import Data.Aeson
import Control.Monad
import Yesod.Core
import Startlude
import Database.Persist.Postgresql
import Data.Aeson
import Control.Monad
import Yesod.Core
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.ToField
data CategoryTitle = FEATURED
| BITCOIN
@@ -46,3 +48,23 @@ instance ToContent CategoryTitle where
toContent = toContent . toJSON
instance ToTypedContent CategoryTitle where
toTypedContent = toTypedContent . toJSON
<<<<<<< HEAD
=======
instance FromField CategoryTitle where
fromField a = fromJSONField a
instance FromField [CategoryTitle] where
fromField a = fromJSONField a
instance ToField [CategoryTitle] where
toField a = toJSONField a
parseCT :: Text -> CategoryTitle
parseCT = \case
"featured" -> FEATURED
"bitcoin" -> BITCOIN
"lightning" -> LIGHTNING
"data" -> DATA
"messaging" -> MESSAGING
"social" -> SOCIAL
"alt coin" -> ALTCOIN
-- _ -> fail "unknown category title"
>>>>>>> aggregate query functions