Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#756] add pagination to drep/list #813

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading