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
@@ -1,15 +1,57 @@
package edu.ntnu.idi.idatt.backend.classroom.api

import edu.ntnu.idi.idatt.backend.classroom.application.CreateClassroomCommand
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.UpdateClassroomCommand
import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom
import edu.ntnu.idi.idatt.backend.classroom.application.OwnedClassroomSummary

/**
* Converts the join classroom request to the application command.
*
* @return a command used to process a classroom joining request.
*/
fun JoinClassroomRequest.toCommand(authenticatedSubject: String): JoinClassroomCommand =
JoinClassroomCommand(
classroomCode = classroomCode,
authenticatedSubject = authenticatedSubject,
)

/**
* Converts a create classroom request to the application command.
*
* @return a command used to process the creation request.
*/
fun CreateClassroomRequest.toCommand(subject: String): CreateClassroomCommand =
CreateClassroomCommand(
title = title,
description = description,
authenticatedSubject = subject,
)

/**
* Converts an update classroom request to the application command.
*
* @return a command used to process the update request.
*/
fun UpdateClassroomRequest.toCommand(
id: Long,
authenticatedSubject: String
): UpdateClassroomCommand =
UpdateClassroomCommand(
classroomId = id,
title = title,
description = description,
teacherEmails = teacherEmails,
authenticatedSubject = authenticatedSubject,
)

/**
* Maps a join classroom result to a response.
*
* @return a response containing classroom join request details.
*/
fun JoinClassroomResult.toJoinClassroomResponse(): JoinClassroomResponse =
JoinClassroomResponse(
classroomId = classroomId,
Expand All @@ -18,6 +60,51 @@ fun JoinClassroomResult.toJoinClassroomResponse(): JoinClassroomResponse =
message = message,
)

/**
* Maps a classroom domain entity to a classroom response.
*
* @return a classroom response DTO.
*/
fun Classroom.toResponse(): ClassroomResponse =
ClassroomResponse(
id = id,
title = title,
description = description,
joinCode = joinCode ?: "",
teacherUserIds = teacherMemberships.mapNotNull { it.teacher?.id }
)

/**
* Maps a classroom domain entity to a classroom detail response.
*
* @return a detailed classroom response DTO.
*/
fun Classroom.toDetailResponse(): ClassroomDetailResponse =
ClassroomDetailResponse(
id = id,
title = title,
description = description,
joinCode = joinCode ?: "",

teachers = teacherMemberships.mapNotNull { membership ->
val teacher = membership.teacher ?: return@mapNotNull null

ClassroomTeacherResponse(
userId = teacher.id ?: return@mapNotNull null,
email = teacher.email,
)
},

pupils = pupilMemberships.mapNotNull { membership ->
val pupil = membership.pupil ?: return@mapNotNull null

ClassroomPupilResponse(
userId = pupil.id ?: return@mapNotNull null,
displayName = pupil.username ?: "Unknown",
)
}
)

