Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package edu.ntnu.idi.idatt.backend.classroom.api

import edu.ntnu.idi.idatt.backend.classroom.application.JoinClassroomCommand
import edu.ntnu.idi.idatt.backend.classroom.application.JoinClassroomResult
import edu.ntnu.idi.idatt.backend.classroom.application.OwnedClassroomSummary

fun JoinClassroomRequest.toCommand(authenticatedSubject: String): JoinClassroomCommand =
JoinClassroomCommand(
Expand All @@ -16,3 +17,14 @@ fun JoinClassroomResult.toJoinClassroomResponse(): JoinClassroomResponse =
membershipStatus = membershipStatus,
message = message,
)

fun OwnedClassroomSummary.toOwnedClassroomResponse(): OwnedClassroomResponse =
OwnedClassroomResponse(
id = id,
title = title,
description = description,
pupilCount = pupilCount,
pendingPupilCount = pendingPupilCount,
teacherCount = teacherCount,
taskCount = taskCount,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import edu.ntnu.idi.idatt.backend.classroom.application.ClassroomService
import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import jakarta.validation.Valid
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
Expand All @@ -17,6 +19,22 @@ import org.springframework.web.bind.annotation.RestController
class ClassroomController(
private val classroomService: ClassroomService,
) {
@GetMapping("/owned")
fun listOwnedClassrooms(
@AuthenticationPrincipal currentUser: CurrentUser,
): List<OwnedClassroomResponse> =
classroomService
.listOwnedClassrooms(currentUser.subject)
.map { it.toOwnedClassroomResponse() }

@GetMapping("/{classroomId}")
fun getOwnedClassroom(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable classroomId: Long,
): OwnedClassroomResponse =
classroomService
.getOwnedClassroom(currentUser.subject, classroomId)
.toOwnedClassroomResponse()

/**
* Endpoint for joining a classroom.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.ntnu.idi.idatt.backend.classroom.api

/**
* Minimal teacher-facing classroom summary used by classroom overview and playtest flows.
*/
data class OwnedClassroomResponse(
val id: Long,
val title: String,
val description: String?,
val pupilCount: Int,
val pendingPupilCount: Int,
val teacherCount: Int,
val taskCount: Int,
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package edu.ntnu.idi.idatt.backend.classroom.application

import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom
import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomPupilStatus
import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomStatus
import edu.ntnu.idi.idatt.backend.classroom.infrastructure.ClassroomRepository
import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress
import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId
import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository
import edu.ntnu.idi.idatt.backend.game.tasks.infrastructure.TaskRepository
import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver
import edu.ntnu.idi.idatt.backend.iam.domain.UserRole
import org.springframework.context.ApplicationEventPublisher
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
Expand All @@ -24,8 +28,42 @@ class ClassroomService(
private val classroomRepository: ClassroomRepository,
private val currentUserResolver: CurrentUserResolver,
private val pupilGameProgressRepository: PupilGameProgressRepository,
private val taskRepository: TaskRepository,
private val eventPublisher: ApplicationEventPublisher,
) {
@Transactional(readOnly = true)
fun listOwnedClassrooms(authenticatedSubject: String): List<OwnedClassroomSummary> {
val user = currentUserResolver.requireTeacherOrAdmin(authenticatedSubject)
val userId = requireNotNull(user.id) { "Authenticated user is missing an id" }

val classrooms = if (user.role == UserRole.ADMIN) {
classroomRepository.findAll().sortedWith(compareBy({ it.title }, { it.id }))
} else {
classroomRepository.findAllByOwnerTeacherIdOrderByTitleAscIdAsc(userId)
}

return classrooms.map(::toOwnedClassroomSummary)
}

@Transactional(readOnly = true)
fun getOwnedClassroom(
authenticatedSubject: String,
classroomId: Long,
): OwnedClassroomSummary {
val user = currentUserResolver.requireTeacherOrAdmin(authenticatedSubject)
val classroom = classroomRepository.findById(classroomId)
.orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Classroom not found")
}
val userId = requireNotNull(user.id) { "Authenticated user is missing an id" }

if (user.role != UserRole.ADMIN && classroom.ownerTeacherId != userId) {
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Only the classroom owner may access this classroom")
}

return toOwnedClassroomSummary(classroom)
}

/**
* Adds an authenticated pupil to a classroom as a pending member.
*
Expand Down Expand Up @@ -78,4 +116,15 @@ class ClassroomService(
}
return normalizedCode
}

private fun toOwnedClassroomSummary(classroom: Classroom): OwnedClassroomSummary =
OwnedClassroomSummary(
id = classroom.id,
title = classroom.title,
description = classroom.description,
pupilCount = classroom.pupilMemberships.count { it.status == ClassroomPupilStatus.ACTIVE },
pendingPupilCount = classroom.pupilMemberships.count { it.status == ClassroomPupilStatus.PENDING },
teacherCount = 1,
taskCount = taskRepository.countByClassroomId(classroom.id).toInt(),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.ntnu.idi.idatt.backend.classroom.application

/**
* Lightweight teacher-facing classroom summary used by classroom overview and playtest flows.
*/
data class OwnedClassroomSummary(
val id: Long,
val title: String,
val description: String?,
val pupilCount: Int,
val pendingPupilCount: Int,
val teacherCount: Int,
val taskCount: Int,
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface ClassroomRepository : JpaRepository<Classroom, Long> {
*/
fun findFirstByOwnerTeacherIdOrderByIdAsc(ownerTeacherId: Long): Classroom?

fun findAllByOwnerTeacherIdOrderByTitleAscIdAsc(ownerTeacherId: Long): List<Classroom>

/**
* Returns all classrooms with the average total score across their pupils.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException

enum class GameReadMode {
DEFAULT,
TEACHER_PLAYTEST,
}

/**
* Resolves classroom-scoped game read capabilities from the authenticated session.
*
* This centralizes the differences between normal pupil access, normal teacher/admin access, and
* read-only teacher preview sessions so map-stop and task endpoints apply consistent rules.
* This centralizes the differences between normal pupil access, teacher authoring reads, and
* classroom-scoped teacher playtest reads so map-stop and task endpoints apply consistent rules.
*/
@Service
class GameAccessContextResolver(
Expand All @@ -31,6 +36,7 @@ class GameAccessContextResolver(
fun resolveReadContext(
currentUser: CurrentUser,
classroomId: Long,
readMode: GameReadMode = GameReadMode.DEFAULT,
): GameAccessContext {
val user = currentUserResolver.requireUser(currentUser.subject)
val classroom = classroomRepository.findById(classroomId)
Expand All @@ -39,15 +45,12 @@ class GameAccessContextResolver(
}
val userId = requireNotNull(user.id) { "Authenticated user is missing an id" }

if (currentUser.isPreviewSession()) {
if (readMode == GameReadMode.TEACHER_PLAYTEST) {
if (user.role !in setOf(UserRole.TEACHER, UserRole.ADMIN)) {
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Only teachers and admins may use preview")
}
if (currentUser.previewClassroomId != classroomId) {
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Preview session is limited to one classroom")
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Only teachers and admins may use teacher playtest")
}
if (user.role == UserRole.TEACHER && classroom.ownerTeacherId != userId) {
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Only the classroom owner may preview this classroom")
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Only the classroom owner may playtest this classroom")
}
return GameAccessContext(
classroom = classroom,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package edu.ntnu.idi.idatt.backend.game.mapstops.api

import edu.ntnu.idi.idatt.backend.game.application.GameReadMode
import edu.ntnu.idi.idatt.backend.game.mapstops.application.MapStopService
import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
* Teacher-only read endpoints for classroom playtesting of the published game map.
*/
@RestController
@RequestMapping("/api/v1/teacher/classrooms/{classroomId}/playtest/map-stops")
class TeacherPlaytestMapStopsController(
private val mapStopService: MapStopService,
) {
@GetMapping
fun getMapOverview(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable classroomId: Long,
): List<MapStopOverviewResponse> =
mapStopService
.getMapOverview(currentUser, classroomId, GameReadMode.TEACHER_PLAYTEST)
.map { it.toResponse() }

@GetMapping("/{slug}")
fun getMapStopBySlug(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable classroomId: Long,
@PathVariable slug: String,
): MapStopOverviewResponse =
mapStopService
.getMapStopOverviewBySlug(currentUser, classroomId, slug, GameReadMode.TEACHER_PLAYTEST)
.toResponse()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package edu.ntnu.idi.idatt.backend.game.mapstops.application

import edu.ntnu.idi.idatt.backend.game.application.GameAccessContext
import edu.ntnu.idi.idatt.backend.game.application.GameAccessContextResolver
import edu.ntnu.idi.idatt.backend.game.application.GameReadMode
import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop
import edu.ntnu.idi.idatt.backend.game.mapstops.domain.StopProgressStatus
import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository
Expand Down Expand Up @@ -52,8 +53,12 @@ class MapStopService(
* @param classroomId classroom to scope the progress lookup to
* @return sorted list of stops with progress state for the pupil
*/
fun getMapOverview(currentUser: CurrentUser, classroomId: Long): List<MapStopOverviewEntry> {
val accessContext = gameAccessContextResolver.resolveReadContext(currentUser, classroomId)
fun getMapOverview(
currentUser: CurrentUser,
classroomId: Long,
readMode: GameReadMode = GameReadMode.DEFAULT,
): List<MapStopOverviewEntry> {
val accessContext = gameAccessContextResolver.resolveReadContext(currentUser, classroomId, readMode)
val stops = mapStopsRepository.findAllByIsActiveTrueOrderByDisplayOrderAsc()
if (accessContext.allStopsOpen || accessContext.pupilUserId == null) {
return stops.mapNotNull { stop -> stop.toOpenOverviewEntry() }
Expand Down Expand Up @@ -92,8 +97,9 @@ class MapStopService(
currentUser: CurrentUser,
classroomId: Long,
slug: String,
readMode: GameReadMode = GameReadMode.DEFAULT,
): MapStopOverviewEntry {
val accessContext = gameAccessContextResolver.resolveReadContext(currentUser, classroomId)
val accessContext = gameAccessContextResolver.resolveReadContext(currentUser, classroomId, readMode)
val stop = getMapStopBySlug(slug)
if (accessContext.allStopsOpen || accessContext.pupilUserId == null) {
return stop.toOpenOverviewEntry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ fun CreateTaskRequest.toCommand(
): CreateTaskCommand =
CreateTaskCommand(
authenticatedSubject = currentUser.subject,
sessionMode = currentUser.sessionMode,
classroomId = classroomId,
stopId = stopId,
title = title.toLocalizedText(),
Expand All @@ -150,7 +149,6 @@ fun UpdateTaskRequest.toCommand(
): UpdateTaskCommand =
UpdateTaskCommand(
authenticatedSubject = currentUser.subject,
sessionMode = currentUser.sessionMode,
classroomId = classroomId,
taskId = taskId,
stopId = stopId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package edu.ntnu.idi.idatt.backend.game.tasks.api

import edu.ntnu.idi.idatt.backend.game.application.GameReadMode
import edu.ntnu.idi.idatt.backend.game.tasks.application.TaskService
import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

/**
* Teacher-only read endpoints for classroom playtesting of published classroom tasks.
*/
@RestController
@RequestMapping("/api/v1/teacher/classrooms/{classroomId}/playtest/tasks")
class TeacherPlaytestTaskController(
private val taskService: TaskService,
) {
@GetMapping
fun listTasks(
@PathVariable classroomId: Long,
@RequestParam(required = false) stopId: Long?,
@AuthenticationPrincipal currentUser: CurrentUser,
): List<TaskSummaryResponse> =
taskService
.listTasks(currentUser, classroomId, stopId, GameReadMode.TEACHER_PLAYTEST)
.map { it.toResponse() }

@GetMapping("/{taskId}")
fun getTask(
@PathVariable classroomId: Long,
@PathVariable taskId: Long,
@AuthenticationPrincipal currentUser: CurrentUser,
): TaskResponse =
taskService
.getTask(currentUser, classroomId, taskId, GameReadMode.TEACHER_PLAYTEST)
.let { it.task.toResponse(includeAnswers = it.includeAnswers) }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package edu.ntnu.idi.idatt.backend.game.tasks.application

import edu.ntnu.idi.idatt.backend.iam.application.SessionMode
import edu.ntnu.idi.idatt.backend.game.tasks.domain.TaskPassingRule
import edu.ntnu.idi.idatt.backend.game.tasks.domain.TaskType
import java.time.Instant
Expand Down Expand Up @@ -47,7 +46,6 @@ data class ReplaceTaskItemCommand(
*/
data class CreateTaskCommand(
val authenticatedSubject: String,
val sessionMode: SessionMode,
val classroomId: Long,
val stopId: Long,
val title: LocalizedText,
Expand All @@ -65,7 +63,6 @@ data class CreateTaskCommand(
*/
data class UpdateTaskCommand(
val authenticatedSubject: String,
val sessionMode: SessionMode,
val classroomId: Long,
val taskId: Long,
val stopId: Long,
Expand Down
Loading