Skip to content

Commit

Permalink
[#756] add pagination to drep/list
Browse files Browse the repository at this point in the history
Signed-off-by: jankun4 <[email protected]>
  • Loading branch information
jankun4 committed Apr 22, 2024
1 parent 7d21596 commit ecd4cc9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ changes.

### Added

- added pagination to `drep/list` [Issue 756](https://github.com/IntersectMBO/govtool/issues/756)
- added search query param to the `drep/getVotes` [Issue 640](https://github.com/IntersectMBO/govtool/issues/640)
- added filtering and sorting capabilities to the `drep/list` [Issue 722](https://github.com/IntersectMBO/govtool/issues/722)
- added drepView and txHash to the `ada-holder/get-current-delegation` [Issue 689](https://github.com/IntersectMBO/govtool/issues/689)
Expand Down
26 changes: 22 additions & 4 deletions govtool/backend/src/VVA/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ type VVAApi =
:> QueryParam "search" Text
:> QueryParams "status" DRepStatus
:> QueryParam "sort" DRepSortMode
:> Get '[JSON] [DRep]
:> QueryParam "page" Natural
:> QueryParam "pageSize" Natural
:> Get '[JSON] ListDRepsResponse
:<|> "drep" :> "get-voting-power" :> Capture "drepId" HexText :> Get '[JSON] Integer
:<|> "drep" :> "getVotes"
:> Capture "drepId" HexText
Expand Down Expand Up @@ -120,8 +122,8 @@ delegationToResponse Types.Delegation {..} =
}


drepList :: App m => Maybe Text -> [DRepStatus] -> Maybe DRepSortMode -> m [DRep]
drepList mSearchQuery statuses mSortMode = do
drepList :: App m => Maybe Text -> [DRepStatus] -> Maybe DRepSortMode -> Maybe Natural -> Maybe Natural -> m ListDRepsResponse
drepList mSearchQuery statuses mSortMode mPage mPageSize = do
CacheEnv {dRepListCache} <- asks vvaCache
dreps <- cacheRequest dRepListCache () DRep.listDReps

Expand All @@ -148,7 +150,23 @@ drepList mSearchQuery statuses mSortMode = do
dRepRegistrationStatus


return $ map drepRegistrationToDrep $ sortDReps $ filterDRepsByQuery $ filterDRepsByStatus dreps
let allValidDReps = map drepRegistrationToDrep $ sortDReps $ filterDRepsByQuery $ filterDRepsByStatus dreps


let page = (fromIntegral $ fromMaybe 0 mPage) :: Int
pageSize = (fromIntegral $ fromMaybe 10 mPageSize) :: Int

total = length allValidDReps :: Int

let elements = take pageSize $ drop (page * pageSize) allValidDReps

return $ ListDRepsResponse
{ listDRepsResponsePage = fromIntegral page
, listDRepsResponsePageSize = fromIntegral pageSize
, listDRepsResponseTotal = fromIntegral total
, listDRepsResponseElements = elements
}


getVotingPower :: App m => HexText -> m Integer
getVotingPower (unHexText -> dRepId) = do
Expand Down
36 changes: 36 additions & 0 deletions govtool/backend/src/VVA/API/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,42 @@ instance ToSchema DRep where
& example
?~ toJSON exampleDrep


exampleListDRepsResponse :: Text
exampleListDRepsResponse =
"{ \"page\": 0,"
<> "\"pageSize\": 1,"
<> "\"total\": 1000,"
<> "\"elements\": ["
<> exampleDrep <> "]}"

data ListDRepsResponse
= ListDRepsResponse
{ listDRepsResponsePage :: Integer
, listDRepsResponsePageSize :: Integer
, listDRepsResponseTotal :: Integer
, listDRepsResponseElements :: [DRep]
}
deriving (Generic, Show)

deriveJSON (jsonOptions "listDRepsResponse") ''ListDRepsResponse

instance ToSchema ListDRepsResponse where
declareNamedSchema proxy = do
NamedSchema name_ schema_ <-
genericDeclareNamedSchema
( fromAesonOptions $
jsonOptions "listDRepsResponse"
)
proxy
return $
NamedSchema name_ $
schema_
& description ?~ "ListProposalsResponse"
& example
?~ toJSON exampleListDRepsResponse


data DelegationResponse
= DelegationResponse
{ delegationResponseDRepHash :: Maybe HexText
Expand Down

0 comments on commit ecd4cc9

Please sign in to comment.