def query[A]()

in api/src/main/scala/com/gu/adapters/store/Store.scala [69:111]


  def query[A](
    table: String,
    index: String,
    key: String,
    value: A,
    since: Option[DateTime] = None,
    until: Option[DateTime] = None,
    order: Option[OrderBy] = Some(Descending)
  ): Either[Error, QueryResponse] = {

    val spec = new QuerySpec()
      .withHashKey(key, value)
      .withMaxResultSize(props.pageSize)

    val hasBefore = until.isDefined
    val oldestFirst = order.contains(Ascending)

    if (hasBefore && oldestFirst) {
      until.foreach(t => spec.withRangeKeyCondition(new RangeKeyCondition("LastModified").lt(ISODateFormatter.print(t))))
      spec.withScanIndexForward(false)
    } else if (!hasBefore && oldestFirst) {
      since.foreach(t => spec.withRangeKeyCondition(new RangeKeyCondition("LastModified").gt(ISODateFormatter.print(t))))
      spec.withScanIndexForward(true)
    } else if (hasBefore) {
      until.foreach(t => spec.withRangeKeyCondition(new RangeKeyCondition("LastModified").gt(ISODateFormatter.print(t))))
      spec.withScanIndexForward(true)
    } else if (!hasBefore) {
      since.foreach(t => spec.withRangeKeyCondition(new RangeKeyCondition("LastModified").lt(ISODateFormatter.print(t))))
      spec.withScanIndexForward(false)
    }

    val result = handleIoErrors(db.getTable(table).getIndex(index).query(spec))

    for {
      pages <- result.map(_.pages.asScala.toList)
      qr <- result.map(_.getLastLowLevelResult.getQueryResult)
      items = pages.flatMap(_.asScala)
      avatars = items.map(item => asAvatar(item)).flatMap(_.toOption)
    } yield {
      val orderedAvatars = until.map(_ => avatars.reverse).getOrElse(avatars)
      QueryResponse(orderedAvatars, qr.getLastEvaluatedKey != null)
    }
  }