Skip to content

Commit

Permalink
Rework post UI
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinovega committed Dec 4, 2024
1 parent c0e68ed commit 616d1cd
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 95 deletions.
23 changes: 9 additions & 14 deletions daikoku/app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ import org.apache.pekko.http.scaladsl.util.FastFuture
import org.apache.pekko.stream.Materializer
import org.apache.pekko.stream.scaladsl.{Flow, JsonFraming, Sink, Source}
import org.apache.pekko.util.ByteString
import cats.Id
import cats.data.EitherT
import cats.implicits.{catsSyntaxOptionId, toTraverseOps}
import controllers.AppError
import controllers.AppError._
import fr.maif.otoroshi.daikoku.actions.{
DaikokuAction,
DaikokuActionContext,
DaikokuActionMaybeWithGuest,
DaikokuActionMaybeWithoutUser
}
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionContext, DaikokuActionMaybeWithGuest, DaikokuActionMaybeWithoutUser}
import fr.maif.otoroshi.daikoku.audit.AuditTrailEvent
import fr.maif.otoroshi.daikoku.audit.config.ElasticAnalyticsConfig
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async._
import fr.maif.otoroshi.daikoku.domain.NotificationAction.{
ApiAccess,
ApiSubscriptionDemand
}
import fr.maif.otoroshi.daikoku.domain.NotificationAction.{ApiAccess, ApiSubscriptionDemand}
import fr.maif.otoroshi.daikoku.domain.UsagePlanVisibility.Private
import fr.maif.otoroshi.daikoku.domain._
import fr.maif.otoroshi.daikoku.domain.json._
Expand All @@ -41,6 +31,7 @@ import play.api.i18n.I18nSupport
import play.api.libs.json._
import play.api.libs.streams.Accumulator
import play.api.mvc._
import storage.{Desc}

import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}
Expand Down Expand Up @@ -3580,13 +3571,17 @@ class ApiController(
)
),
offset,
limit
limit,
Json.obj("lastModificationAt" -> 1).some,
Desc.some
)
.map(data =>
Right(
Json.obj(
"posts" -> JsArray(data._1.map(_.asJson)),
"total" -> data._2
"total" -> data._2,
"nextCursor" -> (if ((offset + limit) < data._2) offset + limit else JsNull),
"prevCursor" -> (if (offset < limit) JsNull else offset - limit )
)
)
)
Expand Down
19 changes: 18 additions & 1 deletion daikoku/app/domain/SchemaDefinition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,22 @@ object SchemaDefinition {
)
)
)
lazy val ApiSubscriptionTransferSuccessType = new PossibleObject(
ObjectType(
"ApiSubscriptionTransferSuccess",
"A notification triggered when a checkout session is available",
interfaces[
(DataStore, DaikokuActionContext[JsValue]),
ApiSubscriptionTransferSuccess
](NotificationActionType),
fields[
(DataStore, DaikokuActionContext[JsValue]),
ApiSubscriptionTransferSuccess
](
Field("subscription", StringType, resolve = _.value.subscription.value)
)
)
)

lazy val NotificationInterfaceType: ObjectType[
(DataStore, DaikokuActionContext[JsValue]),
Expand Down Expand Up @@ -2579,7 +2595,8 @@ object SchemaDefinition {
TransferApiOwnershipType,
ApiSubscriptionRejectType,
ApiSubscriptionAcceptType,
CheckoutForSubscriptionType
CheckoutForSubscriptionType,
ApiSubscriptionTransferSuccessType
)
)
)
Expand Down
15 changes: 14 additions & 1 deletion daikoku/app/storage/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import play.api.libs.json._

import scala.concurrent.{ExecutionContext, Future}

sealed trait SortingOrder {
def name: String
}

case object Desc extends SortingOrder {
def name: String = "DESC"
}
case object Asc extends SortingOrder {
def name: String = "ASC"
}


trait TenantCapableRepo[Of, Id <: ValueType] {
def forTenant(tenant: Tenant): Repo[Of, Id] = forTenant(tenant.id)

Expand Down Expand Up @@ -75,7 +87,8 @@ trait Repo[Of, Id <: ValueType] {
query: JsObject,
page: Int,
pageSize: Int,
sort: Option[JsObject] = None
sort: Option[JsObject] = None,
order: Option[SortingOrder] = None
)(implicit
ec: ExecutionContext
): Future[(Seq[Of], Long)]
Expand Down
18 changes: 11 additions & 7 deletions daikoku/app/storage/drivers/postgres/PostgresDataStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1693,9 +1693,10 @@ abstract class PostgresRepo[Of, Id <: ValueType](
query: JsObject,
page: Int,
pageSize: Int,
sort: Option[JsObject] = None
sort: Option[JsObject] = None,
order: Option[SortingOrder] = None
)(implicit ec: ExecutionContext): Future[(Seq[Of], Long)] =
super.findWithPagination(query, page, pageSize, sort)
super.findWithPagination(query, page, pageSize, sort, order)
}

abstract class PostgresTenantAwareRepo[Of, Id <: ValueType](
Expand Down Expand Up @@ -1930,13 +1931,15 @@ abstract class PostgresTenantAwareRepo[Of, Id <: ValueType](
query: JsObject,
page: Int,
pageSize: Int,
sort: Option[JsObject] = None
sort: Option[JsObject] = None,
order: Option[SortingOrder] = None
)(implicit ec: ExecutionContext): Future[(Seq[Of], Long)] =
super.findWithPagination(
query ++ Json.obj("_tenant" -> tenant.value),
page,
pageSize,
sort
sort,
order
)
}

Expand Down Expand Up @@ -2264,7 +2267,8 @@ abstract class CommonRepo[Of, Id <: ValueType](env: Env, reactivePg: ReactivePg)
query: JsObject,
page: Int,
pageSize: Int,
sort: Option[JsObject] = None
sort: Option[JsObject] = None,
order : Option[SortingOrder] = None
)(implicit
ec: ExecutionContext
): Future[(Seq[Of], Long)] = {
Expand Down Expand Up @@ -2308,7 +2312,7 @@ abstract class CommonRepo[Of, Id <: ValueType](env: Env, reactivePg: ReactivePg)

if (query.values.isEmpty)
reactivePg.querySeq(
s"SELECT * FROM $tableName ORDER BY ${sortedKeys.mkString(",")} ASC LIMIT $$1 OFFSET $$2",
s"SELECT * FROM $tableName ORDER BY ${sortedKeys.mkString(",")} ${order.map(_.name).getOrElse(Asc.name)} LIMIT $$1 OFFSET $$2",
Seq(Integer.valueOf(pageSize), Integer.valueOf(page * pageSize))
) { row =>
rowToJson(row, format)
Expand All @@ -2317,7 +2321,7 @@ abstract class CommonRepo[Of, Id <: ValueType](env: Env, reactivePg: ReactivePg)
val (sql, params) = convertQuery(query)
reactivePg.querySeq(
s"SELECT * FROM $tableName WHERE $sql ORDER BY ${sortedKeys
.mkString(",")} ASC ${if (pageSize > 0)
.mkString(",")} ${order.map(_.name).getOrElse(Asc.name)} ${if (pageSize > 0)
s"LIMIT ${Integer.valueOf(pageSize)}"
else ""} OFFSET ${Integer.valueOf(page * pageSize)}",
params.map {
Expand Down
Loading

0 comments on commit 616d1cd

Please sign in to comment.