mirror of
https://github.com/VinylDNS/vinyldns
synced 2025-09-04 00:05:12 +00:00
fix pagination
This commit is contained in:
@@ -113,7 +113,7 @@ class MySqlGroupChangeRepositoryIntegrationSpec
|
||||
|
||||
val listResponse = repo.getGroupChanges(groupId, None, 100).unsafeRunSync()
|
||||
listResponse.changes shouldBe expectedChanges
|
||||
listResponse.lastEvaluatedTimeStamp shouldBe None
|
||||
listResponse.nextId shouldBe None
|
||||
}
|
||||
|
||||
"get group changes properly using a maxItems of 1" in {
|
||||
@@ -130,9 +130,7 @@ class MySqlGroupChangeRepositoryIntegrationSpec
|
||||
val listResponse =
|
||||
repo.getGroupChanges(groupId, startFrom = None, maxItems = 1).unsafeRunSync()
|
||||
listResponse.changes shouldBe expectedChanges
|
||||
listResponse.lastEvaluatedTimeStamp shouldBe Some(
|
||||
expectedChanges.head.created.toEpochMilli.toString
|
||||
)
|
||||
listResponse.nextId shouldBe Some(1)
|
||||
}
|
||||
|
||||
"page group changes using a startFrom and maxItems" in {
|
||||
@@ -145,33 +143,33 @@ class MySqlGroupChangeRepositoryIntegrationSpec
|
||||
.reverse
|
||||
|
||||
val expectedPageOne = Seq(changesSorted(0))
|
||||
val expectedPageOneNext = Some(changesSorted(0).created.toEpochMilli.toString)
|
||||
val expectedPageOneNext = Some(1)
|
||||
val expectedPageTwo = Seq(changesSorted(1))
|
||||
val expectedPageTwoNext = Some(changesSorted(1).created.toEpochMilli.toString)
|
||||
val expectedPageTwoNext = Some(2)
|
||||
val expectedPageThree = Seq(changesSorted(2))
|
||||
val expectedPageThreeNext = Some(changesSorted(2).created.toEpochMilli.toString)
|
||||
val expectedPageThreeNext = Some(3)
|
||||
|
||||
// get first page
|
||||
val pageOne =
|
||||
repo.getGroupChanges(groupId, startFrom = None, maxItems = 1).unsafeRunSync()
|
||||
pageOne.changes shouldBe expectedPageOne
|
||||
pageOne.lastEvaluatedTimeStamp shouldBe expectedPageOneNext
|
||||
pageOne.nextId shouldBe expectedPageOneNext
|
||||
|
||||
// get second page
|
||||
val pageTwo =
|
||||
repo
|
||||
.getGroupChanges(groupId, startFrom = pageOne.lastEvaluatedTimeStamp, maxItems = 1)
|
||||
.getGroupChanges(groupId, startFrom = pageOne.nextId, maxItems = 1)
|
||||
.unsafeRunSync()
|
||||
pageTwo.changes shouldBe expectedPageTwo
|
||||
pageTwo.lastEvaluatedTimeStamp shouldBe expectedPageTwoNext
|
||||
pageTwo.nextId shouldBe expectedPageTwoNext
|
||||
|
||||
// get final page
|
||||
val pageThree =
|
||||
repo
|
||||
.getGroupChanges(groupId, startFrom = pageTwo.lastEvaluatedTimeStamp, maxItems = 1)
|
||||
.getGroupChanges(groupId, startFrom = pageTwo.nextId, maxItems = 1)
|
||||
.unsafeRunSync()
|
||||
pageThree.changes shouldBe expectedPageThree
|
||||
pageThree.lastEvaluatedTimeStamp shouldBe expectedPageThreeNext
|
||||
pageThree.nextId shouldBe expectedPageThreeNext
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -47,9 +47,9 @@ class MySqlGroupChangeRepository extends GroupChangeRepository with Monitored {
|
||||
sql"""
|
||||
|SELECT data
|
||||
| FROM group_change
|
||||
| WHERE group_id = {groupId} AND created_timestamp < {startFrom}
|
||||
| WHERE group_id = {groupId}
|
||||
| ORDER BY created_timestamp DESC
|
||||
| LIMIT {maxItems}
|
||||
| LIMIT {maxItems} OFFSET {startFrom}
|
||||
""".stripMargin
|
||||
|
||||
private final val LIST_GROUP_CHANGE_NO_START =
|
||||
@@ -100,7 +100,7 @@ class MySqlGroupChangeRepository extends GroupChangeRepository with Monitored {
|
||||
|
||||
def getGroupChanges(
|
||||
groupId: String,
|
||||
startFrom: Option[String],
|
||||
startFrom: Option[Int],
|
||||
maxItems: Int
|
||||
): IO[ListGroupChangesResults] =
|
||||
monitor("repo.GroupChange.getGroupChanges") {
|
||||
@@ -112,21 +112,25 @@ class MySqlGroupChangeRepository extends GroupChangeRepository with Monitored {
|
||||
val query = startFrom match {
|
||||
case Some(start) =>
|
||||
LIST_GROUP_CHANGES_WITH_START
|
||||
.bindByName('groupId -> groupId, 'startFrom -> start, 'maxItems -> maxItems)
|
||||
.bindByName('groupId -> groupId, 'startFrom -> start, 'maxItems -> (maxItems + 1))
|
||||
case None =>
|
||||
LIST_GROUP_CHANGE_NO_START
|
||||
.bindByName('groupId -> groupId, 'maxItems -> maxItems)
|
||||
.bindByName('groupId -> groupId, 'maxItems -> (maxItems + 1))
|
||||
}
|
||||
val queryResult = query
|
||||
.map(toGroupChange(1))
|
||||
.list()
|
||||
.apply()
|
||||
|
||||
val nextId =
|
||||
if (queryResult.size < maxItems) None
|
||||
else queryResult.lastOption.map(_.created.toEpochMilli.toString)
|
||||
val maxQueries = queryResult.take(maxItems)
|
||||
val startValue = startFrom.getOrElse(0)
|
||||
|
||||
ListGroupChangesResults(queryResult, nextId)
|
||||
val nextId = queryResult match {
|
||||
case _ if queryResult.size <= maxItems | queryResult.isEmpty => None
|
||||
case _ => Some(startValue + maxItems)
|
||||
}
|
||||
|
||||
ListGroupChangesResults(maxQueries, nextId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user