Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from javaBin/bruker-registrering
Browse files Browse the repository at this point in the history
Bruker registrering
  • Loading branch information
daudmohamed authored Apr 6, 2024
2 parents 82b4024 + 4d09fe6 commit 54011cc
Show file tree
Hide file tree
Showing 31 changed files with 163 additions and 179 deletions.
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

18 changes: 0 additions & 18 deletions .idea/gradle.xml

This file was deleted.

20 changes: 0 additions & 20 deletions .idea/jarRepositories.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/kotlinc.xml

This file was deleted.

13 changes: 0 additions & 13 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

9 changes: 0 additions & 9 deletions .idea/workshop-wizard.iml

This file was deleted.

8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ To start the application: Go to the backend folder and `gradle app:run` or run i

### Frontend
TBD


### TODO
- [ ] Finish workshop endpoint
- [ ] Add and finish user endpoint
- [ ] Add and Finish registration endpoint
- [ ] Add and Finish cancellation endpoint
- [ ] Finish workshop wizard
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

# Ignore Gradle build output directory
build

.idea/
8 changes: 0 additions & 8 deletions backend/.idea/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions backend/.idea/compiler.xml

This file was deleted.

17 changes: 0 additions & 17 deletions backend/.idea/gradle.xml

This file was deleted.

20 changes: 0 additions & 20 deletions backend/.idea/jarRepositories.xml

This file was deleted.

6 changes: 0 additions & 6 deletions backend/.idea/kotlinc.xml

This file was deleted.

10 changes: 0 additions & 10 deletions backend/.idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions backend/.idea/vcs.xml

This file was deleted.

5 changes: 5 additions & 0 deletions backend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
val postgres_version = "42.7.1"
val exposed_version = "0.41.1"
val h2_version = "2.1.214"
val kotlinx_datetime_version = "0.2.1"

// Use the Kotlin JUnit 5 integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
Expand Down Expand Up @@ -82,13 +83,17 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
implementation("com.github.seratch:kotliquery:1.9.0")

// Datetime
implementation("org.jetbrains.kotlinx:kotlinx-datetime:$kotlinx_datetime_version")


// Database
// https://mvnrepository.com/artifact/org.flywaydb/flyway-core - database migrations
implementation("org.flywaydb:flyway-core:$flyway_version")
implementation("org.flywaydb:flyway-database-postgresql:$flyway_version")
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
implementation("org.jetbrains.exposed:exposed-java-time:$exposed_version")
implementation("com.h2database:h2:$h2_version")
// https://mvnrepository.com/artifact/com.zaxxer/HikariCP - connection pooling
implementation("com.zaxxer:HikariCP:$hikari_version")
Expand Down
11 changes: 11 additions & 0 deletions backend/app/src/main/kotlin/backend/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import backend.route.adminRoutes
import backend.route.userRoutes
import backend.repository.AdminRepository
import backend.repository.UserRepository
import backend.repository.WorkshopRepository
import backend.route.workshopRoute
import com.inventy.plugins.DatabaseFactory
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
Expand All @@ -33,15 +36,23 @@ fun Application.module() {
fun Application.configureRouting() {
val userRepository = UserRepository()
val adminRepository = AdminRepository()
val workshopRepository = WorkshopRepository()

install(ContentNegotiation) {
json()
}
routing {
userRoutes(userRepository)
adminRoutes(adminRepository, userRepository)
workshopRoute(workshopRepository)
healthz()

authenticate ("auth0-user") {
get("/auth") {
call.respondText("Hello, world!")
}
}

get {
call.respondText("Hello, world!")
}
Expand Down
11 changes: 8 additions & 3 deletions backend/app/src/main/kotlin/backend/config/Auth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ fun Application.configureAuth() {
val containsAudience = credential.payload.audience.contains(environment.config.property("auth0.audience").getString())

if (containsAudience) {
val userRepository = UserRepository()
return CustomPrincipal(credential.payload, 1)
/*val userRepository = UserRepository()
val subject = credential.payload.subject
val providerId = subject.split("|")[1]
val provider = userRepository.findProviderById(providerId) ?: throw Exception("Provider not found")
return CustomPrincipal(credential.payload, provider.userId)
return CustomPrincipal(credential.payload, provider.userId)*/
}

return null
Expand All @@ -33,7 +34,11 @@ fun Application.configureAuth() {
.build()

install(Authentication) {
jwt("auth0") {
jwt("auth0-user") {
verifier(jwkProvider, issuer)
validate { credential -> validateCreds(credential) }
}
jwt("auth0-admin") {
verifier(jwkProvider, issuer)
validate { credential -> validateCreds(credential) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DatabaseFactory(
if (embedded) {
return HikariDataSource().apply {
dataSourceClassName = JdbcDataSource::class.qualifiedName
addDataSourceProperty("url", "jdbc:h2:mem:inventy;DB_CLOSE_DELAY=-1")
addDataSourceProperty("url", "jdbc:h2:mem:workshop;DB_CLOSE_DELAY=-1")
addDataSourceProperty("user", "root")
addDataSourceProperty("password", "")
maximumPoolSize = 10
Expand Down
2 changes: 1 addition & 1 deletion backend/app/src/main/kotlin/backend/dto/WorkshopDTO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package backend.dto
import kotlinx.serialization.Serializable

@Serializable
data class WorkshopDTO(val title: String, val teacherName: String)
data class WorkshopDTO(val title: String, val description: String)

21 changes: 20 additions & 1 deletion backend/app/src/main/kotlin/backend/model/Workshop.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
package backend.model

class Workshop(override val id: Int, val title: String, val teacherName: String) : Model
import backend.dto.WorkshopDTO
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import java.time.Instant

@Serializable
class Workshop(
override val id: Int,
val title: String,
val description: String,
@Contextual
val startTime: Instant,
@Contextual
val endTime: Instant,
val capacity: Int
) : Model {
constructor(id: Int, title: String, description: String) : this(id, title, description, Instant.now(), Instant.now(), 30)
fun toDTO() = WorkshopDTO(title, description)

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AdminRepository {
val userMap = TestData.userMap
val registrationMap = TestData.registrationMap
val workshopMap = TestData.workshopMap

/*
fun getWorkshops(): List<AdminWorkshopDTO> {
return workshopMap.map { workshop ->
val registrations =
Expand Down Expand Up @@ -41,4 +41,5 @@ class AdminRepository {
}
return AdminWorkshopDTO(workshop.title, workshop.teacherName, registrations)
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package backend.repository

import backend.config.CustomPrincipal
import io.ktor.server.auth.*

class UserOwnedRepository {
open class UserOwnedRepository(val userId: Long) {
companion object {
inline fun <T, reified R: UserOwnedRepository> userContext(auth: AuthenticationContext, block: (repository: R) -> T): T {
val principal = auth.principal<CustomPrincipal>()
val repository = R::class.java.getDeclaredConstructor(Long::class.java).newInstance(principal!!.userId)
return block(repository)
}
}
}
}
Loading

0 comments on commit 54011cc

Please sign in to comment.