Skip to content

Commit 3cae62b

Browse files
committed
WIP: working pagination wrong mapping of results
1 parent 010939d commit 3cae62b

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 }
@@ -201,37 +168,6 @@ instance ToParamSchema GovernanceActionType where
201168
& type_ ?~ OpenApiString
202169
& enum_ ?~ map toJSON (enumFromTo minBound maxBound :: [GovernanceActionType])
203170

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

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

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

0 commit comments

Comments
 (0)