Skip to content

Commit

Permalink
feat: add panel get activity logs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
duruer committed Feb 9, 2025
1 parent c62ba68 commit 39059e7
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,24 @@ abstract class PanelActivityLogDao : Dao<PanelActivityLog>(PanelActivityLog::cla
panelActivityLog: PanelActivityLog,
sqlClient: SqlClient
): Long

abstract suspend fun byUserId(
userId: Long,
page: Long,
sqlClient: SqlClient
): List<PanelActivityLog>

abstract suspend fun getAll(
page: Long,
sqlClient: SqlClient
): List<PanelActivityLog>

abstract suspend fun count(
userId: Long,
sqlClient: SqlClient
): Long

abstract suspend fun count(
sqlClient: SqlClient
): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,65 @@ class PanelActivityLogDaoImpl : PanelActivityLogDao() {

return rows.property(MySQLClient.LAST_INSERTED_ID)
}

override suspend fun byUserId(
userId: Long,
page: Long,
sqlClient: SqlClient
): List<PanelActivityLog> {
val query =
"SELECT ${fields.toTableQuery()} FROM `${getTablePrefix() + tableName}` WHERE `userId` = ? ORDER BY `createdAt` DESC, `id` DESC LIMIT 10 ${if (page == 1L) "" else "OFFSET ${(page - 1) * 10}"}"

val rows: RowSet<Row> = sqlClient
.preparedQuery(query)
.execute(
Tuple.of(
userId
)
).coAwait()

return rows.toEntities()
}

override suspend fun getAll(
page: Long,
sqlClient: SqlClient
): List<PanelActivityLog> {
val query =
"SELECT ${fields.toTableQuery()} FROM `${getTablePrefix() + tableName}` ORDER BY `createdAt` DESC, `id` DESC LIMIT 10 ${if (page == 1L) "" else "OFFSET ${(page - 1) * 10}"}"

val rows: RowSet<Row> = sqlClient
.preparedQuery(query)
.execute()
.coAwait()

return rows.toEntities()
}

override suspend fun count(
userId: Long,
sqlClient: SqlClient
): Long {
val query = "SELECT COUNT(*) FROM `${getTablePrefix() + tableName}` WHERE `userId` = ?"

val rows: RowSet<Row> = sqlClient
.preparedQuery(query)
.execute(Tuple.of(userId))
.coAwait()

return rows.toList()[0].getLong(0)
}

override suspend fun count(
sqlClient: SqlClient
): Long {
val query = "SELECT COUNT(*) FROM `${getTablePrefix() + tableName}`"

val rows: RowSet<Row> = sqlClient
.preparedQuery(query)
.execute()
.coAwait()

return rows.toList()[0].getLong(0)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.panomc.platform.route.api.panel

import com.panomc.platform.annotation.Endpoint
import com.panomc.platform.auth.AuthProvider
import com.panomc.platform.auth.panel.permission.AccessActivityLogsPermission
import com.panomc.platform.db.DatabaseManager
import com.panomc.platform.error.PageNotFound
import com.panomc.platform.model.*
import io.vertx.ext.web.RoutingContext
import io.vertx.ext.web.validation.ValidationHandler
import io.vertx.ext.web.validation.builder.Parameters.optionalParam
import io.vertx.ext.web.validation.builder.ValidationHandlerBuilder
import io.vertx.json.schema.SchemaParser
import io.vertx.json.schema.common.dsl.Schemas.intSchema

@Endpoint
class PanelGetActivityLogsAPI(
private val databaseManager: DatabaseManager,
private val authProvider: AuthProvider,
) : PanelApi() {
override val paths = listOf(Path("/api/panel/logs/activity", RouteType.GET))

override fun getValidationHandler(schemaParser: SchemaParser): ValidationHandler =
ValidationHandlerBuilder.create(schemaParser)
.queryParameter(optionalParam("page", intSchema()))
.build()

override suspend fun handle(context: RoutingContext): Result {
val parameters = getParameters(context)

val page = parameters.queryParameter("page")?.long ?: 1

val userId = authProvider.getUserIdFromRoutingContext(context)

val hasPermission = authProvider.hasPermission(userId, AccessActivityLogsPermission(), context)

val sqlClient = getSqlClient()

val totalCount = if (hasPermission)
databaseManager.panelActivityLogDao.count(sqlClient)
else
databaseManager.panelActivityLogDao.count(userId, sqlClient)

var totalPage = kotlin.math.ceil(totalCount.toDouble() / 10).toLong()

if (totalPage < 1) {
totalPage = 1
}

if (page > totalPage || page < 1) {
throw PageNotFound()
}

val platforms = if (hasPermission)
databaseManager.panelActivityLogDao.getAll(page, sqlClient)
else
databaseManager.panelActivityLogDao.byUserId(userId, page, sqlClient)

val response = mutableMapOf(
"data" to platforms,
"meta" to mapOf(
"filteredCount" to totalCount,
"totalCount" to totalCount,
"totalPage" to totalPage
)
)

return Successful(
response,
)
}
}

0 comments on commit 39059e7

Please sign in to comment.