From a05f3f01ecc5906f749f7380a14122ba662ce9d4 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 14:59:02 +0200 Subject: [PATCH 01/20] Add XpEvent class Signed-off-by: sindrelm --- .../idatt/backend/game/xp/domain/XpEvent.kt | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt new file mode 100644 index 00000000..09452261 --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt @@ -0,0 +1,31 @@ +package edu.ntnu.idi.idatt.backend.game.xp.domain + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.Table + +/** + * Defines an XP-awarding event that can be linked to a pupil. + * + * The entity represents the catalog entry for a single awardable XP source, + * such as completing a task or reaching a milestone. Actual pupil awards are + * tracked separately through [PupilXpEvents]. + * + * @property id database identifier + * @property title human-readable label for the event + * @property xpScore amount of XP granted when the event is awarded + */ +@Entity +@Table(name = "xp_events") +class XpEvent( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + @Column(name = "title", nullable = false) + var title: String = "", + @Column(name = "xp_score", nullable = false) + var xpScore: Int = 0, +) From 353b96b63e20bcbf23e636bd1fdb0f703d27e420 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Thu, 23 Apr 2026 14:59:18 +0200 Subject: [PATCH 02/20] Add PupilXpEvent class Signed-off-by: sindrelm --- .../backend/game/xp/domain/PupilXpEventId.kt | 22 ++++++++++++ .../backend/game/xp/domain/PupilXpEvents.kt | 36 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvents.kt diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt new file mode 100644 index 00000000..7c11d0a0 --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt @@ -0,0 +1,22 @@ +package edu.ntnu.idi.idatt.backend.game.xp.domain + +import jakarta.persistence.Column +import jakarta.persistence.Embeddable +import java.io.Serializable + +/** + * Composite identifier for [PupilXpEvents]. + * + * The identifier is made from the pupil user id and the XP event id so the + * same pupil cannot be linked to the same XP event more than once. + * + * @property pupilUserId identifier of the awarded pupil + * @property xpEventId identifier of the awarded XP event definition + */ +@Embeddable +data class PupilXpEventId( + @Column(name = "pupil_user_id") + var pupilUserId: Long = 0, + @Column(name = "xp_event_id") + var xpEventId: Long = 0, +) : Serializable diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvents.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvents.kt new file mode 100644 index 00000000..7dd9becc --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvents.kt @@ -0,0 +1,36 @@ +package edu.ntnu.idi.idatt.backend.game.xp.domain + +import edu.ntnu.idi.idatt.backend.iam.domain.User +import jakarta.persistence.EmbeddedId +import jakarta.persistence.Entity +import jakarta.persistence.FetchType +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne +import jakarta.persistence.MapsId +import jakarta.persistence.Table + +/** + * Records that a pupil has received a specific XP event. + * + * This join entity makes XP event awards idempotent by using a composite key + * over the pupil and event identifiers. One row therefore represents one + * awarded event for one pupil. + * + * @property id composite identifier for the awarded event + * @property pupil pupil who received the event + * @property xpEvent XP event definition that was awarded + */ +@Entity +@Table(name = "pupil_xp_events") +class PupilXpEvents( + @EmbeddedId + var id: PupilXpEventId = PupilXpEventId(), + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @MapsId("pupilUserId") + @JoinColumn(name = "pupil_user_id", nullable = false) + var pupil: User? = null, + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @MapsId("xpEventId") + @JoinColumn(name = "xp_event_id", nullable = false) + var xpEvent: XpEvent? = null, +) From f7423ea8bbb5e823be5e3b33e36e04c3fd7ab703 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 09:26:27 +0200 Subject: [PATCH 03/20] Add XpEventRepository Signed-off-by: sindrelm --- .../xp/infrastructure/XpEventRepository.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt new file mode 100644 index 00000000..0f9de80a --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt @@ -0,0 +1,29 @@ +package edu.ntnu.idi.idatt.backend.game.xp.infrastructure + +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.query.Param +import org.springframework.stereotype.Repository + +/** + * Repository for persisted [XpEvent] definitions and awarded event lookups. + */ +@Repository +interface XpEventRepository : JpaRepository { + /** + * Returns all XP events that have been awarded to one pupil. + * + * @param pupilId identifier of the pupil whose awarded XP events should be fetched. + * @return all XP event definitions linked to the pupil through [edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvents]. + */ + @Query( + """ + SELECT xpEvent + FROM PupilXpEvents awardedEvent + JOIN awardedEvent.xpEvent xpEvent + WHERE awardedEvent.id.pupilUserId = :pupilId + """, + ) + fun findAllByPupilId(@Param("pupilId") pupilId: Long): List +} From 08f790eab253727f9a0e18fff79cfd89d30d16fe Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 09:34:14 +0200 Subject: [PATCH 04/20] Add PupilXpEventRepository Signed-off-by: sindrelm --- .../infrastructure/PupilXpEventRepository.kt | 29 +++++++++++++++++++ .../xp/infrastructure/XpEventRepository.kt | 22 ++------------ 2 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt new file mode 100644 index 00000000..80c566e1 --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt @@ -0,0 +1,29 @@ +package edu.ntnu.idi.idatt.backend.game.xp.infrastructure + +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvents +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository + +/** + * Repository for awarded XP events linked to pupils. + */ +@Repository +interface PupilXpEventRepository : JpaRepository { + /** + * Returns all awarded XP event rows for one pupil. + * + * @param pupilUserId identifier of the pupil. + * @return awarded XP event links for the given pupil. + */ + fun findAllByIdPupilUserId(pupilUserId: Long): List + + /** + * Checks whether one XP event has already been awarded to one pupil. + * + * @param pupilUserId identifier of the pupil. + * @param xpEventId identifier of the XP event definition. + * @return `true` when the composite award row already exists. + */ + fun existsByIdPupilUserIdAndIdXpEventId(pupilUserId: Long, xpEventId: Long): Boolean +} diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt index 0f9de80a..171267a5 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt @@ -2,28 +2,10 @@ package edu.ntnu.idi.idatt.backend.game.xp.infrastructure import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent import org.springframework.data.jpa.repository.JpaRepository -import org.springframework.data.jpa.repository.Query -import org.springframework.data.repository.query.Param import org.springframework.stereotype.Repository /** - * Repository for persisted [XpEvent] definitions and awarded event lookups. + * Repository for persisted [XpEvent] definitions. */ @Repository -interface XpEventRepository : JpaRepository { - /** - * Returns all XP events that have been awarded to one pupil. - * - * @param pupilId identifier of the pupil whose awarded XP events should be fetched. - * @return all XP event definitions linked to the pupil through [edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvents]. - */ - @Query( - """ - SELECT xpEvent - FROM PupilXpEvents awardedEvent - JOIN awardedEvent.xpEvent xpEvent - WHERE awardedEvent.id.pupilUserId = :pupilId - """, - ) - fun findAllByPupilId(@Param("pupilId") pupilId: Long): List -} +interface XpEventRepository : JpaRepository From 88bb7f968cd9a3dffcc347cc4341b7e9842b4e41 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 09:48:12 +0200 Subject: [PATCH 05/20] Add repeatable and code field Signed-off-by: sindrelm --- .../idi/idatt/backend/game/xp/domain/XpEvent.kt | 9 +++++++++ .../idatt/backend/game/xp/domain/XpEventCode.kt | 15 +++++++++++++++ .../game/xp/infrastructure/XpEventRepository.kt | 11 ++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEventCode.kt diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt index 09452261..d96d6174 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt @@ -2,6 +2,8 @@ package edu.ntnu.idi.idatt.backend.game.xp.domain import jakarta.persistence.Column import jakarta.persistence.Entity +import jakarta.persistence.EnumType +import jakarta.persistence.Enumerated import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType import jakarta.persistence.Id @@ -15,8 +17,10 @@ import jakarta.persistence.Table * tracked separately through [PupilXpEvents]. * * @property id database identifier + * @property code stable business key used to identify the event in code * @property title human-readable label for the event * @property xpScore amount of XP granted when the event is awarded + * @property repeatable whether the same pupil may be awarded the event more than once */ @Entity @Table(name = "xp_events") @@ -24,8 +28,13 @@ class XpEvent( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) var id: Long? = null, + @Enumerated(EnumType.STRING) + @Column(name = "code", nullable = false, unique = true, length = 100) + var code: XpEventCode = XpEventCode.TASK_COMPLETED, @Column(name = "title", nullable = false) var title: String = "", @Column(name = "xp_score", nullable = false) var xpScore: Int = 0, + @Column(name = "repeatable", nullable = false) + var repeatable: Boolean = false, ) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEventCode.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEventCode.kt new file mode 100644 index 00000000..f5c38a0e --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEventCode.kt @@ -0,0 +1,15 @@ +package edu.ntnu.idi.idatt.backend.game.xp.domain + +/** + * Stable identifiers for XP event definitions. + * + * The enum is used as the canonical business key for XP awards, while the + * corresponding [XpEvent] rows still hold presentation fields such as title + * and score in the database. + */ +enum class XpEventCode { + STOP_COMPLETED, + MEDAL_AWARDED, + NOTEBOOK_ENTRY_CREATED, + UPDATE_AVATAR, +} diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt index 171267a5..5f98a2b2 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt @@ -1,5 +1,6 @@ package edu.ntnu.idi.idatt.backend.game.xp.infrastructure +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository @@ -8,4 +9,12 @@ import org.springframework.stereotype.Repository * Repository for persisted [XpEvent] definitions. */ @Repository -interface XpEventRepository : JpaRepository +interface XpEventRepository : JpaRepository { + /** + * Finds one XP event definition by its stable business code. + * + * @param code canonical XP event identifier. + * @return matching event definition, or `null` when absent. + */ + fun findByCode(code: XpEventCode): XpEvent? +} From c82e84ba002ece27b8985bdac6786dd4567d4e60 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 10:13:33 +0200 Subject: [PATCH 06/20] Add XpService Signed-off-by: sindrelm --- .../backend/game/xp/application/XpService.kt | 70 +++++++++++++++++++ .../{PupilXpEvents.kt => PupilXpEvent.kt} | 12 ++-- .../backend/game/xp/domain/PupilXpEventId.kt | 6 +- .../idatt/backend/game/xp/domain/XpEvent.kt | 2 +- .../infrastructure/PupilXpEventRepository.kt | 15 +++- 5 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt rename src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/{PupilXpEvents.kt => PupilXpEvent.kt} (70%) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt new file mode 100644 index 00000000..af69fb61 --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt @@ -0,0 +1,70 @@ +package edu.ntnu.idi.idatt.backend.game.xp.application + +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.PupilXpEventRepository +import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.XpEventRepository +import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver +import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser +import org.springframework.http.HttpStatus +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import org.springframework.web.server.ResponseStatusException + +/** + * Application service for awarding XP events to pupils. + * + * The service resolves the authenticated pupil, loads the configured XP event + * by its stable [XpEventCode], and either creates or updates the pupil's + * progress row depending on whether the event is repeatable. + * + * @property pupilXpEventRepository repository for awarded pupil XP rows + * @property xpEventRepository repository for XP event definitions + * @property currentUserResolver resolver for the authenticated pupil user + */ +@Service +class XpService( + private val pupilXpEventRepository: PupilXpEventRepository, + private val xpEventRepository: XpEventRepository, + private val currentUserResolver: CurrentUserResolver, +) { + /** + * Awards one XP event to the authenticated pupil. + * + * For non-repeatable events, duplicate awards are ignored. For repeatable + * events, the stored completion count is incremented on the existing + * pupil-event row. + * + * @param currentUser authenticated pupil receiving the award + * @param eventCode stable code for the XP event to award + * @throws ResponseStatusException when the XP event definition does not exist + */ + @Transactional + fun awardXpEvent(currentUser: CurrentUser, eventCode: XpEventCode) { + val pupil = currentUserResolver.requirePupil(currentUser.subject) + val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" } + + val xpEvent = xpEventRepository.findByCode(eventCode) + ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "XP event was not found") + val xpEventId = requireNotNull(xpEvent.id) { "XP event is missing an id" } + + val pupilXpEvent = pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(pupilId, xpEventId) + + if (pupilXpEvent != null) { + if (!xpEvent.repeatable) return + + pupilXpEvent.completionCount += 1 + pupilXpEventRepository.save(pupilXpEvent) + return + } + + pupilXpEventRepository.save( + PupilXpEvent( + id = PupilXpEventId(pupilUserId = pupilId, xpEventId = xpEventId), + pupil = pupil, + xpEvent = xpEvent, + ), + ) + } +} diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvents.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvent.kt similarity index 70% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvents.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvent.kt index 7dd9becc..d91c2433 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvents.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvent.kt @@ -1,6 +1,7 @@ package edu.ntnu.idi.idatt.backend.game.xp.domain import edu.ntnu.idi.idatt.backend.iam.domain.User +import jakarta.persistence.Column import jakarta.persistence.EmbeddedId import jakarta.persistence.Entity import jakarta.persistence.FetchType @@ -12,17 +13,18 @@ import jakarta.persistence.Table /** * Records that a pupil has received a specific XP event. * - * This join entity makes XP event awards idempotent by using a composite key - * over the pupil and event identifiers. One row therefore represents one - * awarded event for one pupil. + * This join entity keeps one row per pupil and XP event by using a composite + * key over the pupil and event identifiers. For repeatable events, the same + * row tracks how many times the pupil has completed the event. * * @property id composite identifier for the awarded event * @property pupil pupil who received the event * @property xpEvent XP event definition that was awarded + * @property completionCount number of times the pupil has completed the event */ @Entity @Table(name = "pupil_xp_events") -class PupilXpEvents( +class PupilXpEvent( @EmbeddedId var id: PupilXpEventId = PupilXpEventId(), @ManyToOne(fetch = FetchType.LAZY, optional = false) @@ -33,4 +35,6 @@ class PupilXpEvents( @MapsId("xpEventId") @JoinColumn(name = "xp_event_id", nullable = false) var xpEvent: XpEvent? = null, + @Column(name = "completion_count", nullable = false) + var completionCount: Int = 1, ) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt index 7c11d0a0..1c3d497a 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt @@ -5,10 +5,10 @@ import jakarta.persistence.Embeddable import java.io.Serializable /** - * Composite identifier for [PupilXpEvents]. + * Composite identifier for [PupilXpEvent]. * - * The identifier is made from the pupil user id and the XP event id so the - * same pupil cannot be linked to the same XP event more than once. + * The identifier is made from the pupil user id and the XP event id so one + * row can represent one pupil-event pair. * * @property pupilUserId identifier of the awarded pupil * @property xpEventId identifier of the awarded XP event definition diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt index d96d6174..556dec12 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt @@ -14,7 +14,7 @@ import jakarta.persistence.Table * * The entity represents the catalog entry for a single awardable XP source, * such as completing a task or reaching a milestone. Actual pupil awards are - * tracked separately through [PupilXpEvents]. + * tracked separately through [PupilXpEvent]. * * @property id database identifier * @property code stable business key used to identify the event in code diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt index 80c566e1..321cb03e 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt @@ -1,7 +1,7 @@ package edu.ntnu.idi.idatt.backend.game.xp.infrastructure import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvents +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository @@ -9,14 +9,14 @@ import org.springframework.stereotype.Repository * Repository for awarded XP events linked to pupils. */ @Repository -interface PupilXpEventRepository : JpaRepository { +interface PupilXpEventRepository : JpaRepository { /** * Returns all awarded XP event rows for one pupil. * * @param pupilUserId identifier of the pupil. * @return awarded XP event links for the given pupil. */ - fun findAllByIdPupilUserId(pupilUserId: Long): List + fun findAllByIdPupilUserId(pupilUserId: Long): List /** * Checks whether one XP event has already been awarded to one pupil. @@ -26,4 +26,13 @@ interface PupilXpEventRepository : JpaRepository * @return `true` when the composite award row already exists. */ fun existsByIdPupilUserIdAndIdXpEventId(pupilUserId: Long, xpEventId: Long): Boolean + + /** + * Finds the awarded XP row for one pupil-event pair. + * + * @param pupilUserId identifier of the pupil. + * @param xpEventId identifier of the XP event definition. + * @return awarded XP row, or `null` when absent. + */ + fun findByIdPupilUserIdAndIdXpEventId(pupilUserId: Long, xpEventId: Long): PupilXpEvent? } From 90d227f47c62ca6524df251bab7e4d4b6fc5765e Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 10:32:33 +0200 Subject: [PATCH 07/20] Add updatePupilGameProgressLevel function Signed-off-by: sindrelm --- .../backend/game/xp/application/XpService.kt | 59 ++++++++++++++++--- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt index af69fb61..f138bb7f 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt @@ -1,5 +1,6 @@ package edu.ntnu.idi.idatt.backend.game.xp.application +import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode @@ -11,6 +12,7 @@ import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import org.springframework.web.server.ResponseStatusException +import java.time.Instant /** * Application service for awarding XP events to pupils. @@ -21,12 +23,14 @@ import org.springframework.web.server.ResponseStatusException * * @property pupilXpEventRepository repository for awarded pupil XP rows * @property xpEventRepository repository for XP event definitions + * @property pupilGameProgressRepository repository for classroom-scoped pupil game progress * @property currentUserResolver resolver for the authenticated pupil user */ @Service class XpService( private val pupilXpEventRepository: PupilXpEventRepository, private val xpEventRepository: XpEventRepository, + private val pupilGameProgressRepository: PupilGameProgressRepository, private val currentUserResolver: CurrentUserResolver, ) { /** @@ -56,15 +60,56 @@ class XpService( pupilXpEvent.completionCount += 1 pupilXpEventRepository.save(pupilXpEvent) - return + } else { + pupilXpEventRepository.save( + PupilXpEvent( + id = PupilXpEventId(pupilUserId = pupilId, xpEventId = xpEventId), + pupil = pupil, + xpEvent = xpEvent, + ), + ) + } + updatePupilGameProgressLevel(currentUser) + } + + /** + * Recomputes the authenticated pupil's classroom-scoped XP total from awarded XP events. + * + * The method sums the configured XP score for each awarded event multiplied + * by its completion count, then stores the updated XP snapshot on the + * pupil's aggregated classroom progress record. + * + * @param currentUser authenticated pupil whose progress should be recalculated + * @throws ResponseStatusException when no game progress row exists for the active classroom + */ + @Transactional + private fun updatePupilGameProgressLevel(currentUser: CurrentUser) { + val pupil = currentUserResolver.requirePupil(currentUser.subject) + val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" } + + val classroom = currentUserResolver.getClassroomByUser(currentUser) + val classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" } + + val pupilGameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) + ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "PupilGameProgress was not found") + + val pupilXpEvents = pupilXpEventRepository.findAllByIdPupilUserId(pupilId) + val totalXp = pupilXpEvents.sumOf { pupilXpEvent -> + val xpEvent = requireNotNull(pupilXpEvent.xpEvent) { + "Pupil XP event is missing its XP event reference" + } + xpEvent.xpScore * pupilXpEvent.completionCount } - pupilXpEventRepository.save( - PupilXpEvent( - id = PupilXpEventId(pupilUserId = pupilId, xpEventId = xpEventId), - pupil = pupil, - xpEvent = xpEvent, - ), + pupilGameProgress.updateProgress( + totalScore = pupilGameProgress.totalScore, + totalXp = totalXp, + currentLevel = pupilGameProgress.currentLevel, + completedStopsCount = pupilGameProgress.completedStopsCount, + completedTasksCount = pupilGameProgress.completedTasksCount, + finalBossCompleted = pupilGameProgress.finalBossCompleted, + lastActivityAt = Instant.now(), ) + pupilGameProgressRepository.save(pupilGameProgress) } } From ffd60f5fc107f64636a03eb1893f676e20cc14f1 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 10:34:51 +0200 Subject: [PATCH 08/20] Add current_level calculation Signed-off-by: sindrelm --- .../backend/game/xp/application/XpService.kt | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt index f138bb7f..bad8ff70 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt @@ -33,6 +33,10 @@ class XpService( private val pupilGameProgressRepository: PupilGameProgressRepository, private val currentUserResolver: CurrentUserResolver, ) { + companion object { + private const val XP_PER_LEVEL = 150 + } + /** * Awards one XP event to the authenticated pupil. * @@ -76,8 +80,9 @@ class XpService( * Recomputes the authenticated pupil's classroom-scoped XP total from awarded XP events. * * The method sums the configured XP score for each awarded event multiplied - * by its completion count, then stores the updated XP snapshot on the - * pupil's aggregated classroom progress record. + * by its completion count, derives the matching level from the total XP, + * then stores the updated progression snapshot on the pupil's aggregated + * classroom progress record. * * @param currentUser authenticated pupil whose progress should be recalculated * @throws ResponseStatusException when no game progress row exists for the active classroom @@ -100,11 +105,12 @@ class XpService( } xpEvent.xpScore * pupilXpEvent.completionCount } + val currentLevel = calculateLevelFromXp(totalXp) pupilGameProgress.updateProgress( - totalScore = pupilGameProgress.totalScore, + totalScore = totalXp, totalXp = totalXp, - currentLevel = pupilGameProgress.currentLevel, + currentLevel = currentLevel, completedStopsCount = pupilGameProgress.completedStopsCount, completedTasksCount = pupilGameProgress.completedTasksCount, finalBossCompleted = pupilGameProgress.finalBossCompleted, @@ -112,4 +118,16 @@ class XpService( ) pupilGameProgressRepository.save(pupilGameProgress) } + + /** + * Maps accumulated XP to a 1-based level. + * + * Each full [XP_PER_LEVEL] XP increases the pupil's level by one, while + * pupils with no XP remain at level 1. + * + * @param totalXp classroom-scoped accumulated XP. + * @return derived 1-based level. + */ + private fun calculateLevelFromXp(totalXp: Int): Int = + (totalXp / XP_PER_LEVEL) + 1 } From 78b769ffd70f226c5c0b889ac840319987ad6336 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 10:38:11 +0200 Subject: [PATCH 09/20] Add db migration schema Signed-off-by: sindrelm --- .../db/migration/V21__create_xp_tables.sql | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/resources/db/migration/V21__create_xp_tables.sql diff --git a/src/main/resources/db/migration/V21__create_xp_tables.sql b/src/main/resources/db/migration/V21__create_xp_tables.sql new file mode 100644 index 00000000..10a02809 --- /dev/null +++ b/src/main/resources/db/migration/V21__create_xp_tables.sql @@ -0,0 +1,22 @@ +CREATE TABLE xp_events ( + id BIGINT NOT NULL AUTO_INCREMENT, + code VARCHAR(100) NOT NULL, + title VARCHAR(255) NOT NULL, + xp_score INT NOT NULL, + repeatable BOOLEAN NOT NULL DEFAULT FALSE, + CONSTRAINT pk_xp_events PRIMARY KEY (id), + CONSTRAINT uk_xp_events_code UNIQUE (code) +); + +CREATE TABLE pupil_xp_events ( + pupil_user_id BIGINT NOT NULL, + xp_event_id BIGINT NOT NULL, + completion_count INT NOT NULL DEFAULT 1, + CONSTRAINT pk_pupil_xp_events PRIMARY KEY (pupil_user_id, xp_event_id), + CONSTRAINT fk_pupil_xp_events_pupil + FOREIGN KEY (pupil_user_id) REFERENCES users (id) ON DELETE CASCADE, + CONSTRAINT fk_pupil_xp_events_xp_event + FOREIGN KEY (xp_event_id) REFERENCES xp_events (id) ON DELETE CASCADE +); + +CREATE INDEX idx_pupil_xp_events_xp_event ON pupil_xp_events (xp_event_id); From 9ea13cae6c70d29f22549232f936b3156440fa61 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 10:50:41 +0200 Subject: [PATCH 10/20] Add tests Signed-off-by: sindrelm --- .../idatt/backend/game/xp/domain/XpEvent.kt | 2 +- .../idatt/backend/game/xp/XpServiceTests.kt | 202 ++++++++++++++++++ 2 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/edu/ntnu/idi/idatt/backend/game/xp/XpServiceTests.kt diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt index 556dec12..00b38c1d 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt @@ -30,7 +30,7 @@ class XpEvent( var id: Long? = null, @Enumerated(EnumType.STRING) @Column(name = "code", nullable = false, unique = true, length = 100) - var code: XpEventCode = XpEventCode.TASK_COMPLETED, + var code: XpEventCode, @Column(name = "title", nullable = false) var title: String = "", @Column(name = "xp_score", nullable = false) diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/xp/XpServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/xp/XpServiceTests.kt new file mode 100644 index 00000000..ed29f1b6 --- /dev/null +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/xp/XpServiceTests.kt @@ -0,0 +1,202 @@ +package edu.ntnu.idi.idatt.backend.game.xp + +import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom +import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress +import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId +import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.PupilXpEventRepository +import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.XpEventRepository +import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver +import edu.ntnu.idi.idatt.backend.iam.domain.User +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.security.CurrentUser +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertSame +import org.junit.jupiter.api.Test +import org.mockito.ArgumentCaptor +import org.mockito.Mockito + +class XpServiceTests { + @Test + fun `award xp event creates pupil event row and updates progress for first award`() { + val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) + val pupilXpEventRepository = Mockito.mock(PupilXpEventRepository::class.java) + val xpEventRepository = Mockito.mock(XpEventRepository::class.java) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = XpService( + pupilXpEventRepository, + xpEventRepository, + pupilGameProgressRepository, + currentUserResolver, + ) + val currentUser = currentUser() + val pupil = pupil() + val classroom = classroom() + val xpEvent = xpEvent(id = 10L, xpScore = 75, repeatable = false) + val progress = gameProgress(pupil, classroom) + + Mockito.`when`(currentUserResolver.requirePupil(currentUser.subject)).thenReturn(pupil) + Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) + Mockito.`when`(xpEventRepository.findByCode(XpEventCode.STOP_COMPLETED)).thenReturn(xpEvent) + Mockito.`when`(pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(1L, 10L)).thenReturn(null) + Mockito.`when`(pupilXpEventRepository.findAllByIdPupilUserId(1L)).thenReturn( + listOf( + PupilXpEvent( + id = PupilXpEventId(pupilUserId = 1L, xpEventId = 10L), + pupil = pupil, + xpEvent = xpEvent, + completionCount = 1, + ), + ), + ) + Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(1L, 5L)).thenReturn(progress) + + service.awardXpEvent(currentUser, XpEventCode.STOP_COMPLETED) + + val captor = ArgumentCaptor.forClass(PupilXpEvent::class.java) + Mockito.verify(pupilXpEventRepository).save(captor.capture()) + val savedAward = captor.value + assertEquals(1L, savedAward.id.pupilUserId) + assertEquals(10L, savedAward.id.xpEventId) + assertSame(pupil, savedAward.pupil) + assertSame(xpEvent, savedAward.xpEvent) + assertEquals(1, savedAward.completionCount) + + assertEquals(75, progress.totalXp) + assertEquals(1, progress.currentLevel) + Mockito.verify(pupilGameProgressRepository).save(progress) + } + + @Test + fun `award xp event ignores duplicate non repeatable award`() { + val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) + val pupilXpEventRepository = Mockito.mock(PupilXpEventRepository::class.java) + val xpEventRepository = Mockito.mock(XpEventRepository::class.java) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = XpService( + pupilXpEventRepository, + xpEventRepository, + pupilGameProgressRepository, + currentUserResolver, + ) + val currentUser = currentUser() + val pupil = pupil() + val existingAward = PupilXpEvent( + id = PupilXpEventId(pupilUserId = 1L, xpEventId = 10L), + pupil = pupil, + xpEvent = xpEvent(id = 10L, xpScore = 75, repeatable = false), + completionCount = 1, + ) + + Mockito.`when`(currentUserResolver.requirePupil(currentUser.subject)).thenReturn(pupil) + Mockito.`when`(xpEventRepository.findByCode(XpEventCode.STOP_COMPLETED)).thenReturn(existingAward.xpEvent) + Mockito.`when`(pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(1L, 10L)).thenReturn(existingAward) + + service.awardXpEvent(currentUser, XpEventCode.STOP_COMPLETED) + + Mockito.verify(pupilXpEventRepository, Mockito.never()).save(Mockito.any(PupilXpEvent::class.java)) + Mockito.verifyNoInteractions(pupilGameProgressRepository) + } + + @Test + fun `award xp event increments repeatable award and recalculates level from total xp`() { + val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) + val pupilXpEventRepository = Mockito.mock(PupilXpEventRepository::class.java) + val xpEventRepository = Mockito.mock(XpEventRepository::class.java) + val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val service = XpService( + pupilXpEventRepository, + xpEventRepository, + pupilGameProgressRepository, + currentUserResolver, + ) + val currentUser = currentUser() + val pupil = pupil() + val classroom = classroom() + val xpEvent = xpEvent(id = 10L, xpScore = 80, repeatable = true) + val existingAward = PupilXpEvent( + id = PupilXpEventId(pupilUserId = 1L, xpEventId = 10L), + pupil = pupil, + xpEvent = xpEvent, + completionCount = 1, + ) + val progress = gameProgress( + pupil = pupil, + classroom = classroom, + totalXp = 80, + currentLevel = 1, + ) + + Mockito.`when`(currentUserResolver.requirePupil(currentUser.subject)).thenReturn(pupil) + Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) + Mockito.`when`(xpEventRepository.findByCode(XpEventCode.STOP_COMPLETED)).thenReturn(xpEvent) + Mockito.`when`(pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(1L, 10L)).thenReturn(existingAward) + Mockito.`when`(pupilXpEventRepository.findAllByIdPupilUserId(1L)).thenReturn(listOf(existingAward)) + Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(1L, 5L)).thenReturn(progress) + + service.awardXpEvent(currentUser, XpEventCode.STOP_COMPLETED) + + assertEquals(2, existingAward.completionCount) + Mockito.verify(pupilXpEventRepository).save(existingAward) + assertEquals(160, progress.totalXp) + assertEquals(2, progress.currentLevel) + Mockito.verify(pupilGameProgressRepository).save(progress) + } + + private fun currentUser(): CurrentUser = + CurrentUser( + subject = "1", + portalType = "pupil", + email = null, + authorities = setOf("ROLE_PUPIL"), + ) + + private fun pupil(): User = + User( + id = 1L, + username = "ada", + passwordHash = "hash", + role = UserRole.PUPIL, + status = UserStatus.ACTIVE, + ) + + private fun classroom(): Classroom = + Classroom( + id = 5L, + ownerTeacherId = 9L, + title = "XP Classroom", + ) + + private fun xpEvent(id: Long, xpScore: Int, repeatable: Boolean): XpEvent = + XpEvent( + id = id, + code = XpEventCode.STOP_COMPLETED, + title = "Stop complete", + xpScore = xpScore, + repeatable = repeatable, + ) + + private fun gameProgress( + pupil: User, + classroom: Classroom, + totalXp: Int = 0, + currentLevel: Int = 1, + ): PupilGameProgress = + PupilGameProgress( + id = PupilGameProgressId( + pupilUserId = requireNotNull(pupil.id), + classroomId = classroom.id, + ), + pupil = pupil, + classroom = classroom, + totalScore = 0, + totalXp = totalXp, + currentLevel = currentLevel, + ) +} From cf6225dd845cab9bf24af9e271fde996cbef50c5 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 11:00:39 +0200 Subject: [PATCH 11/20] Add bootstrap data Signed-off-by: sindrelm --- .../backend/bootstrap/DevDataBootstrap.kt | 7 ++ .../idatt/backend/bootstrap/XpBootstrap.kt | 86 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt index a7611edd..0335fc93 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/DevDataBootstrap.kt @@ -62,6 +62,7 @@ class DevDataBootstrap( private val mapStopBootstrap: MapStopBootstrap, private val medalBootstrap: MedalBootstrap, private val taskBootstrap: TaskBootstrap, + private val xpBootstrap: XpBootstrap, private val pupilProgressSeedingService: PupilProgressSeedingService, private val pupilStopProgressRepository: PupilStopProgressRepository, ) : ApplicationRunner { @@ -172,6 +173,12 @@ class DevDataBootstrap( mapStopBootstrap.seedAllMapStops() medalBootstrap.seedAllMedals() + xpBootstrap.seedAllXpEvents() + xpBootstrap.seedPupilXpEvent(pupilAda) + xpBootstrap.seedPupilXpEvent(pupilAda) + xpBootstrap.seedPupilXpEvent(pupilAda) + xpBootstrap.seedPupilXpEvent(pupilAda) + xpBootstrap.seedPupilXpEvent(pupilAda) val activeStops = taskBootstrap.seedAllTasks(classroom = sampleClassroom, teacher = teacher) seedNotebookEntries( classroom = sampleClassroom, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt new file mode 100644 index 00000000..3641b11d --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt @@ -0,0 +1,86 @@ +package edu.ntnu.idi.idatt.backend.bootstrap + +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent +import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.PupilXpEventRepository +import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.XpEventRepository +import edu.ntnu.idi.idatt.backend.iam.domain.User +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty +import org.springframework.stereotype.Component + +@Component +@ConditionalOnProperty(name = ["app.bootstrap.enabled"], havingValue = "true") +class XpBootstrap( + private val xpEventRepository: XpEventRepository, + private val pupilXpEventRepository: PupilXpEventRepository, +) { + fun seedAllXpEvents() { + if (xpEventRepository.findByCode(XpEventCode.STOP_COMPLETED) != null) { + return + } + + xpEventRepository.save( + XpEvent( + code = XpEventCode.STOP_COMPLETED, + title = "Stopp fullfort", + xpScore = 75, + repeatable = true, + ), + ) + + xpEventRepository.save( + XpEvent( + code = XpEventCode.MEDAL_REWARDED, + title = "Medalje låst opp", + xpScore = 75, + repeatable = true, + ), + ) + + xpEventRepository.save( + XpEvent( + code = XpEventCode.NOTEBOOK_ENTRY_CREATED, + title = "La inn notat", + xpScore = 10, + repeatable = true, + ), + ) + + xpEventRepository.save( + XpEvent( + code = XpEventCode.UPDATE_AVATAR, + title = "Oppdatert avatar for første gang", + xpScore = 25, + repeatable = false, + ), + ) + } + + fun seedPupilXpEvent(pupil: User) { + val pupilId = requireNotNull(pupil.id) { "Seeded pupil must have an id" } + val xpEvent = xpEventRepository.findByCode(XpEventCode.STOP_COMPLETED) + ?: throw IllegalStateException("XP event ${XpEventCode.STOP_COMPLETED} must be seeded before pupil XP rows") + val xpEventId = requireNotNull(xpEvent.id) { "Seeded XP event must have an id" } + + val existingPupilXpEvent = pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(pupilId, xpEventId) + if (existingPupilXpEvent != null) { + existingPupilXpEvent.completionCount += 1 + pupilXpEventRepository.save(existingPupilXpEvent) + return + } + + pupilXpEventRepository.save( + PupilXpEvent( + id = PupilXpEventId( + pupilUserId = pupilId, + xpEventId = xpEventId, + ), + pupil = pupil, + xpEvent = xpEvent, + completionCount = 1, + ), + ) + } +} From 560ba30bc6d09c19b109fc39a02bd51829a85e87 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 11:30:58 +0200 Subject: [PATCH 12/20] Refactor services to use CurrentUser instead of pupilId Signed-off-by: sindrelm --- .../api/PupilStopProgressController.kt | 2 +- .../application/PupilStopProgressService.kt | 14 ++-- .../backend/game/tasks/api/TaskController.kt | 2 +- .../backend/game/tasks/api/TaskRequests.kt | 4 +- .../game/tasks/application/TaskModels.kt | 3 +- .../game/tasks/application/TaskService.kt | 18 ++--- .../mapstops/PupilStopProgressServiceTests.kt | 66 ++++++++++++++----- 7 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/api/PupilStopProgressController.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/api/PupilStopProgressController.kt index 9fc5a1ee..83e16c37 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/api/PupilStopProgressController.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/api/PupilStopProgressController.kt @@ -99,7 +99,7 @@ class PupilStopProgressController( ): StopProgressResponse { currentUserResolver.requirePupil(currentUser.subject) return progressService - .updateProgress(pupilUserId, classroomId, stopId, request.toCommand()) + .updateProgress(currentUser, classroomId, stopId, request.toCommand()) .toStopProgressResponse() } } diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt index f2680a41..860b0fd6 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt @@ -5,6 +5,8 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.domain.PupilStopProgress import edu.ntnu.idi.idatt.backend.game.mapstops.domain.StopProgressStatus import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.PupilStopProgressRepository +import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver +import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser import edu.ntnu.idi.idatt.backend.shared.events.DomainEventPublisher import edu.ntnu.idi.idatt.backend.shared.events.MapStopCompletedEvent import edu.ntnu.idi.idatt.backend.shared.events.PupilGameCompletedEvent @@ -21,14 +23,14 @@ import java.time.Instant * * @property progressRepository JPA repository for pupil stop progress records * @property mapStopRepository Repository for map stop definitions - * @property medalService Service for awarding medals upon stop completion - * @property userRepository Repository for user lookup - * @property classroomRepository Repository for classroom lookup + * @property currentUserResolver resolver for the authenticated pupil user + * @property domainEventPublisher publisher for stop progression domain events */ @Service class PupilStopProgressService( private val progressRepository: PupilStopProgressRepository, private val mapStopRepository: MapStopsRepository, + private val currentUserResolver: CurrentUserResolver, private val domainEventPublisher: DomainEventPublisher, ) { /** @@ -68,7 +70,7 @@ class PupilStopProgressService( * is higher. If the stop is transitioned to [StopProgressStatus.COMPLETED], the next active * stop is automatically unlocked by transitioning it to [StopProgressStatus.NOT_STARTED]. * - * @param pupilUserId pupil whose progress to update + * @param currentUser authenticated pupil whose progress to update * @param classroomId classroom to scope the update to * @param stopId map stop to update progress for * @param command update payload containing the fields to mutate @@ -79,11 +81,13 @@ class PupilStopProgressService( */ @Transactional fun updateProgress( - pupilUserId: Long, + currentUser: CurrentUser, classroomId: Long, stopId: Long, command: UpdateStopProgressCommand, ): PupilStopProgress { + val pupil = currentUserResolver.requirePupil(currentUser.subject) + val pupilUserId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" } val progress = getProgressForStop(pupilUserId, classroomId, stopId) val activeStops = mapStopRepository.findAllByIsActiveTrueOrderByDisplayOrderAsc() diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskController.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskController.kt index 9be6c621..839eee13 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskController.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskController.kt @@ -60,7 +60,7 @@ class TaskController( @RequestBody @Valid request: SubmitStopAnswersRequest, @AuthenticationPrincipal currentUser: CurrentUser, ): StopSubmissionResponse = - taskService.submitStopAnswers(request.toCommand(classroomId, currentUser.subject)).toResponse() + taskService.submitStopAnswers(request.toCommand(classroomId, currentUser)).toResponse() /** * Creates a new draft task owned by the classroom's teacher. diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskRequests.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskRequests.kt index 3479fcce..1ad78b64 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskRequests.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/api/TaskRequests.kt @@ -218,9 +218,9 @@ private fun LocalizedOptionalTextRequest.toLocalizedOptionalText(): LocalizedOpt fun SubmitStopAnswersRequest.toCommand( classroomId: Long, - authenticatedSubject: String, + currentUser: CurrentUser, ) = edu.ntnu.idi.idatt.backend.game.tasks.application.SubmitStopAnswersCommand( - authenticatedSubject = authenticatedSubject, + currentUser = currentUser, classroomId = classroomId, stopId = stopId, taskAnswers = taskAnswers.map { taskAnswer -> diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskModels.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskModels.kt index f4909c43..69a41e2d 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskModels.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskModels.kt @@ -2,6 +2,7 @@ package edu.ntnu.idi.idatt.backend.game.tasks.application import edu.ntnu.idi.idatt.backend.game.tasks.domain.TaskPassingRule import edu.ntnu.idi.idatt.backend.game.tasks.domain.TaskType +import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser import java.time.Instant /** @@ -147,7 +148,7 @@ data class TaskSummary( * Command used to submit answers for all tasks at a given stop. */ data class SubmitStopAnswersCommand( - val authenticatedSubject: String, + val currentUser: CurrentUser, val classroomId: Long, val stopId: Long, val taskAnswers: List, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskService.kt index 5685252d..59b50880 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/application/TaskService.kt @@ -232,7 +232,7 @@ class TaskService( */ @Transactional fun submitStopAnswers(command: SubmitStopAnswersCommand): StopSubmissionResult { - val (_, user) = requireParticipatingClassroom(command.authenticatedSubject, command.classroomId) + val (_, user) = requireParticipatingClassroom(command.currentUser.subject, command.classroomId) if (user.role != UserRole.PUPIL) { throw ResponseStatusException(HttpStatus.FORBIDDEN, "Only pupils may submit stop answers") } @@ -271,7 +271,7 @@ class TaskService( val totalMaxScore = tasks.sumOf { it.maxScore } val passed = taskResults.all { it.passed } val updatedProgress = updateProgressAfterSubmission( - pupilId = pupilId, + currentUser = command.currentUser, classroomId = command.classroomId, stopId = command.stopId, currentStatus = progress.status, @@ -577,7 +577,7 @@ class TaskService( } private fun updateProgressAfterSubmission( - pupilId: Long, + currentUser: CurrentUser, classroomId: Long, stopId: Long, currentStatus: StopProgressStatus, @@ -588,7 +588,7 @@ class TaskService( when { passed && currentStatus == StopProgressStatus.NOT_STARTED -> { pupilStopProgressService.updateProgress( - pupilId, + currentUser, classroomId, stopId, UpdateStopProgressCommand( @@ -599,7 +599,7 @@ class TaskService( ), ) pupilStopProgressService.updateProgress( - pupilId, + currentUser, classroomId, stopId, UpdateStopProgressCommand( @@ -612,7 +612,7 @@ class TaskService( } passed && currentStatus == StopProgressStatus.IN_PROGRESS -> pupilStopProgressService.updateProgress( - pupilId, + currentUser, classroomId, stopId, UpdateStopProgressCommand( @@ -624,7 +624,7 @@ class TaskService( ) passed -> pupilStopProgressService.updateProgress( - pupilId, + currentUser, classroomId, stopId, UpdateStopProgressCommand( @@ -636,7 +636,7 @@ class TaskService( ) currentStatus == StopProgressStatus.NOT_STARTED -> pupilStopProgressService.updateProgress( - pupilId, + currentUser, classroomId, stopId, UpdateStopProgressCommand( @@ -648,7 +648,7 @@ class TaskService( ) else -> pupilStopProgressService.updateProgress( - pupilId, + currentUser, classroomId, stopId, UpdateStopProgressCommand( diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt index dc4fd5e3..ad92c063 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt @@ -1,7 +1,6 @@ package edu.ntnu.idi.idatt.backend.game.mapstops import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom -import edu.ntnu.idi.idatt.backend.classroom.infrastructure.ClassroomRepository import edu.ntnu.idi.idatt.backend.game.mapstops.application.PupilStopProgressService import edu.ntnu.idi.idatt.backend.game.mapstops.application.UpdateStopProgressCommand import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop @@ -10,9 +9,11 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.domain.PupilStopProgressId import edu.ntnu.idi.idatt.backend.game.mapstops.domain.StopProgressStatus import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.PupilStopProgressRepository +import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User 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.security.CurrentUser import edu.ntnu.idi.idatt.backend.shared.events.DomainEvent import edu.ntnu.idi.idatt.backend.shared.events.DomainEventPublisher import edu.ntnu.idi.idatt.backend.shared.events.MapStopCompletedEvent @@ -29,7 +30,6 @@ import org.mockito.ArgumentMatchers.any import org.mockito.Mockito import org.springframework.http.HttpStatus import org.springframework.web.server.ResponseStatusException -import java.util.Optional class PupilStopProgressServiceTests { @@ -101,7 +101,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = StopProgressStatus.NOT_STARTED, null, null, null), ) @@ -120,7 +121,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = StopProgressStatus.IN_PROGRESS, null, null, null), ) @@ -139,7 +141,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = StopProgressStatus.COMPLETED, null, null, null), ) @@ -160,7 +163,8 @@ class PupilStopProgressServiceTests { // first completion service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = StopProgressStatus.COMPLETED, null, null, null), ) val firstCompletedAt = record.completedAt @@ -182,7 +186,8 @@ class PupilStopProgressServiceTests { val exception = assertThrows { service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = StopProgressStatus.IN_PROGRESS, null, null, null), ) } @@ -203,7 +208,8 @@ class PupilStopProgressServiceTests { val exception = assertThrows { service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = StopProgressStatus.COMPLETED, null, null, null), ) } @@ -222,7 +228,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = null, currentTaskId = null, bestScore = 80, medalAwarded = null), ) @@ -239,7 +246,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = null, currentTaskId = null, bestScore = 50, medalAwarded = null), ) @@ -256,7 +264,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = null, currentTaskId = 7L, bestScore = null, medalAwarded = null), ) @@ -276,7 +285,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand( status = StopProgressStatus.COMPLETED, currentTaskId = 99L, @@ -298,7 +308,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = null, currentTaskId = null, bestScore = null, medalAwarded = true), ) @@ -316,7 +327,8 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, classroomId = 2L, stopId = 1L, + currentUser = pupilCurrentUser(), + classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = null, currentTaskId = null, bestScore = null, medalAwarded = null), ) @@ -337,7 +349,7 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) service.updateProgress( - pupilUserId = 1L, + currentUser = pupilCurrentUser(), classroomId = 2L, stopId = 99L, command = UpdateStopProgressCommand(status = StopProgressStatus.COMPLETED, null, null, null), @@ -360,7 +372,7 @@ class PupilStopProgressServiceTests { stubFindAndSave(progressRepository, record) val result = service.updateProgress( - pupilUserId = 1L, + currentUser = pupilCurrentUser(), classroomId = 2L, stopId = 1L, command = UpdateStopProgressCommand(status = StopProgressStatus.COMPLETED, null, null, null), @@ -563,8 +575,28 @@ class PupilStopProgressServiceTests { progressRepository: PupilStopProgressRepository, mapStopRepository: MapStopsRepository, domainEventPublisher: DomainEventPublisher = Mockito.mock(DomainEventPublisher::class.java), + currentUserResolver: CurrentUserResolver = Mockito.mock(CurrentUserResolver::class.java).also { + Mockito.`when`(it.requirePupil("1")).thenReturn(pupil()) + }, ): PupilStopProgressService = - PupilStopProgressService(progressRepository, mapStopRepository, domainEventPublisher) + PupilStopProgressService(progressRepository, mapStopRepository, currentUserResolver, domainEventPublisher) + + private fun pupilCurrentUser(): CurrentUser = + CurrentUser( + subject = "1", + portalType = "pupil", + email = null, + authorities = setOf("ROLE_PUPIL"), + ) + + private fun pupil(): User = + User( + id = 1L, + username = "pupil-one", + passwordHash = "hash", + role = UserRole.PUPIL, + status = UserStatus.ACTIVE, + ) private fun stubFind(repo: PupilStopProgressRepository, record: PupilStopProgress) { Mockito.`when`(repo.findByIdPupilUserIdAndIdClassroomIdAndIdStopId( From f1a5d44cd81b22df4154dd7382e9b2598260a18c Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 12:18:00 +0200 Subject: [PATCH 13/20] Add pupil_xp_event entry on stop completion Signed-off-by: sindrelm --- .../game/mapstops/application/PupilStopProgressService.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt index 860b0fd6..994422d9 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt @@ -7,6 +7,8 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepositor import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.PupilStopProgressRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser +import edu.ntnu.idi.idatt.backend.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode import edu.ntnu.idi.idatt.backend.shared.events.DomainEventPublisher import edu.ntnu.idi.idatt.backend.shared.events.MapStopCompletedEvent import edu.ntnu.idi.idatt.backend.shared.events.PupilGameCompletedEvent @@ -32,6 +34,7 @@ class PupilStopProgressService( private val mapStopRepository: MapStopsRepository, private val currentUserResolver: CurrentUserResolver, private val domainEventPublisher: DomainEventPublisher, + private val xpService: XpService, ) { /** * Returns all stop progress records for a pupil in a classroom. @@ -106,6 +109,7 @@ class PupilStopProgressService( progress.completedAt = Instant.now() unlockNextStop(pupilUserId, classroomId, stopId, activeStops) publishCompletionEvents(pupilUserId, classroomId, stopId, progress) + xpService.awardXpEvent(currentUser, XpEventCode.STOP_COMPLETED) } } if (progress.status != StopProgressStatus.COMPLETED) { From a73bea27e383300f90771a48a6e80c154559288c Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 12:20:37 +0200 Subject: [PATCH 14/20] Add pupil_xp_event entry on notebook entry creation Signed-off-by: sindrelm --- .../idatt/backend/notebook/application/NotebookService.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 1e5a65e5..6dd806ff 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -7,6 +7,8 @@ import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser 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.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -25,6 +27,7 @@ class NotebookService( private val currentUserResolver: CurrentUserResolver, private val mapStopsRepository: MapStopsRepository, private val pupilGameProgressRepository: PupilGameProgressRepository, + private val xpService: XpService, ) { /** * Returns all notebook entries owned by the authenticated pupil. @@ -70,6 +73,8 @@ class NotebookService( ResponseStatusException(HttpStatus.NOT_FOUND, "Map stop not found") } + xpService.awardXpEvent(currentUser, XpEventCode.NOTEBOOK_ENTRY_CREATED) + val notebookEntry = NotebookEntry( pupil = if (command.sourceType == NotebookSourceType.PUPIL) pupil else null, classroom = classroom, From eca5330403060a6f8c1963e306450fa4ab6febfc Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 12:25:08 +0200 Subject: [PATCH 15/20] Add pupil_xp_event entry on avatar update Signed-off-by: sindrelm --- .../backend/users/avatar/application/UserAvatarService.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt index 3259c32b..9546cf65 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt @@ -4,6 +4,8 @@ import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User 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.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -19,6 +21,7 @@ import org.springframework.web.server.ResponseStatusException class UserAvatarService( private val currentUserResolver: CurrentUserResolver, private val userAvatarRepository: UserAvatarRepository, + private val xpService: XpService, ) { /** * Returns the current authenticated pupil's avatar. @@ -51,11 +54,15 @@ class UserAvatarService( ): UserAvatar { val user = currentUserResolver.requirePupil(authenticatedSubject) val userAvatar = findUserAvatar(user) + userAvatar.update( bodyType = command.bodyType, skinTone = command.skinTone, outfitStyle = command.outfitStyle, ) + + xpService.awardXpEvent(currentUser, XpEventCode.UPDATE_AVATAR) + return userAvatarRepository.save(userAvatar) } From cdbd941f2a1edce72738df2d37db86728ca98886 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 12:26:57 +0200 Subject: [PATCH 16/20] Add pupil_xp_event entry on medal awarded Signed-off-by: sindrelm --- .../idi/idatt/backend/game/medals/application/MedalService.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt index b053efac..140e61ca 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt @@ -6,6 +6,8 @@ import edu.ntnu.idi.idatt.backend.game.medals.repository.MedalRepository import edu.ntnu.idi.idatt.backend.game.medals.repository.PupilMedalRepository import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom +import edu.ntnu.idi.idatt.backend.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode import org.springframework.dao.DataIntegrityViolationException import org.springframework.stereotype.Service import org.springframework.transaction.PlatformTransactionManager @@ -24,9 +26,11 @@ import java.util.stream.Collectors class MedalService( private val medalRepository: MedalRepository, private val pupilMedalRepository: PupilMedalRepository, + private val xpService: XpService, transactionManager: PlatformTransactionManager, ) { private val awardTransaction = TransactionTemplate(transactionManager).apply { + xpService.awardXpEvent(currentUser, XpEventCode.MEDAL_AWARD) propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW } From 6de8808e2aec7497de5945c21a93bf4cf9ff4cbc Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 12:37:28 +0200 Subject: [PATCH 17/20] Fix typos Signed-off-by: sindrelm --- .../ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt | 2 +- .../backend/game/medals/application/MedalService.kt | 13 +++++++++++-- .../backend/notebook/application/NotebookService.kt | 3 ++- .../users/avatar/application/UserAvatarService.kt | 11 ++++++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt index 3641b11d..9f694640 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt @@ -32,7 +32,7 @@ class XpBootstrap( xpEventRepository.save( XpEvent( - code = XpEventCode.MEDAL_REWARDED, + code = XpEventCode.MEDAL_AWARDED, title = "Medalje låst opp", xpScore = 75, repeatable = true, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt index 140e61ca..2eadd74a 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt @@ -5,6 +5,7 @@ import edu.ntnu.idi.idatt.backend.game.medals.domain.PupilMedal import edu.ntnu.idi.idatt.backend.game.medals.repository.MedalRepository import edu.ntnu.idi.idatt.backend.game.medals.repository.PupilMedalRepository import edu.ntnu.idi.idatt.backend.iam.domain.User +import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom import edu.ntnu.idi.idatt.backend.game.xp.application.XpService import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode @@ -30,7 +31,6 @@ class MedalService( transactionManager: PlatformTransactionManager, ) { private val awardTransaction = TransactionTemplate(transactionManager).apply { - xpService.awardXpEvent(currentUser, XpEventCode.MEDAL_AWARD) propagationBehavior = TransactionDefinition.PROPAGATION_REQUIRES_NEW } @@ -84,7 +84,16 @@ class MedalService( pupil = pupil, classroom = classroom, medal = medal, - ) + ), + ) + xpService.awardXpEvent( + CurrentUser( + subject = pupilId.toString(), + portalType = "pupil", + email = null, + authorities = setOf("ROLE_PUPIL"), + ), + XpEventCode.MEDAL_AWARDED, ) } } catch (exception: DataIntegrityViolationException) { diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 6dd806ff..130f4768 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -58,6 +58,7 @@ class NotebookService( @Transactional fun createNotebookEntry(command: CreateNotebookEntryCommand): NotebookEntry { val pupil = currentUserResolver.requirePupil(command.currentUser.subject) + val pupilId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" } val classroom = currentUserResolver.getClassroomByUser(command.currentUser) if (command.sourceType == NotebookSourceType.GAME) { @@ -73,7 +74,7 @@ class NotebookService( ResponseStatusException(HttpStatus.NOT_FOUND, "Map stop not found") } - xpService.awardXpEvent(currentUser, XpEventCode.NOTEBOOK_ENTRY_CREATED) + xpService.awardXpEvent(command.currentUser, XpEventCode.NOTEBOOK_ENTRY_CREATED) val notebookEntry = NotebookEntry( pupil = if (command.sourceType == NotebookSourceType.PUPIL) pupil else null, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt index 9546cf65..23971ddc 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt @@ -2,6 +2,7 @@ package edu.ntnu.idi.idatt.backend.users.avatar.application import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User +import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser 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.game.xp.application.XpService @@ -61,7 +62,15 @@ class UserAvatarService( outfitStyle = command.outfitStyle, ) - xpService.awardXpEvent(currentUser, XpEventCode.UPDATE_AVATAR) + xpService.awardXpEvent( + CurrentUser( + subject = authenticatedSubject, + portalType = "pupil", + email = null, + authorities = setOf("ROLE_PUPIL"), + ), + XpEventCode.UPDATE_AVATAR, + ) return userAvatarRepository.save(userAvatar) } From dab42053f430dbc730de20350095ab41aa4de55f Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 12:52:43 +0200 Subject: [PATCH 18/20] fix tests Signed-off-by: sindrelm --- .../backend/game/xp/application/XpService.kt | 11 +++++++---- .../mapstops/PupilStopProgressServiceTests.kt | 4 +++- .../backend/game/medals/MedalServiceTests.kt | 7 +++++-- .../backend/notebook/NotebookServiceTests.kt | 19 +++++++++++++++++++ .../users/avatar/UserAvatarServiceTests.kt | 16 +++++++++++----- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt index bad8ff70..70e3e291 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt @@ -53,8 +53,7 @@ class XpService( val pupil = currentUserResolver.requirePupil(currentUser.subject) val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" } - val xpEvent = xpEventRepository.findByCode(eventCode) - ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "XP event was not found") + val xpEvent = xpEventRepository.findByCode(eventCode) ?: return val xpEventId = requireNotNull(xpEvent.id) { "XP event is missing an id" } val pupilXpEvent = pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(pupilId, xpEventId) @@ -92,11 +91,15 @@ class XpService( val pupil = currentUserResolver.requirePupil(currentUser.subject) val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" } - val classroom = currentUserResolver.getClassroomByUser(currentUser) + val classroom = try { + currentUserResolver.getClassroomByUser(currentUser) + } catch (_: ResponseStatusException) { + return + } val classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" } val pupilGameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) - ?: throw ResponseStatusException(HttpStatus.NOT_FOUND, "PupilGameProgress was not found") + ?: return val pupilXpEvents = pupilXpEventRepository.findAllByIdPupilUserId(pupilId) val totalXp = pupilXpEvents.sumOf { pupilXpEvent -> diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt index ad92c063..85144d88 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt @@ -9,6 +9,7 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.domain.PupilStopProgressId import edu.ntnu.idi.idatt.backend.game.mapstops.domain.StopProgressStatus import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.PupilStopProgressRepository +import edu.ntnu.idi.idatt.backend.game.xp.application.XpService import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole @@ -578,8 +579,9 @@ class PupilStopProgressServiceTests { currentUserResolver: CurrentUserResolver = Mockito.mock(CurrentUserResolver::class.java).also { Mockito.`when`(it.requirePupil("1")).thenReturn(pupil()) }, + xpService: XpService = Mockito.mock(XpService::class.java), ): PupilStopProgressService = - PupilStopProgressService(progressRepository, mapStopRepository, currentUserResolver, domainEventPublisher) + PupilStopProgressService(progressRepository, mapStopRepository, currentUserResolver, domainEventPublisher, xpService) private fun pupilCurrentUser(): CurrentUser = CurrentUser( diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/medals/MedalServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/medals/MedalServiceTests.kt index dc0ec094..39aaaaf1 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/medals/MedalServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/medals/MedalServiceTests.kt @@ -7,6 +7,7 @@ import edu.ntnu.idi.idatt.backend.game.medals.domain.MedalType import edu.ntnu.idi.idatt.backend.game.medals.domain.PupilMedal import edu.ntnu.idi.idatt.backend.game.medals.repository.MedalRepository import edu.ntnu.idi.idatt.backend.game.medals.repository.PupilMedalRepository +import edu.ntnu.idi.idatt.backend.game.xp.application.XpService import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole import edu.ntnu.idi.idatt.backend.iam.domain.UserStatus @@ -25,7 +26,8 @@ class MedalServiceTests { fun `award stop medal ignores duplicate award race`() { val medalRepository = Mockito.mock(MedalRepository::class.java) val pupilMedalRepository = Mockito.mock(PupilMedalRepository::class.java) - val service = MedalService(medalRepository, pupilMedalRepository, transactionManager()) + val xpService = Mockito.mock(XpService::class.java) + val service = MedalService(medalRepository, pupilMedalRepository, xpService, transactionManager()) Mockito.`when`(medalRepository.findByStopId(5L)).thenReturn(medal(stopId = 5L)) Mockito.`when`(pupilMedalRepository.existsByPupilIdAndClassroomIdAndMedalId(1L, 2L, 10L)) @@ -44,7 +46,8 @@ class MedalServiceTests { fun `award stop medal rethrows unexpected integrity failure`() { val medalRepository = Mockito.mock(MedalRepository::class.java) val pupilMedalRepository = Mockito.mock(PupilMedalRepository::class.java) - val service = MedalService(medalRepository, pupilMedalRepository, transactionManager()) + val xpService = Mockito.mock(XpService::class.java) + val service = MedalService(medalRepository, pupilMedalRepository, xpService, transactionManager()) val exception = DataIntegrityViolationException("foreign key failure") Mockito.`when`(medalRepository.findByStopId(5L)).thenReturn(medal(stopId = 5L)) diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt index 099de886..f9893efc 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt @@ -4,6 +4,7 @@ import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository +import edu.ntnu.idi.idatt.backend.game.xp.application.XpService import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole @@ -33,11 +34,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -60,11 +63,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -105,11 +110,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -139,11 +146,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -173,11 +182,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -208,11 +219,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -244,11 +257,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -276,11 +291,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) @@ -304,11 +321,13 @@ class NotebookServiceTests { val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, pupilGameProgressRepository, + xpService, ) val currentUser = currentUser("42") val pupil = pupilUser(42L) diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt index 05b6fe12..4c0ace5c 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt @@ -4,6 +4,7 @@ import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole import edu.ntnu.idi.idatt.backend.iam.domain.UserStatus +import edu.ntnu.idi.idatt.backend.game.xp.application.XpService import edu.ntnu.idi.idatt.backend.users.avatar.application.UserAvatarService import edu.ntnu.idi.idatt.backend.users.avatar.domain.UserAvatar import edu.ntnu.idi.idatt.backend.users.avatar.infrastructure.UserAvatarRepository @@ -20,7 +21,8 @@ class UserAvatarServiceTests { fun `get user avatar returns persisted avatar for authenticated pupil`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val userAvatarRepository = Mockito.mock(UserAvatarRepository::class.java) - val service = UserAvatarService(currentUserResolver, userAvatarRepository) + val xpService = Mockito.mock(XpService::class.java) + val service = UserAvatarService(currentUserResolver, userAvatarRepository, xpService) val user = pupilUser(42L) val avatar = UserAvatar( user = user, @@ -43,7 +45,8 @@ class UserAvatarServiceTests { fun `get user avatar rejects malformed authenticated subject`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val userAvatarRepository = Mockito.mock(UserAvatarRepository::class.java) - val service = UserAvatarService(currentUserResolver, userAvatarRepository) + val xpService = Mockito.mock(XpService::class.java) + val service = UserAvatarService(currentUserResolver, userAvatarRepository, xpService) val exception = ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid authenticated user subject") Mockito.`when`(currentUserResolver.requirePupil("not-a-number")).thenThrow(exception) @@ -59,7 +62,8 @@ class UserAvatarServiceTests { fun `get user avatar rejects unknown authenticated user`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val userAvatarRepository = Mockito.mock(UserAvatarRepository::class.java) - val service = UserAvatarService(currentUserResolver, userAvatarRepository) + val xpService = Mockito.mock(XpService::class.java) + val service = UserAvatarService(currentUserResolver, userAvatarRepository, xpService) val exception = ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authenticated user was not found") Mockito.`when`(currentUserResolver.requirePupil("42")).thenThrow(exception) @@ -75,7 +79,8 @@ class UserAvatarServiceTests { fun `get user avatar rejects teacher user`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val userAvatarRepository = Mockito.mock(UserAvatarRepository::class.java) - val service = UserAvatarService(currentUserResolver, userAvatarRepository) + val xpService = Mockito.mock(XpService::class.java) + val service = UserAvatarService(currentUserResolver, userAvatarRepository, xpService) val exception = ResponseStatusException(HttpStatus.FORBIDDEN, "Only pupils are allowed") Mockito.`when`(currentUserResolver.requirePupil("7")).thenThrow(exception) @@ -91,7 +96,8 @@ class UserAvatarServiceTests { fun `get user avatar rejects pupil without avatar row`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val userAvatarRepository = Mockito.mock(UserAvatarRepository::class.java) - val service = UserAvatarService(currentUserResolver, userAvatarRepository) + val xpService = Mockito.mock(XpService::class.java) + val service = UserAvatarService(currentUserResolver, userAvatarRepository, xpService) val user = pupilUser(42L) Mockito.`when`(currentUserResolver.requirePupil("42")).thenReturn(user) From 0bcd2e6d3da791d571c3195e67def68c08c74088 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 13:21:47 +0200 Subject: [PATCH 19/20] Refactor folders out of game/ Signed-off-by: sindrelm --- .../idatt/backend/bootstrap/MedalBootstrap.kt | 6 +++--- .../idi/idatt/backend/bootstrap/XpBootstrap.kt | 12 ++++++------ .../application/PupilStopProgressService.kt | 4 ++-- .../{game => }/medals/api/MedalsController.kt | 8 ++++---- .../medals/application/MedalService.kt | 14 +++++++------- .../backend/{game => }/medals/domain/Medal.kt | 2 +- .../{game => }/medals/domain/MedalType.kt | 2 +- .../{game => }/medals/domain/PupilMedal.kt | 2 +- .../medals/repository/MedalRepository.kt | 4 ++-- .../medals/repository/PupilMedalRepository.kt | 4 ++-- .../notebook/application/NotebookService.kt | 4 ++-- .../avatar/application/UserAvatarService.kt | 4 ++-- .../{game => }/xp/application/XpService.kt | 12 ++++++------ .../backend/{game => }/xp/domain/PupilXpEvent.kt | 2 +- .../{game => }/xp/domain/PupilXpEventId.kt | 2 +- .../backend/{game => }/xp/domain/XpEvent.kt | 2 +- .../backend/{game => }/xp/domain/XpEventCode.kt | 2 +- .../xp/infrastructure/PupilXpEventRepository.kt | 6 +++--- .../xp/infrastructure/XpEventRepository.kt | 6 +++--- .../mapstops/PupilStopProgressServiceTests.kt | 2 +- .../{game => }/medals/MedalServiceTests.kt | 16 ++++++++-------- .../backend/notebook/NotebookServiceTests.kt | 2 +- .../users/avatar/UserAvatarServiceTests.kt | 2 +- .../backend/{game => }/xp/XpServiceTests.kt | 16 ++++++++-------- 24 files changed, 68 insertions(+), 68 deletions(-) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/api/MedalsController.kt (91%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/application/MedalService.kt (90%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/domain/Medal.kt (95%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/domain/MedalType.kt (55%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/domain/PupilMedal.kt (95%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/repository/MedalRepository.kt (73%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/repository/PupilMedalRepository.kt (79%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/application/XpService.kt (93%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/domain/PupilXpEvent.kt (96%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/domain/PupilXpEventId.kt (92%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/domain/XpEvent.kt (96%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/domain/XpEventCode.kt (87%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/infrastructure/PupilXpEventRepository.kt (87%) rename src/main/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/infrastructure/XpEventRepository.kt (73%) rename src/test/kotlin/edu/ntnu/idi/idatt/backend/{game => }/medals/MedalServiceTests.kt (87%) rename src/test/kotlin/edu/ntnu/idi/idatt/backend/{game => }/xp/XpServiceTests.kt (94%) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/MedalBootstrap.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/MedalBootstrap.kt index 20d2acbb..a3487b20 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/MedalBootstrap.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/MedalBootstrap.kt @@ -1,9 +1,9 @@ package edu.ntnu.idi.idatt.backend.bootstrap import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository -import edu.ntnu.idi.idatt.backend.game.medals.domain.Medal -import edu.ntnu.idi.idatt.backend.game.medals.domain.MedalType -import edu.ntnu.idi.idatt.backend.game.medals.repository.MedalRepository +import edu.ntnu.idi.idatt.backend.medals.domain.Medal +import edu.ntnu.idi.idatt.backend.medals.domain.MedalType +import edu.ntnu.idi.idatt.backend.medals.repository.MedalRepository import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.stereotype.Component diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt index 9f694640..319904d9 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/bootstrap/XpBootstrap.kt @@ -1,11 +1,11 @@ package edu.ntnu.idi.idatt.backend.bootstrap -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode -import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.PupilXpEventRepository -import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.XpEventRepository +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEvent +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.xp.domain.XpEvent +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.infrastructure.PupilXpEventRepository +import edu.ntnu.idi.idatt.backend.xp.infrastructure.XpEventRepository import edu.ntnu.idi.idatt.backend.iam.domain.User import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty import org.springframework.stereotype.Component diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt index 994422d9..fd98c294 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/application/PupilStopProgressService.kt @@ -7,8 +7,8 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepositor import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.PupilStopProgressRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser -import edu.ntnu.idi.idatt.backend.game.xp.application.XpService -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode import edu.ntnu.idi.idatt.backend.shared.events.DomainEventPublisher import edu.ntnu.idi.idatt.backend.shared.events.MapStopCompletedEvent import edu.ntnu.idi.idatt.backend.shared.events.PupilGameCompletedEvent diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/api/MedalsController.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/api/MedalsController.kt similarity index 91% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/api/MedalsController.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/api/MedalsController.kt index 64ba75a5..95279608 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/api/MedalsController.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/api/MedalsController.kt @@ -1,8 +1,8 @@ -package edu.ntnu.idi.idatt.backend.game.medals.api +package edu.ntnu.idi.idatt.backend.medals.api -import edu.ntnu.idi.idatt.backend.game.medals.application.MedalService -import edu.ntnu.idi.idatt.backend.game.medals.domain.Medal -import edu.ntnu.idi.idatt.backend.game.medals.domain.PupilMedal +import edu.ntnu.idi.idatt.backend.medals.application.MedalService +import edu.ntnu.idi.idatt.backend.medals.domain.Medal +import edu.ntnu.idi.idatt.backend.medals.domain.PupilMedal import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser import org.springframework.http.HttpStatus diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/application/MedalService.kt similarity index 90% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/application/MedalService.kt index 2eadd74a..04c534a1 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/application/MedalService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/application/MedalService.kt @@ -1,14 +1,14 @@ -package edu.ntnu.idi.idatt.backend.game.medals.application +package edu.ntnu.idi.idatt.backend.medals.application -import edu.ntnu.idi.idatt.backend.game.medals.domain.Medal -import edu.ntnu.idi.idatt.backend.game.medals.domain.PupilMedal -import edu.ntnu.idi.idatt.backend.game.medals.repository.MedalRepository -import edu.ntnu.idi.idatt.backend.game.medals.repository.PupilMedalRepository +import edu.ntnu.idi.idatt.backend.medals.domain.Medal +import edu.ntnu.idi.idatt.backend.medals.domain.PupilMedal +import edu.ntnu.idi.idatt.backend.medals.repository.MedalRepository +import edu.ntnu.idi.idatt.backend.medals.repository.PupilMedalRepository import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom -import edu.ntnu.idi.idatt.backend.game.xp.application.XpService -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode import org.springframework.dao.DataIntegrityViolationException import org.springframework.stereotype.Service import org.springframework.transaction.PlatformTransactionManager diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/Medal.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/Medal.kt similarity index 95% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/Medal.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/Medal.kt index 280cd12d..4699d7e6 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/Medal.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/Medal.kt @@ -1,4 +1,4 @@ -package edu.ntnu.idi.idatt.backend.game.medals.domain +package edu.ntnu.idi.idatt.backend.medals.domain import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop import jakarta.persistence.* diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/MedalType.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/MedalType.kt similarity index 55% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/MedalType.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/MedalType.kt index 8388aeb8..645c9dde 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/MedalType.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/MedalType.kt @@ -1,4 +1,4 @@ -package edu.ntnu.idi.idatt.backend.game.medals.domain +package edu.ntnu.idi.idatt.backend.medals.domain enum class MedalType { STOP_COMPLETION, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/PupilMedal.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/PupilMedal.kt similarity index 95% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/PupilMedal.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/PupilMedal.kt index 81452755..90d1bb81 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/domain/PupilMedal.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/domain/PupilMedal.kt @@ -1,4 +1,4 @@ -package edu.ntnu.idi.idatt.backend.game.medals.domain +package edu.ntnu.idi.idatt.backend.medals.domain import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/repository/MedalRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/repository/MedalRepository.kt similarity index 73% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/repository/MedalRepository.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/repository/MedalRepository.kt index 5aa67708..de1ee167 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/repository/MedalRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/repository/MedalRepository.kt @@ -1,6 +1,6 @@ -package edu.ntnu.idi.idatt.backend.game.medals.repository +package edu.ntnu.idi.idatt.backend.medals.repository -import edu.ntnu.idi.idatt.backend.game.medals.domain.Medal +import edu.ntnu.idi.idatt.backend.medals.domain.Medal import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/repository/PupilMedalRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/repository/PupilMedalRepository.kt similarity index 79% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/repository/PupilMedalRepository.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/repository/PupilMedalRepository.kt index 1ca0d753..965335f3 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/medals/repository/PupilMedalRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/medals/repository/PupilMedalRepository.kt @@ -1,6 +1,6 @@ -package edu.ntnu.idi.idatt.backend.game.medals.repository +package edu.ntnu.idi.idatt.backend.medals.repository -import edu.ntnu.idi.idatt.backend.game.medals.domain.PupilMedal +import edu.ntnu.idi.idatt.backend.medals.domain.PupilMedal import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository import java.util.stream.Stream diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 130f4768..6c53dc07 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -7,8 +7,8 @@ import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser 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.game.xp.application.XpService -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt index 23971ddc..cbeb19d5 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/application/UserAvatarService.kt @@ -5,8 +5,8 @@ import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser 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.game.xp.application.XpService -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt similarity index 93% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt index 70e3e291..0d94d439 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/application/XpService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt @@ -1,11 +1,11 @@ -package edu.ntnu.idi.idatt.backend.game.xp.application +package edu.ntnu.idi.idatt.backend.xp.application import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode -import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.PupilXpEventRepository -import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.XpEventRepository +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEvent +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.infrastructure.PupilXpEventRepository +import edu.ntnu.idi.idatt.backend.xp.infrastructure.XpEventRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser import org.springframework.http.HttpStatus diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvent.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/PupilXpEvent.kt similarity index 96% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvent.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/PupilXpEvent.kt index d91c2433..e9b4fde8 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEvent.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/PupilXpEvent.kt @@ -1,4 +1,4 @@ -package edu.ntnu.idi.idatt.backend.game.xp.domain +package edu.ntnu.idi.idatt.backend.xp.domain import edu.ntnu.idi.idatt.backend.iam.domain.User import jakarta.persistence.Column diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/PupilXpEventId.kt similarity index 92% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/PupilXpEventId.kt index 1c3d497a..58547f0d 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/PupilXpEventId.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/PupilXpEventId.kt @@ -1,4 +1,4 @@ -package edu.ntnu.idi.idatt.backend.game.xp.domain +package edu.ntnu.idi.idatt.backend.xp.domain import jakarta.persistence.Column import jakarta.persistence.Embeddable diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/XpEvent.kt similarity index 96% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/XpEvent.kt index 00b38c1d..19c6d905 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEvent.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/XpEvent.kt @@ -1,4 +1,4 @@ -package edu.ntnu.idi.idatt.backend.game.xp.domain +package edu.ntnu.idi.idatt.backend.xp.domain import jakarta.persistence.Column import jakarta.persistence.Entity diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEventCode.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/XpEventCode.kt similarity index 87% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEventCode.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/XpEventCode.kt index f5c38a0e..45211d74 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/domain/XpEventCode.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/domain/XpEventCode.kt @@ -1,4 +1,4 @@ -package edu.ntnu.idi.idatt.backend.game.xp.domain +package edu.ntnu.idi.idatt.backend.xp.domain /** * Stable identifiers for XP event definitions. diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/infrastructure/PupilXpEventRepository.kt similarity index 87% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/infrastructure/PupilXpEventRepository.kt index 321cb03e..b34c04de 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/PupilXpEventRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/infrastructure/PupilXpEventRepository.kt @@ -1,7 +1,7 @@ -package edu.ntnu.idi.idatt.backend.game.xp.infrastructure +package edu.ntnu.idi.idatt.backend.xp.infrastructure -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEvent import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/infrastructure/XpEventRepository.kt similarity index 73% rename from src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt rename to src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/infrastructure/XpEventRepository.kt index 5f98a2b2..31eb1752 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/xp/infrastructure/XpEventRepository.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/infrastructure/XpEventRepository.kt @@ -1,7 +1,7 @@ -package edu.ntnu.idi.idatt.backend.game.xp.infrastructure +package edu.ntnu.idi.idatt.backend.xp.infrastructure -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.domain.XpEvent import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt index 85144d88..e0da7563 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/mapstops/PupilStopProgressServiceTests.kt @@ -9,7 +9,7 @@ import edu.ntnu.idi.idatt.backend.game.mapstops.domain.PupilStopProgressId import edu.ntnu.idi.idatt.backend.game.mapstops.domain.StopProgressStatus import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.PupilStopProgressRepository -import edu.ntnu.idi.idatt.backend.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.application.XpService import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/medals/MedalServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/medals/MedalServiceTests.kt similarity index 87% rename from src/test/kotlin/edu/ntnu/idi/idatt/backend/game/medals/MedalServiceTests.kt rename to src/test/kotlin/edu/ntnu/idi/idatt/backend/medals/MedalServiceTests.kt index 39aaaaf1..18824559 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/medals/MedalServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/medals/MedalServiceTests.kt @@ -1,13 +1,13 @@ -package edu.ntnu.idi.idatt.backend.game.medals +package edu.ntnu.idi.idatt.backend.medals import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom -import edu.ntnu.idi.idatt.backend.game.medals.application.MedalService -import edu.ntnu.idi.idatt.backend.game.medals.domain.Medal -import edu.ntnu.idi.idatt.backend.game.medals.domain.MedalType -import edu.ntnu.idi.idatt.backend.game.medals.domain.PupilMedal -import edu.ntnu.idi.idatt.backend.game.medals.repository.MedalRepository -import edu.ntnu.idi.idatt.backend.game.medals.repository.PupilMedalRepository -import edu.ntnu.idi.idatt.backend.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.medals.application.MedalService +import edu.ntnu.idi.idatt.backend.medals.domain.Medal +import edu.ntnu.idi.idatt.backend.medals.domain.MedalType +import edu.ntnu.idi.idatt.backend.medals.domain.PupilMedal +import edu.ntnu.idi.idatt.backend.medals.repository.MedalRepository +import edu.ntnu.idi.idatt.backend.medals.repository.PupilMedalRepository +import edu.ntnu.idi.idatt.backend.xp.application.XpService import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole import edu.ntnu.idi.idatt.backend.iam.domain.UserStatus diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt index f9893efc..215e3c24 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt @@ -4,7 +4,7 @@ import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository -import edu.ntnu.idi.idatt.backend.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.application.XpService import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt index 4c0ace5c..68a2a4b3 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/avatar/UserAvatarServiceTests.kt @@ -4,7 +4,7 @@ import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole import edu.ntnu.idi.idatt.backend.iam.domain.UserStatus -import edu.ntnu.idi.idatt.backend.game.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.application.XpService import edu.ntnu.idi.idatt.backend.users.avatar.application.UserAvatarService import edu.ntnu.idi.idatt.backend.users.avatar.domain.UserAvatar import edu.ntnu.idi.idatt.backend.users.avatar.infrastructure.UserAvatarRepository diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/xp/XpServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/xp/XpServiceTests.kt similarity index 94% rename from src/test/kotlin/edu/ntnu/idi/idatt/backend/game/xp/XpServiceTests.kt rename to src/test/kotlin/edu/ntnu/idi/idatt/backend/xp/XpServiceTests.kt index ed29f1b6..50ccc63b 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/game/xp/XpServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/xp/XpServiceTests.kt @@ -1,16 +1,16 @@ -package edu.ntnu.idi.idatt.backend.game.xp +package edu.ntnu.idi.idatt.backend.xp import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository -import edu.ntnu.idi.idatt.backend.game.xp.application.XpService -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEvent -import edu.ntnu.idi.idatt.backend.game.xp.domain.PupilXpEventId -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEvent -import edu.ntnu.idi.idatt.backend.game.xp.domain.XpEventCode -import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.PupilXpEventRepository -import edu.ntnu.idi.idatt.backend.game.xp.infrastructure.XpEventRepository +import edu.ntnu.idi.idatt.backend.xp.application.XpService +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEvent +import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEventId +import edu.ntnu.idi.idatt.backend.xp.domain.XpEvent +import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode +import edu.ntnu.idi.idatt.backend.xp.infrastructure.PupilXpEventRepository +import edu.ntnu.idi.idatt.backend.xp.infrastructure.XpEventRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole From 17fbc40e4ec94fe392563a26e138309f8eb43eb7 Mon Sep 17 00:00:00 2001 From: sindrelm Date: Fri, 24 Apr 2026 13:27:13 +0200 Subject: [PATCH 20/20] Make public Signed-off-by: sindrelm --- .../edu/ntnu/idi/idatt/backend/xp/application/XpService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt index 0d94d439..be545d8d 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt @@ -87,7 +87,7 @@ class XpService( * @throws ResponseStatusException when no game progress row exists for the active classroom */ @Transactional - private fun updatePupilGameProgressLevel(currentUser: CurrentUser) { + fun updatePupilGameProgressLevel(currentUser: CurrentUser) { val pupil = currentUserResolver.requirePupil(currentUser.subject) val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" }