Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import edu.ntnu.idi.idatt.backend.iam.domain.UserRole
import edu.ntnu.idi.idatt.backend.iam.domain.UserStatus
import edu.ntnu.idi.idatt.backend.iam.infrastructure.RefreshTokenRepository
import edu.ntnu.idi.idatt.backend.iam.infrastructure.UserRepository
import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookEntry
import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookSourceType
import edu.ntnu.idi.idatt.backend.notebook.infrastructure.NotebookEntryRepository
import edu.ntnu.idi.idatt.backend.users.avatar.domain.UserAvatar
import edu.ntnu.idi.idatt.backend.users.avatar.infrastructure.UserAvatarRepository
import edu.ntnu.idi.idatt.backend.users.profile.domain.PupilProfile
Expand Down Expand Up @@ -64,6 +67,7 @@ class DevDataBootstrap(
private val userAvatarRepository: UserAvatarRepository,
private val refreshTokenRepository: RefreshTokenRepository,
private val pupilGameProgressRepository: PupilGameProgressRepository,
private val notebookEntryRepository: NotebookEntryRepository,
private val passwordEncoder: PasswordEncoder,
private val mapStopBootstrap: MapStopBootstrap,
private val mapStopsRepository: MapStopsRepository,
Expand Down Expand Up @@ -157,6 +161,12 @@ class DevDataBootstrap(
teacher = teacher,
stops = mapStopsRepository.findAllByIsActiveTrueOrderByDisplayOrderAsc(),
)
seedNotebookEntries(
classroom = sampleClassroom,
pupilAda = pupilAda,
pupilNoah = pupilNoah,
stops = mapStopsRepository.findAllByIsActiveTrueOrderByDisplayOrderAsc(),
)

pupilProgressSeedingService.seedIfAbsent(
pupil = pupilAda,
Expand Down Expand Up @@ -366,6 +376,78 @@ class DevDataBootstrap(
MessageDigest.getInstance("SHA-256").digest(rawToken.toByteArray(StandardCharsets.UTF_8)),
)

private fun seedNotebookEntries(
classroom: Classroom,
pupilAda: User,
pupilNoah: User,
stops: List<MapStop>,
) {
val activeStopsBySlug = stops.associateBy { requireNotNull(it.slug) { "Seeded map stop must have a slug" } }

activeStopsBySlug["nyhetskvartalet"]?.let { stop ->
seedNotebookEntry(
pupil = pupilAda,
classroom = classroom,
stop = stop,
sourceType = NotebookSourceType.PUPIL,
title = "Merkelige overskrifter",
content = "Ada la merke til at overskriften brukte sterke følelser og manglet tydelige kilder.",
)
}
activeStopsBySlug["postkontoret"]?.let { stop ->
seedNotebookEntry(
pupil = pupilAda,
classroom = classroom,
stop = stop,
sourceType = NotebookSourceType.GAME,
title = "Phishing-tegn",
content = "Spillet markerte dårlig språk, hastverk og mistenkelig lenke som tydelige faresignaler.",
)
}
activeStopsBySlug["fotografen"]?.let { stop ->
seedNotebookEntry(
pupil = pupilNoah,
classroom = classroom,
stop = stop,
sourceType = NotebookSourceType.PUPIL,
title = "Bildesøk hjalp",
content = "Noah fant samme bilde i en eldre artikkel og skrev ned at kontekst manglet i innlegget.",
)
}
}

private fun seedNotebookEntry(
pupil: User,
classroom: Classroom,
stop: MapStop,
sourceType: NotebookSourceType,
title: String,
content: String,
) {
val pupilId = requireNotNull(pupil.id) { "Pupil seed user must have an id" }
val classroomId = classroom.id
val stopId = requireNotNull(stop.id) { "Seeded map stop must have an id" }
val existingEntry = notebookEntryRepository.findAllByPupilId(pupilId).firstOrNull {
it.classroom.id == classroomId && it.stop.id == stopId && it.title == title
}
if (existingEntry != null) {
existingEntry.sourceType = sourceType
existingEntry.content = content
return
}

notebookEntryRepository.save(
NotebookEntry(
pupil = pupil,
classroom = classroom,
stop = stop,
sourceType = sourceType,
title = title,
content = content,
),
)
}

private fun seedTasks(
classroom: Classroom,
teacher: User,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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.CreateNotebookEntryCommand
import edu.ntnu.idi.idatt.backend.notebook.application.DeleteNotebookEntryCommand
import edu.ntnu.idi.idatt.backend.notebook.application.UpdateNotebookEntryCommand
import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookEntry

/**
* Converts a notebook entry entity into the public API response payload.
*
* @return serialized notebook entry response
*/
fun NotebookEntry.toResponse(): NotebookEntryResponse =
NotebookEntryResponse(
id = requireNotNull(id) { "Notebook entry is missing an id" },
pupilUserId = requireNotNull(pupil.id) { "Notebook entry pupil is missing an id" },
classroomId = classroom.id,
stopId = requireNotNull(stop.id) { "Notebook entry stop is missing an id" },
sourceType = sourceType.name,
title = title,
content = content,
createdAt = createdAt,
updatedAt = updatedAt,
)

/**
* Converts the create request payload into an application command.
*
* @param currentUser authenticated principal for the request
* @return create command consumed by the service layer
*/
fun CreateNotebookEntryRequest.toCommand(currentUser: CurrentUser): CreateNotebookEntryCommand =
CreateNotebookEntryCommand(
currentUser = currentUser,
classroomId = classroomId,
stopId = stopId,
sourceType = sourceType,
title = title,
content = content,
)

/**
* Converts the update request payload into an application command.
*
* @param currentUser authenticated principal for the request
* @param notebookEntryId notebook entry id from the request path
* @return update command consumed by the service layer
*/
fun UpdateNotebookEntryRequest.toCommand(
currentUser: CurrentUser,
notebookEntryId: Long,
): UpdateNotebookEntryCommand =
UpdateNotebookEntryCommand(
currentUser = currentUser,
notebookEntryId = notebookEntryId,
title = title,
content = content,
)

/**
* Converts the authenticated principal and path id into a delete command.
*
* @param notebookEntryId notebook entry id from the request path
* @return delete command consumed by the service layer
*/
fun CurrentUser.toDeleteCommand(notebookEntryId: Long): DeleteNotebookEntryCommand =
DeleteNotebookEntryCommand(
currentUser = this,
notebookEntryId = notebookEntryId,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 jakarta.validation.Valid
import org.springframework.http.HttpStatus
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.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController

/**
* REST API for the authenticated pupil's notebook entries.
*
* @property notebookService application service that handles notebook entry operations
*/
@RestController
@RequestMapping("/api/v1/users/me/notebook-entries")
class NotebookEntryController(
private val notebookService: NotebookService,
) {
/**
* Returns all notebook entries owned by the authenticated pupil.
*
* @param currentUser authenticated principal resolved from the JWT access token
* @return notebook entries serialized for the API layer
*/
@GetMapping
fun getNotebookEntries(
@AuthenticationPrincipal currentUser: CurrentUser,
): List<NotebookEntryResponse> =
notebookService.getAllNotebookEntries(currentUser).map { it.toResponse() }

/**
* Creates a new notebook entry for the authenticated pupil.
*
* @param currentUser authenticated principal resolved from the JWT access token
* @param request notebook entry payload to persist
* @return created notebook entry
*/
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
fun createNotebookEntry(
@AuthenticationPrincipal currentUser: CurrentUser,
@Valid @RequestBody request: CreateNotebookEntryRequest,
): NotebookEntryResponse =
notebookService.createNotebookEntry(request.toCommand(currentUser)).toResponse()

/**
* Updates one notebook entry owned by the authenticated pupil.
*
* @param currentUser authenticated principal resolved from the JWT access token
* @param notebookEntryId notebook entry id from the request path
* @param request notebook entry payload to persist
* @return updated notebook entry
*/
@PutMapping("/{notebookEntryId}")
fun updateNotebookEntry(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable notebookEntryId: Long,
@Valid @RequestBody request: UpdateNotebookEntryRequest,
): NotebookEntryResponse =
notebookService.updateNotebookEntry(request.toCommand(currentUser, notebookEntryId)).toResponse()

/**
* Deletes one notebook entry owned by the authenticated pupil.
*
* @param currentUser authenticated principal resolved from the JWT access token
* @param notebookEntryId notebook entry id from the request path
*/
@DeleteMapping("/{notebookEntryId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
fun deleteNotebookEntry(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable notebookEntryId: Long,
) {
notebookService.deleteNotebookEntry(currentUser.toDeleteCommand(notebookEntryId))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.ntnu.idi.idatt.backend.notebook.api

import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookSourceType
import jakarta.validation.constraints.Min
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull

/**
* Request payload for creating a notebook entry.
*
* @property classroomId classroom the entry belongs to
* @property stopId map stop the entry belongs to
* @property sourceType origin of the entry content
* @property title short notebook entry title
* @property content full notebook entry body
*/
data class CreateNotebookEntryRequest(
@field:Min(1)
val classroomId: Long,
@field:Min(1)
val stopId: Long,
@field:NotNull
var sourceType: NotebookSourceType,
@field:NotBlank
val title: String,
@field:NotBlank
val content: String,
)

/**
* Request payload for updating a notebook entry.
*
* @property title updated notebook entry title
* @property content updated notebook entry body
*/
data class UpdateNotebookEntryRequest(
@field:NotBlank
val title: String,
@field:NotBlank
val content: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.ntnu.idi.idatt.backend.notebook.api

import java.time.Instant

/**
* Serialized notebook entry returned by notebook endpoints.
*
* @property id notebook entry identifier
* @property pupilUserId owner pupil user id
* @property classroomId classroom id for the entry context
* @property stopId map stop id for the entry context
* @property sourceType serialized notebook source type
* @property title notebook entry title
* @property content notebook entry body
* @property createdAt timestamp when the entry was created
* @property updatedAt timestamp when the entry was last updated
*/
data class NotebookEntryResponse(
val id: Long,
val pupilUserId: Long,
val classroomId: Long,
val stopId: Long,
val sourceType: String,
val title: String,
val content: String,
val createdAt: Instant?,
val updatedAt: Instant?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package edu.ntnu.idi.idatt.backend.notebook.application

import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser
import edu.ntnu.idi.idatt.backend.notebook.domain.NotebookSourceType

/**
* Command representing the intent to create a notebook entry.
*
* This is part of the application layer and should not depend on HTTP or framework-specific classes.
*
* @property currentUser authenticated user creating the entry.
* @property classroomId Identifier of the classroom the entry belongs to.
* @property stopId Identifier of the map stop the entry belongs to.
* @property sourceType Source that produced the notebook entry.
* @property title Short notebook entry title.
* @property content Full notebook entry body.
*/
data class CreateNotebookEntryCommand(
val currentUser: CurrentUser,
val classroomId: Long,
val stopId: Long,
val sourceType: NotebookSourceType,
val title: String,
val content: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package edu.ntnu.idi.idatt.backend.notebook.application

import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser

/**
* Command representing the intent to delete an existing notebook entry.
*
* This is part of the application layer and should not depend on HTTP or framework-specific classes.
*
* @property currentUser authenticated user deleting the entry.
* @property notebookEntryId Identifier of the notebook entry being deleted.
*/
data class DeleteNotebookEntryCommand(
val currentUser: CurrentUser,
val notebookEntryId: Long,
)
Loading