Skip to content

Commit

Permalink
feat: add first integration with neo4j
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenSrcerer committed Feb 12, 2024
1 parent 000e919 commit 587246c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ dependencies {
// implementation("io.quarkus:quarkus-hibernate-orm-panache")
// implementation("io.quarkus:quarkus-reactive-pg-client")

implementation("org.neo4j:neo4j-ogm-quarkus:3.6.0")
implementation("io.quarkiverse.neo4j:quarkus-neo4j:3.6.0")

implementation("io.quarkus:quarkus-resteasy-reactive-jackson")
implementation("io.quarkus:quarkus-resteasy-reactive")
implementation("io.quarkus:quarkus-config-yaml")
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/online/danielstefani/paddy/db/device/Device.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package online.danielstefani.paddy.db.device

import org.neo4j.ogm.annotation.Id
import org.neo4j.ogm.annotation.NodeEntity

@NodeEntity
class Device(
@Id val serial: String,
var jwt: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package online.danielstefani.paddy.db.device

import jakarta.enterprise.context.ApplicationScoped
import jakarta.inject.Inject
import org.neo4j.ogm.session.SessionFactory
import org.neo4j.ogm.session.queryForObject

@ApplicationScoped
class DeviceRepository {

@Inject
private lateinit var sessionFactory: SessionFactory

fun createDevice(serial: String, jwt: String) {
with(sessionFactory.openSession()) {
val existingDevice = this.queryForObject<Device>(
"MATCH (deviceNode:Device {serial: $serial}) RETURN deviceNode",
emptyMap()
)

if (existingDevice != null)
this.save(existingDevice.also { it.jwt = jwt })
else
this.save(Device(serial, jwt))
}
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package online.danielstefani.paddy.security

import jakarta.ws.rs.GET
import jakarta.ws.rs.POST
import jakarta.ws.rs.Path
import jakarta.ws.rs.Produces
import jakarta.ws.rs.core.MediaType
import online.danielstefani.paddy.db.device.DeviceRepository
import org.jboss.resteasy.reactive.RestPath
import java.util.*


@Path("/")
class JwksController(
private val jwtService: JwtService
private val jwtService: JwtService,
private val deviceRepository: DeviceRepository
) {

@Produces(MediaType.APPLICATION_JSON)
Expand All @@ -23,4 +28,18 @@ class JwksController(
fun getJwt(): String {
return jwtService.makeJwt()
}

@Path("/create-device/{serial}")
@POST
fun createDevice(@RestPath serial: String?): Map<String, String> {
val jwt = jwtService.makeJwt()
val deviceSerial = if (serial.isNullOrEmpty()) UUID.randomUUID().toString() else serial

deviceRepository.createDevice(deviceSerial, jwt)

return mapOf(
Pair("serial", deviceSerial),
Pair("jwt", jwt)
)
}
}

0 comments on commit 587246c

Please sign in to comment.