Skip to content

Commit 2ee0f6f

Browse files
committed
WIP: working pagination wrong mapping of results
1 parent cf20ed9 commit 2ee0f6f

File tree

7 files changed

+312
-236
lines changed

7 files changed

+312
-236
lines changed

Diff for: govtool/backend/sql/list-dreps.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ FilteredDRepData AS (
237237
motivations ILIKE ? OR
238238
qualifications ILIKE ?
239239
)
240-
AND (COALESCE(?, '') = '' OR status IN (?)) -- Status filtering
240+
AND (?::text = '' OR (SELECT COUNT(*) FROM unnest(?)) > 0 AND status IN (SELECT * FROM unnest(?)))
241241
AND (drep_type != 'SoleVoter' OR view ILIKE ?) -- Filter out SoleVoter unless dh.view matches
242242
)
243243
SELECT
244244
(SELECT COUNT(*) FROM FilteredDRepData) AS total,
245-
jsonb_agg(elements) AS elements
245+
COALESCE(jsonb_agg(elements), '[]'::jsonb) AS elements
246246
FROM (
247247
SELECT *
248248
FROM FilteredDRepData

Diff for: govtool/backend/src/VVA/API.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import VVA.Network as Network
4343
import qualified VVA.Proposal as Proposal
4444
import qualified VVA.Transaction as Transaction
4545
import qualified VVA.Types as Types
46+
import VVA.Common.Types
4647
import VVA.Types (App, AppEnv (..),
4748
AppError (CriticalError, InternalError, ValidationError),
4849
CacheEnv (..))
@@ -155,7 +156,7 @@ drepList mSearchQuery statuses mSortMode mPage mPageSize = do
155156
let response = ListDRepsResponse
156157
{ listDRepsResponsePage = fromIntegral page
157158
, listDRepsResponsePageSize = fromIntegral pageSize
158-
, listDRepsResponseTotal = fromIntegral (length cachedDReps) -- Fix total count
159+
, listDRepsResponseTotal = fromIntegral (length cachedDReps)
159160
, listDRepsResponseElements = cachedDReps
160161
}
161162
return response

Diff for: govtool/backend/src/VVA/API/Types.hs

+1-189
Original file line numberDiff line numberDiff line change
@@ -54,40 +54,7 @@ import VVA.API.Utils
5454
import VVA.Config
5555
import qualified VVA.Proposal as Proposal
5656
import VVA.Types (AppError (ValidationError))
57-
58-
newtype HexText
59-
= HexText { unHexText :: Text }
60-
deriving newtype (Eq, Show)
61-
62-
instance FromJSON HexText where
63-
parseJSON (Aeson.String t) = do
64-
if Text.length t `mod` 2 == 1 || Text.any (not . isHexDigit) t
65-
then mzero
66-
else pure $ HexText t
67-
68-
instance ToJSON HexText where
69-
toJSON (HexText t) = Aeson.String t
70-
71-
-- To use it in routes, we need to be able to parse it from Text:
72-
instance FromHttpApiData HexText where
73-
parseUrlPiece txt
74-
| Text.all isHexDigit txt && even (Text.length txt) = Right (HexText txt)
75-
| otherwise = Left "Not a valid hex value"
76-
77-
78-
instance ToParamSchema HexText where
79-
toParamSchema _ = mempty
80-
& type_ ?~ OpenApiString
81-
& format ?~ "hex"
82-
83-
instance ToSchema HexText where
84-
declareNamedSchema _ = do
85-
textSchema <- declareNamedSchema (Proxy :: Proxy Text)
86-
return $ textSchema
87-
& name ?~ "HexText"
88-
& schema . type_ ?~ OpenApiString
89-
& schema . format ?~ "hex"
90-
& schema . example ?~ toJSON (HexText "a1b2c3")
57+
import VVA.Common.Types
9158

9259
newtype AnyValue
9360
= AnyValue { unAnyValue :: Maybe Value }
@@ -204,37 +171,6 @@ instance ToParamSchema GovernanceActionType where
204171
& type_ ?~ OpenApiString
205172
& enum_ ?~ map toJSON (enumFromTo minBound maxBound :: [GovernanceActionType])
206173

207-
208-
data DRepSortMode = Random | VotingPower | RegistrationDate | Status deriving (Bounded, Enum, Eq, Generic, Read, Show)
209-
210-
instance FromJSON DRepSortMode where
211-
parseJSON (Aeson.String dRepSortMode) = pure $ fromJust $ readMaybe (Text.unpack dRepSortMode)
212-
parseJSON _ = fail ""
213-
214-
instance ToJSON DRepSortMode where
215-
toJSON x = Aeson.String $ Text.pack $ show x
216-
217-
instance ToSchema DRepSortMode where
218-
declareNamedSchema proxy = do
219-
NamedSchema name_ schema_ <- genericDeclareNamedSchema (fromAesonOptions defaultOptions) proxy
220-
return $
221-
NamedSchema name_ $
222-
schema_
223-
& description ?~ "DRep Sort Mode"
224-
& example ?~ toJSON VotingPower
225-
226-
instance FromHttpApiData DRepSortMode where
227-
parseQueryParam t = case readMaybe $ Text.unpack t of
228-
Just x -> Right x
229-
Nothing -> Left ("incorrect DRep sort mode: " <> t)
230-
231-
instance ToParamSchema DRepSortMode where
232-
toParamSchema _ =
233-
mempty
234-
& type_ ?~ OpenApiString
235-
& enum_ ?~ map toJSON (enumFromTo minBound maxBound :: [DRepSortMode])
236-
237-
238174
data GovernanceActionSortMode = SoonestToExpire | NewestCreated | MostYesVotes deriving
239175
( Bounded
240176
, Enum
@@ -673,115 +609,6 @@ instance ToSchema GetTransactionStatusResponse where
673609
& example
674610
?~ toJSON exampleGetTransactionStatusResponse
675611

676-
newtype DRepHash
677-
= DRepHash Text
678-
deriving (Generic, Show)
679-
680-
instance FromJSON DRepHash where
681-
parseJSON (Aeson.String s) = pure $ DRepHash s
682-
parseJSON x = fail ("expected DRepHash to be a string but got: " <> Char8.unpack (encode x))
683-
684-
instance ToJSON DRepHash where
685-
toJSON (DRepHash raw) = toJSON raw
686-
687-
688-
exampleDrepHash :: Text
689-
exampleDrepHash = "b4e4184bfedf920fec53cdc327de4da661ae427784c0ccca9e3c2f50"
690-
691-
instance ToSchema DRepHash where
692-
declareNamedSchema _ = pure $ NamedSchema (Just "DRepHash") $ mempty
693-
& type_ ?~ OpenApiObject
694-
& description ?~ "Hash of a DRep"
695-
& example
696-
?~ toJSON exampleDrepHash
697-
698-
699-
data DRepStatus = Active | Inactive | Retired deriving (Bounded, Enum, Eq, Generic, Ord, Read, Show)
700-
701-
-- ToJSON instance for DRepStatus
702-
instance ToJSON DRepStatus where
703-
toJSON Retired = "Retired"
704-
toJSON Active = "Active"
705-
toJSON Inactive = "Inactive"
706-
707-
-- FromJSON instance for DRepStatus
708-
instance FromJSON DRepStatus where
709-
parseJSON = withText "DRepStatus" $ \case
710-
"Retired" -> pure Retired
711-
"Active" -> pure Active
712-
"Inactive" -> pure Inactive
713-
_ -> fail "Invalid DRepStatus"
714-
715-
-- ToSchema instance for DRepStatus
716-
instance ToSchema DRepStatus where
717-
declareNamedSchema _ = pure $ NamedSchema (Just "DRepStatus") $ mempty
718-
& type_ ?~ OpenApiString
719-
& description ?~ "DRep Status"
720-
& enum_ ?~ map toJSON [Retired, Active, Inactive]
721-
722-
instance FromHttpApiData DRepStatus where
723-
parseQueryParam t = case readMaybe $ Text.unpack t of
724-
Just x -> Right x
725-
Nothing -> Left ("incorrect DRep status " <> t)
726-
727-
instance ToParamSchema DRepStatus where
728-
toParamSchema _ =
729-
mempty
730-
& type_ ?~ OpenApiString
731-
& enum_ ?~ map toJSON (enumFromTo minBound maxBound :: [DRepStatus])
732-
733-
data DRepType = NormalDRep | SoleVoter
734-
735-
instance Show DRepType where
736-
show NormalDRep = "DRep"
737-
show SoleVoter = "SoleVoter"
738-
739-
-- ToJSON instance for DRepType
740-
instance ToJSON DRepType where
741-
toJSON NormalDRep = "DRep"
742-
toJSON SoleVoter = "SoleVoter"
743-
744-
-- FromJSON instance for DRepType
745-
instance FromJSON DRepType where
746-
parseJSON = withText "DRepType" $ \case
747-
"DRep" -> pure NormalDRep
748-
"SoleVoter" -> pure SoleVoter
749-
_ -> fail "Invalid DRepType"
750-
751-
-- ToSchema instance for DRepType
752-
instance ToSchema DRepType where
753-
declareNamedSchema _ = pure $ NamedSchema (Just "DRepType") $ mempty
754-
& type_ ?~ OpenApiString
755-
& description ?~ "DRep Type"
756-
& enum_ ?~ map toJSON [NormalDRep, SoleVoter]
757-
758-
data DRep
759-
= DRep
760-
{ dRepIsScriptBased :: Bool
761-
, dRepDrepId :: DRepHash
762-
, dRepView :: Text
763-
, dRepUrl :: Maybe Text
764-
, dRepMetadataHash :: Maybe Text
765-
, dRepDeposit :: Integer
766-
, dRepVotingPower :: Maybe Integer
767-
, dRepStatus :: DRepStatus
768-
, dRepType :: DRepType
769-
, dRepLatestTxHash :: Maybe HexText
770-
, dRepLatestRegistrationDate :: UTCTime
771-
, dRepMetadataError :: Maybe Text
772-
, dRepPaymentAddress :: Maybe Text
773-
, dRepGivenName :: Maybe Text
774-
, dRepObjectives :: Maybe Text
775-
, dRepMotivations :: Maybe Text
776-
, dRepQualifications :: Maybe Text
777-
, dRepImageUrl :: Maybe Text
778-
, dRepImageHash :: Maybe HexText
779-
}
780-
deriving (Generic, Show)
781-
782-
783-
deriveJSON (jsonOptions "dRep") ''DRep
784-
785612
exampleDrep :: Text
786613
exampleDrep =
787614
"{\"drepId\": \"d3a62ffe9c214e1a6a9809f7ab2a104c117f85e1f171f8f839d94be5\","
@@ -803,21 +630,6 @@ exampleDrep =
803630
<> "\"imageUrl\": \"https://image.url\","
804631
<> "\"imageHash\": \"9198b1b204273ba5c67a13310b5a806034160f6a063768297e161d9b759cad61\"}"
805632

806-
-- ToSchema instance for DRep
807-
instance ToSchema DRep where
808-
declareNamedSchema proxy = do
809-
NamedSchema name_ schema_ <-
810-
genericDeclareNamedSchema
811-
( fromAesonOptions $ jsonOptions "dRep" )
812-
proxy
813-
return $
814-
NamedSchema name_ $
815-
schema_
816-
& description ?~ "DRep"
817-
& example
818-
?~ toJSON exampleDrep
819-
820-
821633
exampleListDRepsResponse :: Text
822634
exampleListDRepsResponse =
823635
"{ \"page\": 0,"

0 commit comments

Comments
 (0)