Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ class DevDataBootstrap(
seedClassroomMembership(sampleClassroom, pupilAda, "Ada")
seedClassroomMembership(sampleClassroom, pupilNoah, "Noah")
seedClassroomMembership(sampleClassroom, pupilSofia, "Sofia")
seedPupilProfile(pupilAda, "Ada", sampleClassroom.id)
seedPupilProfile(pupilNoah, "Noah", sampleClassroom.id)
seedPupilProfile(pupilSofia, "Sofia", sampleClassroom.id)
seedPupilProfile(pupilAda, "Ada", sampleClassroom)
seedPupilProfile(pupilNoah, "Noah", sampleClassroom)
seedPupilProfile(pupilSofia, "Sofia", sampleClassroom)
seedPupilAvatar(pupilAda)
seedPupilAvatar(pupilNoah)
seedPupilAvatar(pupilSofia)
Expand Down Expand Up @@ -256,20 +256,20 @@ class DevDataBootstrap(
private fun seedPupilProfile(
user: User,
displayName: String,
currentClassroomId: Long?,
currentClassroom: Classroom?,
) {
val userId = requireNotNull(user.id) { "Pupil seed user must have an id" }
val existingProfile = pupilProfileRepository.findById(userId).orElse(null)
if (existingProfile != null) {
existingProfile.displayName = displayName
existingProfile.currentClassroomId = currentClassroomId
existingProfile.currentClassroom = currentClassroom
return
}
pupilProfileRepository.save(
PupilProfile(
user = user,
displayName = displayName,
currentClassroomId = currentClassroomId,
currentClassroom = currentClassroom,
),
)
}
Expand Down Expand Up @@ -341,6 +341,7 @@ class DevDataBootstrap(
membership.nicknameInClass = nicknameInClass
membership.status = ClassroomPupilStatus.ACTIVE
membership.approvedAt = membership.approvedAt ?: Instant.now()

classroomRepository.save(classroom)
}

Expand All @@ -350,14 +351,14 @@ class DevDataBootstrap(
description: String,
joinCode: String,
): Classroom {
classroomRepository.findByJoinCode(joinCode)?.let { existingClassroom ->
val teacherId = requireNotNull(ownerTeacher.id) { "Teacher seed user must have an id" }
val classroom = classroomRepository.findByJoinCode(joinCode)?.let { existingClassroom ->
existingClassroom.ownerTeacherId = requireNotNull(ownerTeacher.id) { "Teacher seed user must have an id" }
existingClassroom.title = title
existingClassroom.description = description
existingClassroom.status = ClassroomStatus.ACTIVE
return existingClassroom
}
return classroomRepository.save(
} ?: classroomRepository.save(
Classroom(
ownerTeacherId = requireNotNull(ownerTeacher.id) { "Teacher seed user must have an id" },
title = title,
Expand All @@ -366,6 +367,16 @@ class DevDataBootstrap(
status = ClassroomStatus.ACTIVE,
),
)

val alreadyMember = classroom.teacherMemberships.any {
it.id.teacherUserId == teacherId
}

if (!alreadyMember) {
classroom.addTeacher(ownerTeacher)
}

return classroomRepository.save(classroom)
}

private fun seedRefreshToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fun JoinClassroomResult.toJoinClassroomResponse(): JoinClassroomResponse =
*/
fun Classroom.toResponse(): ClassroomResponse =
ClassroomResponse(
id = id,
classroomId = id,
title = title,
description = description,
joinCode = joinCode ?: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package edu.ntnu.idi.idatt.backend.classroom.api
/**
* DTO for response to classroom requests.
*
* @param id the id of a classroom.
* @param classroomId the id of a classroom.
* @param title the title of a classroom.
* @param description the optional description of a classroom.
* @param joinCode the code to join the classroom.
* @param teacherUserIds a list of ids of teahcers belonging to the classroom.
*/
data class ClassroomResponse(
val id: Long,
val classroomId: Long,
val title: String,
val description: String?,
val joinCode: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver
import edu.ntnu.idi.idatt.backend.iam.infrastructure.UserRepository
import edu.ntnu.idi.idatt.backend.game.tasks.infrastructure.TaskRepository
import edu.ntnu.idi.idatt.backend.iam.domain.UserRole
import edu.ntnu.idi.idatt.backend.users.profile.infrastructure.PupilProfileRepository
import org.springframework.context.ApplicationEventPublisher
import edu.ntnu.idi.idatt.backend.shared.events.DomainEventPublisher
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
Expand All @@ -36,6 +38,7 @@ class ClassroomService(
private val classroomUserResolver: ClassroomUserResolver,
private val pupilGameProgressService: PupilGameProgressService,
private val taskRepository: TaskRepository,
private val pupilProfileRepository: PupilProfileRepository,
private val domainEventPublisher: DomainEventPublisher,
) {

Expand Down Expand Up @@ -122,10 +125,15 @@ class ClassroomService(
}

classroom.addPupilMembership(user)
val profile = pupilProfileRepository.findById(userId)
.orElseThrow { ResponseStatusException(HttpStatus.NOT_FOUND, "Profile not found") }
profile.currentClassroom = classroom
pupilProfileRepository.save(profile)
classroomRepository.save(classroom)
pupilGameProgressService.createPupilGameProgressEntry(
pupilId = userId,
classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" },

)
domainEventPublisher.publish(PupilJoinedClassroomEvent(pupil = user, classroom = classroom))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package edu.ntnu.idi.idatt.backend.classroom.domain

/**
* Enum representing the possible statuses for a classroom.
* Enum representing the possible statuses for a pupil membership.
*
* - [ACTIVE]: The classroom is open and available for users to join and participate.
* - [INACTIVE]: The classroom is closed or archived and cannot be joined or used.
* - [ACTIVE]: The pupil has been approved.
* - [PENDING]: The pupil has not yet been approved.
*/
enum class ClassroomPupilStatus {
ACTIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class CurrentUserResolver(
.orElseThrow {
ResponseStatusException(HttpStatus.NOT_FOUND, "Pupil profile was not found")
}
val classroomId = pupilProfile.currentClassroomId
val classroomId = pupilProfile.currentClassroom?.id
?: throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Pupil has no current classroom")
classroomRepository.findById(classroomId)
.orElseThrow {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class CurrentUserProfileService(
val pupilProfile = if (user.role == UserRole.PUPIL) {
pupilProfileRepository.findById(userId)
.map {
val progress = getCurrentClassroomProgress(userId, it.currentClassroomId)
val progress = getCurrentClassroomProgress(userId, it.currentClassroom?.id)

PupilProfileDetails(
displayName = it.displayName,
currentClassroomId = it.currentClassroomId,
currentClassroomId = it.currentClassroom?.id,
totalXp = progress?.totalXp ?: 0,
currentLevel = progress?.currentLevel ?: 1,
)
Expand Down Expand Up @@ -97,13 +97,13 @@ class CurrentUserProfileService(
val user = currentUserResolver.requirePupil(authenticatedSubject)
val userId = requireNotNull(user.id) { "Authenticated user is missing an id" }
val pupilProfile = findPupilProfile(userId)
val progress = getCurrentClassroomProgress(userId, pupilProfile.currentClassroomId)
val progress = getCurrentClassroomProgress(userId, pupilProfile.currentClassroom?.id)
pupilProfile.displayName = command.displayName.trim()
val savedProfile = pupilProfileRepository.save(pupilProfile)

return PupilProfileDetails(
displayName = savedProfile.displayName,
currentClassroomId = savedProfile.currentClassroomId,
currentClassroomId = savedProfile.currentClassroom?.id,
totalXp = progress?.totalXp ?: 0,
currentLevel = progress?.currentLevel ?: 1,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package edu.ntnu.idi.idatt.backend.users.profile.domain

import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom
import edu.ntnu.idi.idatt.backend.iam.domain.User

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.MapsId
import jakarta.persistence.OneToOne
import jakarta.persistence.Table
Expand All @@ -17,7 +19,7 @@ import jakarta.persistence.Table
* @property userId shared primary key matching the owning user id
* @property user owning pupil user account
* @property displayName pupil name shown in the product
* @property currentClassroomId current classroom id, or `null` when unassigned
* @property currentClassroom current classroom, or `null` when unassigned
*/
@Entity
@Table(name = "pupil_profiles")
Expand All @@ -31,8 +33,7 @@ class PupilProfile(
var user: User,
@Column(name = "display_name", nullable = false, length = 100)
var displayName: String,
// TODO: replace with @ManyToOne/@JoinColumn when Classroom table exists
// this column references classrooms.id.
@Column(name = "current_classroom_id")
var currentClassroomId: Long? = null,
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "current_classroom_id")
var currentClassroom: Classroom? = null,
)
Loading