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 @@ -71,8 +71,6 @@ class ClassroomUserResolver(
/**
* Verifies that [user] is a member of the given classroom.
*
* TODO: Implement once the classroom membership table is in place.
*
* @param user the authenticated user to check
* @param classroomId the classroom to verify membership of
* @return the classroom if [user] is a member
Expand Down Expand Up @@ -100,7 +98,18 @@ class ClassroomUserResolver(
}

return classroom
}

TODO("Not implemented: requires classroom membership table")
/**
* Verifies that a pupil has a membership row in the supplied classroom.
*
* @param classroom classroom whose pupil memberships should be checked.
* @param pupilUserId pupil user id to verify.
* @throws ResponseStatusException 404 if the pupil is not a member of the classroom.
*/
fun requirePupilInClassroom(classroom: Classroom, pupilUserId: Long) {
if (!classroom.hasPupilMembership(pupilUserId)) {
throw ResponseStatusException(HttpStatus.NOT_FOUND, "Pupil not found in classroom")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package edu.ntnu.idi.idatt.backend.notebook.api

import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import edu.ntnu.idi.idatt.backend.notebook.application.NotebookService
import org.springframework.http.HttpStatus
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.web.bind.annotation.DeleteMapping
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.ResponseStatus
import org.springframework.web.bind.annotation.RestController

/**
* REST API for teacher review of pupil notebook entries.
*/
@RestController
@RequestMapping("/api/v1/classrooms/{classroomId}/pupils/{pupilUserId}/notebook-entries")
class TeacherNotebookEntryController(
private val notebookService: NotebookService,
) {
/**
* Returns notebook entries for a pupil in a classroom.
*
* @param currentUser authenticated teacher principal
* @param classroomId classroom to scope the notebook to
* @param pupilUserId pupil whose notebook should be reviewed
* @return notebook entries serialized for the API layer
*/
@GetMapping
@PreAuthorize("hasRole('TEACHER')")
fun getPupilNotebookEntries(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable classroomId: Long,
@PathVariable pupilUserId: Long,
): List<NotebookEntryResponse> =
notebookService
.getNotebookEntriesForTeacherReview(currentUser, classroomId, pupilUserId)
.map { it.toResponse() }

/**
* Deletes one pupil-authored notebook entry during teacher review.
*
* @param currentUser authenticated teacher principal
* @param classroomId classroom to scope the notebook entry to
* @param pupilUserId pupil whose notebook owns the entry
* @param notebookEntryId notebook entry id to delete
*/
@DeleteMapping("/{notebookEntryId}")
@PreAuthorize("hasRole('TEACHER')")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun deletePupilNotebookEntry(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable classroomId: Long,
@PathVariable pupilUserId: Long,
@PathVariable notebookEntryId: Long,
) {
notebookService.deleteNotebookEntryForTeacherReview(
currentUser,
classroomId,
pupilUserId,
notebookEntryId,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package edu.ntnu.idi.idatt.backend.notebook.application

import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService
import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository
import edu.ntnu.idi.idatt.backend.iam.application.ClassroomUserResolver
import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver
import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookEntry
Expand All @@ -27,6 +28,7 @@ import org.springframework.web.server.ResponseStatusException
class NotebookService(
private val notebookEntryRepository: NotebookEntryRepository,
private val currentUserResolver: CurrentUserResolver,
private val classroomUserResolver: ClassroomUserResolver,
private val mapStopsRepository: MapStopsRepository,
private val pupilGameProgressService: PupilGameProgressService,
private val xpService: XpService,
Expand All @@ -47,7 +49,35 @@ class NotebookService(

val completedStopsCount = pupilGameProgressService.getUserCompletedStopsCount(currentUser)

return notebookEntryRepository.findAllVisibleToPupil(pupilId, classroomId, completedStopsCount)
return notebookEntryRepository.findAllVisibleInNotebook(pupilId, classroomId, completedStopsCount)
}

/**
* Returns notebook entries for a pupil in a classroom when reviewed by a teacher.
*
* @param currentUser authenticated teacher principal
* @param classroomId classroom to scope the notebook to
* @param pupilUserId pupil whose notebook should be reviewed
* @return notebook entries visible in the pupil's notebook context
*/
@Transactional(readOnly = true)
fun getNotebookEntriesForTeacherReview(
currentUser: CurrentUser,
classroomId: Long,
pupilUserId: Long,
): List<NotebookEntry> {
val teacher = currentUserResolver.requireTeacherOrAdmin(currentUser.subject)
val classroom = classroomUserResolver.requireClassroomTeacherAccess(teacher, classroomId)
classroomUserResolver.requirePupilInClassroom(classroom, pupilUserId)

val gameProgress = pupilGameProgressService.getPupilProgress(pupilUserId, classroomId)
val completedStopsCount = gameProgress?.completedStopsCount ?: 0

return notebookEntryRepository.findAllVisibleInNotebook(
pupilUserId,
classroomId,
completedStopsCount,
)
}

/**
Expand All @@ -59,6 +89,7 @@ class NotebookService(
@Transactional
fun createNotebookEntry(command: CreateNotebookEntryCommand): NotebookEntry {
val pupil = currentUserResolver.requirePupil(command.currentUser.subject)
requireNotNull(pupil.id) { "Authenticated pupil is missing an id" }
val classroom = currentUserResolver.getClassroomByUser(command.currentUser)

if (command.sourceType == NotebookSourceType.GAME) {
Expand Down Expand Up @@ -117,6 +148,42 @@ class NotebookService(
notebookEntryRepository.delete(notebookEntry)
}

/**
* Deletes one pupil-authored notebook entry during teacher review.
*
* @param currentUser authenticated teacher principal
* @param classroomId classroom to scope the notebook entry to
* @param pupilUserId pupil whose notebook owns the entry
* @param notebookEntryId notebook entry id to delete
*/
@Transactional
fun deleteNotebookEntryForTeacherReview(
currentUser: CurrentUser,
classroomId: Long,
pupilUserId: Long,
notebookEntryId: Long,
) {
val teacher = currentUserResolver.requireTeacherOrAdmin(currentUser.subject)
val classroom = classroomUserResolver.requireClassroomTeacherAccess(teacher, classroomId)
classroomUserResolver.requirePupilInClassroom(classroom, pupilUserId)

val notebookEntry = notebookEntryRepository.findById(notebookEntryId).orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Notebook entry not found")
}

if (notebookEntry.sourceType == NotebookSourceType.GAME) {
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Game-generated notebook entries cannot be deleted")
}

val ownerId = notebookEntry.pupil?.id
val entryClassroomId = notebookEntry.classroom.id
if (ownerId != pupilUserId || entryClassroomId != classroomId) {
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Notebook entry does not belong to this pupil notebook")
}

notebookEntryRepository.delete(notebookEntry)
}

/**
* Loads one notebook entry and verifies that it is owned by the supplied pupil.
*
Expand All @@ -132,7 +199,7 @@ class NotebookService(
}

if (notebookEntry.sourceType == NotebookSourceType.GAME) {
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Pupil cannot create notebook entries outside the active classroom")
throw ResponseStatusException(HttpStatus.FORBIDDEN, "Game-generated notebook entries cannot be deleted")
}

val ownerId = notebookEntry.pupil?.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@ import org.springframework.stereotype.Repository
interface NotebookEntryRepository : JpaRepository<NotebookEntry, Long> {

/**
* Finds all notebook entries visible to a pupil:
* - pupil-authored entries owned by the pupil
* - game-authored entries for the pupil's active classroom
* Finds all notebook entries visible in a pupil notebook context:
* - pupil-authored entries owned by the pupil in the classroom
* - game-authored entries for the classroom
*
* @param pupilId the id of the pupil
* @param classroomId the id of the active classroom
* @return all visible notebook entries
* @param classroomId the id of the classroom
* @param completedStops number of completed stops used to gate notebook visibility
* @return all visible notebook entries for the pupil notebook
*/
@Query(
"""
select notebookEntry
from NotebookEntry notebookEntry
where (notebookEntry.pupil.id = :pupilId
or (notebookEntry.pupil is null and notebookEntry.classroom.id = :classroomId))
and notebookEntry.classroom.id = :classroomId
and notebookEntry.stop.displayOrder <= :completedStops + 1
"""
)
fun findAllVisibleToPupil(pupilId: Long, classroomId: Long, completedStops: Int): List<NotebookEntry>
fun findAllVisibleInNotebook(
pupilId: Long,
classroomId: Long,
completedStops: Int,
): List<NotebookEntry>
}
Loading