fun OwnedClassroomSummary.toOwnedClassroomResponse(): OwnedClassroomResponse =
OwnedClassroomResponse(
id = id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.*
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
Expand All @@ -27,7 +28,7 @@ class ClassroomController(
.listOwnedClassrooms(currentUser.subject)
.map { it.toOwnedClassroomResponse() }

@GetMapping("/{classroomId}")
@GetMapping("/owned/{classroomId}")
fun getOwnedClassroom(
@AuthenticationPrincipal currentUser: CurrentUser,
@PathVariable classroomId: Long,
Expand All @@ -36,6 +37,32 @@ class ClassroomController(
.getOwnedClassroom(currentUser.subject, classroomId)
.toOwnedClassroomResponse()

/**
* Endpoint for creating a classroom.
*
* @param request The create classroom request containing the classroom information.
* @param currentUser The authenticated user attempting to create the classroom.
* @return the created classroom.
*/
@PostMapping
fun createClassroom(
@RequestBody @Valid request: CreateClassroomRequest,
@AuthenticationPrincipal currentUser: CurrentUser,
): ClassroomResponse =
classroomService.createClassroom(
request.toCommand(currentUser.subject)
).toResponse()

/**
* Retrieves all classrooms the authenticated user is a member of.
*
* @param currentUser the authenticated user extracted from the security context.
* @return a list of classrooms accessible to the user.
*/
@GetMapping
fun getClassrooms(@AuthenticationPrincipal currentUser: CurrentUser): List<ClassroomResponse> =
classroomService.getClassrooms(currentUser.subject).map { it.toResponse() }

/**
* Endpoint for joining a classroom.
*
Expand All @@ -48,5 +75,83 @@ class ClassroomController(
@RequestBody @Valid request: JoinClassroomRequest,
@AuthenticationPrincipal currentUser: CurrentUser,
): JoinClassroomResponse =
classroomService.joinClassroom(request.toCommand(currentUser.subject)).toJoinClassroomResponse()
classroomService.joinClassroom(request.toCommand(currentUser.subject))
.toJoinClassroomResponse()

/**
* Retrieves information about a specific classroom.
*
* @param id the unique id of the classroom.
* @param currentUser the authenticated user extracted form the security context.
* @return classroom details.
*/
@GetMapping("/{id}")
fun getClassroomById(
@PathVariable id: Long,
@AuthenticationPrincipal currentUser: CurrentUser,
): ClassroomDetailResponse =
classroomService.getClassroomById(id, currentUser.subject).toDetailResponse()

/**
* Endpoint for updating classroom information.
*
* @param id the unique id of the classroom to be updated.
* @param request the update classroom request containing the updated classroom data.
* @param currentUser the updated classroom.
*/
@PutMapping("/{id}")
fun updateClassroom(
@PathVariable id: Long,
@RequestBody @Valid request: UpdateClassroomRequest,
@AuthenticationPrincipal currentUser: CurrentUser,
): ClassroomResponse =
classroomService.updateClassroom(
request.toCommand(id, currentUser.subject)
).toResponse()

/**
* Removes a pupil from a classroom.
*
* @param classroomId the id of the classroom the pupil should be removed from.
* @param pupilId the id of the student being removed.
* @param currentUser the authenticates user extracted from the security context.
*/
@DeleteMapping("/{classroomId}/pupils/{pupilId}")
fun removePupil(
@PathVariable classroomId: Long,
@PathVariable pupilId: Long,
@AuthenticationPrincipal currentUser: CurrentUser,
) {
classroomService.removePupil(classroomId, pupilId, currentUser.subject)
}

/**
* Approves a pupils request to join a classroom.
*
* @param classroomId the id of the classroom the pupil is requesting to join.
* @param pupilId the id of the pupil requesting to join.
* @param currentUser the authenticated user extracted from the security context.
*/
@PostMapping("/{classroomId}/pupils/{pupilId}/approve")
fun approvePupil(
@PathVariable classroomId: Long,
@PathVariable pupilId: Long,
@AuthenticationPrincipal currentUser: CurrentUser,
) {
classroomService.approvePupil(classroomId, pupilId, currentUser.subject)
}

/**
* Deletes a classroom.
*
* @param id the id of the classroom being deleted.
* @param currentUser the authenticated user extracted from the security context.
*/
@DeleteMapping("/{id}")
fun deleteClassroom(
@PathVariable id: Long,
@AuthenticationPrincipal currentUser: CurrentUser,
) {
classroomService.deleteClassroom(id, currentUser.subject)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package edu.ntnu.idi.idatt.backend.classroom.api

/**
* DTO for response to retrieving classroom details.
*
* @param id the id of the classroom.
* @param title the title of the classroom.
* @param description the description of the classroom.
* @param joinCode the code for joining the classroom.
* @param teachers a list of the teachers that are members of the classroom.
* @param pupils a list of the pupils that are members of the classroom.
*/
data class ClassroomDetailResponse(
val id: Long,
val title: String,
val description: String?,
val joinCode: String,
val teachers: List<ClassroomTeacherResponse>,
val pupils: List<ClassroomPupilResponse>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.ntnu.idi.idatt.backend.classroom.api

/**
* DTO for response to retrieving pupil information for a classroom.
*
* @param userId the id of a pupil in the classroom.
* @param displayName the name of a pupil in the classroom.
*/
data class ClassroomPupilResponse(
val userId: Long,
val displayName: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.ntnu.idi.idatt.backend.classroom.api

/**
* DTO for response to classroom requests.
*
* @param id 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 title: String,
val description: String?,
val joinCode: String,
val teacherUserIds: List<Long>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package edu.ntnu.idi.idatt.backend.classroom.api

/**
* DTO for response to retrieving teacher information for a classroom.
*
* @param userId the id of a teacher in the classroom.
* @param email the email address of a teacher in the classroom.
*/
data class ClassroomTeacherResponse(
val userId: Long,
val email: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.ntnu.idi.idatt.backend.classroom.api

import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Size

/**
* DTO for request to create a classroom.
*
* @property title the title of the classroom to be created. Must not be blank.
* @property description the optional description of the classroom.
*/
data class CreateClassroomRequest(
@field:NotBlank
@field:Size(max = 100)
val title: String,

val description: String? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package edu.ntnu.idi.idatt.backend.classroom.api

import jakarta.validation.constraints.NotBlank

/**
* DTO for requests to update classroom information.
*
* @property title the title of the classroom. Must not be blank.
* @property description the description of the classroom.
* @property teacherEmails a list of emails of teachers belonging to the classroom.
*/
data class UpdateClassroomRequest(
@field:NotBlank
val title: String,

val description: String? = null,

val teacherEmails: List<String> = emptyList(),
)
Loading