-
Notifications
You must be signed in to change notification settings - Fork 0
feat: notebook entries #41
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e89059c
add NotebookEntry class
sindrelm 124867f
add NotebookEntryRepository
sindrelm dbfd728
add getAllByPupilId method
sindrelm 7bd11cd
Add application layer commands
sindrelm 38f51a0
Add NotebookService class
sindrelm 94ad6c6
Add notebook api endpoints
sindrelm 61f0d84
Add notebook related tests
sindrelm 3f385b4
Add migration schema
sindrelm 89c6aba
Add notebook bootstrap data
sindrelm 790dfa6
Remove lob
sindrelm 251868a
Add missing Kdocs
sindrelm 3cd0a86
Change sourceType to var
sindrelm 2099ec2
Refactor logic into a verifyOwnership helper method
sindrelm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryApiMappings.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 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. | ||
| */ | ||
| 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. | ||
| */ | ||
| 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. | ||
| */ | ||
| 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. | ||
| */ | ||
| fun CurrentUser.toDeleteCommand(notebookEntryId: Long): DeleteNotebookEntryCommand = | ||
| DeleteNotebookEntryCommand( | ||
| currentUser = this, | ||
| notebookEntryId = notebookEntryId, | ||
| ) |
68 changes: 68 additions & 0 deletions
68
src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryController.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| 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. | ||
| */ | ||
| @RestController | ||
| @RequestMapping("/api/v1/users/me/notebook-entries") | ||
| class NotebookEntryController( | ||
| private val notebookService: NotebookService, | ||
| ) { | ||
| /** | ||
| * Returns all notebook entries owned by the authenticated pupil. | ||
| */ | ||
| @GetMapping | ||
| fun getNotebookEntries( | ||
| @AuthenticationPrincipal currentUser: CurrentUser, | ||
| ): List<NotebookEntryResponse> = | ||
| notebookService.getAllNotebookEntries(currentUser).map { it.toResponse() } | ||
|
|
||
| /** | ||
| * Creates a new notebook entry for the authenticated pupil. | ||
| */ | ||
| @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. | ||
| */ | ||
| @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. | ||
| */ | ||
| @DeleteMapping("/{notebookEntryId}") | ||
| @ResponseStatus(HttpStatus.NO_CONTENT) | ||
| fun deleteNotebookEntry( | ||
| @AuthenticationPrincipal currentUser: CurrentUser, | ||
| @PathVariable notebookEntryId: Long, | ||
| ) { | ||
| notebookService.deleteNotebookEntry(currentUser.toDeleteCommand(notebookEntryId)) | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryRequests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| 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. | ||
| */ | ||
| data class CreateNotebookEntryRequest( | ||
| @field:Min(1) | ||
| val classroomId: Long, | ||
| @field:Min(1) | ||
| val stopId: Long, | ||
| @field:NotNull | ||
| val sourceType: NotebookSourceType, | ||
| @field:NotBlank | ||
| val title: String, | ||
| @field:NotBlank | ||
| val content: String, | ||
| ) | ||
|
|
||
| /** | ||
| * Request payload for updating a notebook entry. | ||
| */ | ||
| data class UpdateNotebookEntryRequest( | ||
| @field:NotBlank | ||
| val title: String, | ||
| @field:NotBlank | ||
| val content: String, | ||
| ) | ||
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/api/NotebookEntryResponses.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package edu.ntnu.idi.idatt.backend.notebook.api | ||
|
|
||
| import java.time.Instant | ||
|
|
||
| /** | ||
| * Serialized notebook entry returned by notebook endpoints. | ||
| */ | ||
| 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?, | ||
| ) |
25 changes: 25 additions & 0 deletions
25
...main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/CreateNotebookEntryCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) |
16 changes: 16 additions & 0 deletions
16
...main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/DeleteNotebookEntryCommand.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.