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

Commit

Permalink
Initial user feature
Browse files Browse the repository at this point in the history
  • Loading branch information
daudmohamed committed Oct 28, 2023
1 parent 2807d1f commit b0b60b7
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea/
9 changes: 9 additions & 0 deletions backend/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
// Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
id("org.jetbrains.kotlin.jvm") version "1.9.0"
id("io.ktor.plugin") version "2.3.5"
id("org.jetbrains.kotlin.plugin.serialization") version "1.9.0"


// Apply the application plugin to add support for building a CLI application in Java.
Expand All @@ -24,6 +25,7 @@ dependencies {
val ktor_version = "2.3.5"
val logback_version = "1.4.11"
val slf4j_version = "2.0.9"
val kotlin_version = "1.9.0"

// Use the Kotlin JUnit 5 integration.
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
Expand All @@ -39,7 +41,14 @@ dependencies {
implementation("ch.qos.logback:logback-classic:$logback_version")
implementation("org.slf4j:slf4j-api:$slf4j_version")

// Serialization
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")

// Kotlin Query
implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
implementation("com.github.seratch:kotliquery:1.9.0")
implementation("com.h2database:h2:2.1.214")
}

// Apply a specific Java toolchain to ease working on different environments.
Expand Down
13 changes: 13 additions & 0 deletions backend/app/src/main/kotlin/backend/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
*/
package backend

import backend.user.configureUserRoutes
import backend.user.userRoutes
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun server() = embeddedServer(factory = Netty, port = 8080) {
configureRouting()
}


fun Application.configureRouting() {
install(ContentNegotiation) {
json()
}
configureUserRoutes()
routing {
get {
call.respondText("Hello, world!")
Expand Down
11 changes: 11 additions & 0 deletions backend/app/src/main/kotlin/backend/admin/Routing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package backend.admin

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun Routing.adminRoutes() {
get("/admin") {
call.respondText("Hello, world!")
}
}
9 changes: 9 additions & 0 deletions backend/app/src/main/kotlin/backend/user/DTO.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package backend.user

class DTO {
var id: Int = 0
var firstName: String = ""
var lastName: String = ""
var email: String = ""
}

57 changes: 57 additions & 0 deletions backend/app/src/main/kotlin/backend/user/Repository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package backend.user

import kotlinx.serialization.*
import java.lang.RuntimeException


@Serializable
class User(val id: Int, val firstName: String, val lastName: String, val email: String)

enum class RegistrationState {
PENDING, WAITLIST, APPROVED, CANCELED,

}

@Serializable
class WorkShopRegistration(val id: Int, val userId: Int, val workshopId: Int, var state: RegistrationState = RegistrationState.PENDING)
@Serializable
class Workshop(val id: Int, val title: String, val teacherName: String)

class Repository {
var userMap = mutableMapOf<Int, User>(
1 to User(1, "John", "Doe", "[email protected]"),
2 to User(2, "Jane", "Doe", "[email protected]"),
3 to User(3, "John", "Smith", "[email protected]",)
)

var registrationMap = mutableMapOf<Int, WorkShopRegistration>(
1 to WorkShopRegistration(1,1, 1, RegistrationState.APPROVED),
2 to WorkShopRegistration(2, 1, 2, RegistrationState.APPROVED),
3 to WorkShopRegistration(3, 2, 1, RegistrationState.APPROVED),
4 to WorkShopRegistration(4,3, 3, RegistrationState.APPROVED)
)

var workshopMap = mutableMapOf<Int, Workshop>(
1 to Workshop(1, "Kotlin", "John Doe"),
2 to Workshop(2, "Ktor", "Jane Doe"),
3 to Workshop(3, "Kotlin Multiplatform", "John Doe")
)

fun getWorkShopRegistrations(userId: Int) : List<Workshop> {
val workshopIds = registrationMap.filter { it.value.userId == userId && it.value.state == RegistrationState.APPROVED }.map { it.value.workshopId }
return workshopMap.filter { workshopIds.contains(it.key) }.values.toList()
}

fun addWorkshopRegistrations(userId: Int, workshopId: Int) {
workshopMap.get(workshopId) ?: throw RuntimeException("Workshop does not exist")
val maxId = registrationMap.keys.max() + 1
registrationMap.put(maxId, WorkShopRegistration(maxId, userId, workshopId, RegistrationState.PENDING))
}

fun cancelWorkshopRegistration(uesrId: Int, workshopId: Int) {
val registration = registrationMap.filter { it.value.userId == uesrId && it.value.workshopId == workshopId }
.values.firstOrNull() ?: throw RuntimeException("Workshop registration does not exist")
registration.state = RegistrationState.CANCELED
}

}
34 changes: 34 additions & 0 deletions backend/app/src/main/kotlin/backend/user/Routing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package backend.user

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*



fun Application.configureUserRoutes() {
routing {
userRoutes(Repository())
}
}

fun Routing.userRoutes(userRepository: Repository) {
get("/user/workshop") {
// Should be based on the logged in user
call.respond(userRepository.getWorkShopRegistrations(1))
}

post("/user/workshop/{workshopId}") {
try {
userRepository.addWorkshopRegistrations(1, call.parameters["workshopId"]!!.toInt())
call.respondText("Workshop added")
} catch (e: Exception) {
call.respondText("Workshop not found", status = io.ktor.http.HttpStatusCode.NotFound)
}
}

put("/user/workshop/{workshopId}/cancel") {
userRepository.cancelWorkshopRegistration(1, call.parameters["workshopId"]!!.toInt())
call.respondText("Workshop cancelled")
}
}
29 changes: 29 additions & 0 deletions backend/app/src/main/kotlin/backend/workshop/Repository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package backend.workshop


class Workshop(val id: Int, val title: String, val teacherName: String) {}
class Repository {
var map = mutableMapOf<Int, Workshop>(
1 to Workshop(1, "Kotlin", "John Doe"),
2 to Workshop(2, "Ktor", "Jane Doe"),
3 to Workshop(3, "Kotlin Multiplatform", "John Doe")
)


fun getAll(): List<Workshop> {
return map.values.toList()
}

fun getById(id: Int): Workshop? {
return map[id]
}

fun add(workshop: Workshop) {
val maxId = map.keys.maxOrNull()?.plus(1) ?: 1
map[maxId] = workshop
}

fun update(workshop: Workshop) {
map[workshop.id] = workshop
}
}

0 comments on commit b0b60b7

Please sign in to comment.