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

Small backend fixes #845

Merged
merged 3 commits into from
Mar 3, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import app.ehrenamtskarte.backend.common.webservice.UnauthorizedException
import app.ehrenamtskarte.backend.projects.database.ProjectEntity
import app.ehrenamtskarte.backend.projects.database.Projects
import app.ehrenamtskarte.backend.regions.database.RegionEntity
import app.ehrenamtskarte.backend.regions.database.Regions
import com.expediagroup.graphql.generator.annotations.GraphQLDescription
import graphql.schema.DataFetchingEnvironment
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.not
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction

@Suppress("unused")
Expand All @@ -36,7 +39,7 @@ class ViewAdministratorsQueryService {
@GraphQLDescription("Returns all administrators in a project. This query requires the role PROJECT_ADMIN.")
fun getUsersInProject(
project: String,
dfe: DataFetchingEnvironment
dfe: DataFetchingEnvironment,
): List<Administrator> {
val context = dfe.getContext<GraphQLContext>()
val jwtPayload = context.enforceSignedIn()
Expand All @@ -47,17 +50,19 @@ class ViewAdministratorsQueryService {
if (!Authorizer.mayViewUsersInProject(admin, projectId)) {
throw UnauthorizedException()
}
val administrators =
AdministratorEntity.find { Administrators.projectId eq projectId and not(Administrators.deleted) }

val administrators = (Administrators innerJoin Regions)
.slice(Administrators.columns)
.select { Administrators.projectId eq projectId and not(Administrators.deleted) }
.orderBy(Regions.name to SortOrder.ASC, Administrators.email to SortOrder.ASC)
.let { AdministratorEntity.wrapRows(it) }
administrators.map { Administrator.fromDbEntity(it) }
}
}

@GraphQLDescription("Returns all administrators in a region. This query requires the role REGION_ADMIN or PROJECT_ADMIN.")
fun getUsersInRegion(
regionId: Int,
dfe: DataFetchingEnvironment
dfe: DataFetchingEnvironment,
): List<Administrator> {
val context = dfe.getContext<GraphQLContext>()
val jwtPayload = context.enforceSignedIn()
Expand All @@ -68,8 +73,9 @@ class ViewAdministratorsQueryService {
if (!Authorizer.mayViewUsersInRegion(admin, region)) {
throw UnauthorizedException()
}
val administrators =
AdministratorEntity.find { Administrators.regionId eq regionId and not(Administrators.deleted) }
val administrators = AdministratorEntity
.find { Administrators.regionId eq regionId and not(Administrators.deleted) }
.orderBy(Administrators.email to SortOrder.ASC)

administrators.map { Administrator.fromDbEntity(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import app.ehrenamtskarte.backend.auth.database.repos.AdministratorsRepository
import app.ehrenamtskarte.backend.auth.webservice.schema.types.Role
import app.ehrenamtskarte.backend.config.BackendConfiguration
import org.jetbrains.exposed.sql.Database.Companion.connect
import org.jetbrains.exposed.sql.DatabaseConfig
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.transactions.TransactionManager
import org.jetbrains.exposed.sql.transactions.transaction
import java.io.BufferedReader
Expand Down Expand Up @@ -38,7 +38,7 @@ class Database {
email: String,
password: String,
roleDbValue: String,
projectId: Int? = null
projectId: Int? = null,
) {
val role = Role.fromDbValue(roleDbValue)
transaction {
Expand All @@ -51,14 +51,16 @@ class Database {
config.postgres.url,
driver = "org.postgresql.Driver",
user = config.postgres.user,
password = config.postgres.password
password = config.postgres.password,
databaseConfig = if (config.production) {
null
} else {
DatabaseConfig.invoke {
this.sqlLogger = StdOutSqlLogger
}
},
)

transaction {
if (!config.production) {
addLogger(StdOutSqlLogger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is removed, how are queries logged?

Copy link
Member Author

@michael-markl michael-markl Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed logging for this specific transaction, but added logging for all transactions by specifying the DatabaseConfig

}

setupDatabaseForProjects(config)
setupDatabaseForRegions()
setupDatabaseForStores(Companion::executeScript)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ class WebService {
it.hostedPath = "/graphiql"
it.location = Location.CLASSPATH
}
}.start(host, port)

println("Server is running at http://$host:$port")
println("Goto http://$host:$port/graphiql/ for a graphical editor")
}

val graphQLHandler = GraphQLHandler(config)
val mapStyleHandler = MapStyleHandler(config)
Expand All @@ -73,5 +70,9 @@ class WebService {
app.get(applicationHandler.getPath()) { ctx ->
applicationHandler.handle(ctx)
}

app.start(host, port)
println("Server is running at http://$host:$port")
println("Goto http://$host:$port/graphiql/ for a graphical editor")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app.ehrenamtskarte.backend.common.database.sortByKeys
import app.ehrenamtskarte.backend.projects.database.Projects
import app.ehrenamtskarte.backend.regions.database.RegionEntity
import app.ehrenamtskarte.backend.regions.database.Regions
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select

Expand All @@ -13,6 +14,7 @@ object RegionsRepository {
val query = (Projects innerJoin Regions)
.slice(Regions.columns)
.select { Projects.project eq project }
.orderBy(Regions.name to SortOrder.ASC)
return RegionEntity.wrapRows(query).toList()
}

Expand Down