This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2807d1f
commit b0b60b7
Showing
8 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
backend/app/src/main/kotlin/backend/workshop/Repository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |