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.
Merge branch 'main' of github.com:javaBin/workshop-wizard
- Loading branch information
Showing
14 changed files
with
170 additions
and
49 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
55 changes: 55 additions & 0 deletions
55
backend/app/src/main/kotlin/backend/admin/AdminRepository.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,55 @@ | ||
package backend.admin | ||
|
||
import backend.domain.RegistrationState | ||
import backend.domain.User | ||
import backend.domain.WorkShopRegistration | ||
import backend.domain.Workshop | ||
|
||
class AdminRepository { | ||
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 getWorkshops(): List<AdminWorkshopDTO> { | ||
return workshopMap.map { workshop -> | ||
val registrations = registrationMap.filter { it.value.workshopId == workshop.key } | ||
.map { it.value } | ||
.map { AdminWorkshopRegistration( | ||
userMap[it.userId]!!.firstName, | ||
userMap[it.userId]!!.lastName, | ||
userMap[it.userId]!!.email, | ||
it.state) | ||
} | ||
AdminWorkshopDTO(workshop.value.title, workshop.value.teacherName, registrations) | ||
} | ||
} | ||
|
||
fun getWorkshopRegistrations(workshopId: Int): AdminWorkshopDTO { | ||
val workshop = workshopMap.get(workshopId) ?: throw RuntimeException("Workshop does not exist") | ||
val registrations = registrationMap.filter { it.value.workshopId == workshop.id } | ||
.map { it.value } | ||
.map { | ||
AdminWorkshopRegistration( | ||
userMap[it.userId]!!.firstName, | ||
userMap[it.userId]!!.lastName, | ||
userMap[it.userId]!!.email, | ||
it.state | ||
) | ||
} | ||
return AdminWorkshopDTO(workshop.title, workshop.teacherName, registrations) | ||
} | ||
} |
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,12 @@ | ||
package backend.admin | ||
|
||
import backend.domain.RegistrationState | ||
import backend.user.WorkshopDTO | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class AdminWorkshopDTO(val title: String, val teacherName: String, val registrations: List<AdminWorkshopRegistration>) | ||
|
||
@Serializable | ||
data class AdminWorkshopRegistration(val firstName: String, val lastName: String, val email: String, val state: RegistrationState) | ||
|
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,3 @@ | ||
package backend.domain | ||
|
||
class User(val id: Int, val firstName: String, val lastName: String, val 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,3 @@ | ||
package backend.domain | ||
|
||
class Workshop(val id: Int, val title: String, val teacherName: String) |
3 changes: 3 additions & 0 deletions
3
backend/app/src/main/kotlin/backend/domain/WorkshopRegistration.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,3 @@ | ||
package backend.domain | ||
|
||
class WorkShopRegistration(val id: Int, val userId: Int, val workshopId: Int, var state: RegistrationState = RegistrationState.PENDING) |
7 changes: 7 additions & 0 deletions
7
backend/app/src/main/kotlin/backend/domain/WorkshopRegistrationState.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,7 @@ | ||
package backend.domain | ||
|
||
|
||
enum class RegistrationState { | ||
PENDING, WAITLIST, APPROVED, 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 |
---|---|---|
@@ -1,9 +1,8 @@ | ||
package backend.user | ||
|
||
class DTO { | ||
var id: Int = 0 | ||
var firstName: String = "" | ||
var lastName: String = "" | ||
var email: String = "" | ||
} | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class WorkshopDTO(val title: String, val teacherName: 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 |
---|---|---|
@@ -1,26 +1,33 @@ | ||
package backend.user | ||
|
||
import backend.CustomPrincipal | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
|
||
fun Routing.userRoutes(userRepository: Repository) { | ||
get("/user/workshop") { | ||
// Should be based on the logged in user | ||
call.respond(userRepository.getWorkShopRegistrations(1)) | ||
} | ||
fun Routing.userRoutes(userRepository: UserRepository) { | ||
authenticate ("basic") { | ||
get("/user/workshop") { | ||
// Should be based on the logged in user | ||
val userId = call.authentication.principal<CustomPrincipal>()?.userId!! | ||
call.respond(userRepository.getWorkShopRegistrations(userId)) | ||
} | ||
|
||
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) | ||
post("/user/workshop/{workshopId}") { | ||
try { | ||
val userId = call.authentication.principal<CustomPrincipal>()?.userId!! | ||
userRepository.addWorkshopRegistrations(userId, 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") | ||
put("/user/workshop/{workshopId}/cancel") { | ||
val userId = call.authentication.principal<CustomPrincipal>()?.userId!! | ||
userRepository.cancelWorkshopRegistration(userId, call.parameters["workshopId"]!!.toInt()) | ||
call.respondText("Workshop cancelled") | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,12 @@ | ||
package backend.user | ||
|
||
import kotlinx.serialization.* | ||
import backend.domain.RegistrationState | ||
import backend.domain.User | ||
import backend.domain.WorkShopRegistration | ||
import backend.domain.Workshop | ||
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 { | ||
class UserRepository { | ||
var userMap = mutableMapOf<Int, User>( | ||
1 to User(1, "John", "Doe", "[email protected]"), | ||
2 to User(2, "Jane", "Doe", "[email protected]"), | ||
|
@@ -37,9 +26,9 @@ class Repository { | |
3 to Workshop(3, "Kotlin Multiplatform", "John Doe") | ||
) | ||
|
||
fun getWorkShopRegistrations(userId: Int) : List<Workshop> { | ||
fun getWorkShopRegistrations(userId: Int) : List<WorkshopDTO> { | ||
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() | ||
return workshopMap.filter { workshopIds.contains(it.key) }.values.map { WorkshopDTO(it.title, it.teacherName) } | ||
} | ||
|
||
fun addWorkshopRegistrations(userId: Int, workshopId: Int) { | ||
|
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