diff --git a/src/main/kotlin/com/scrumdapp/checkpointservice/controllers/CheckpointSessionController.kt b/src/main/kotlin/com/scrumdapp/checkpointservice/controllers/CheckpointSessionController.kt index 700a364..53c2139 100644 --- a/src/main/kotlin/com/scrumdapp/checkpointservice/controllers/CheckpointSessionController.kt +++ b/src/main/kotlin/com/scrumdapp/checkpointservice/controllers/CheckpointSessionController.kt @@ -25,6 +25,7 @@ import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import java.time.LocalDate +import java.time.YearMonth @RestController @Validated @@ -83,6 +84,29 @@ class CheckpointSessionController( ?: throw NotFoundException(message = "session with $sessionId not found") } + @GetMapping("/dates") + fun getCalendarSessions( + @Passport passport: PassportContent, + @PathVariable groupId: Long, + @RequestParam @DateTimeFormat(pattern = "yyyy-MM") month: YearMonth + ): List { + passport.userGroups?.find { it.toLong() == groupId } + ?: throw ForbiddenException(message = "User is not a member of this group") + + return sessionService.getCalendarSessions(groupId, month) + } + + @GetMapping("/months") + fun getMonthsWithSessions( + @Passport passport: PassportContent, + @PathVariable groupId: Long + ): List { + passport.userGroups?.find { it.toLong() == groupId } + ?: throw ForbiddenException(message = "User is not a member of this group") + + return sessionService.getMonthsWithSessions(groupId) + } + @PostMapping fun createSession( @Passport passport: PassportContent, diff --git a/src/main/kotlin/com/scrumdapp/checkpointservice/services/CheckpointSessionService.kt b/src/main/kotlin/com/scrumdapp/checkpointservice/services/CheckpointSessionService.kt index fcd024f..a4f89a7 100644 --- a/src/main/kotlin/com/scrumdapp/checkpointservice/services/CheckpointSessionService.kt +++ b/src/main/kotlin/com/scrumdapp/checkpointservice/services/CheckpointSessionService.kt @@ -15,6 +15,7 @@ import org.springframework.security.oauth2.jwt.Jwt import org.springframework.stereotype.Service import java.time.LocalDate import java.time.LocalTime +import java.time.YearMonth @Service class CheckpointSessionService( @@ -89,6 +90,26 @@ class CheckpointSessionService( } return response.toList() } + fun getCalendarSessions(groupId: Long, month: YearMonth): List { + val firstOfMonth = month.atDay(1) + val lastOfMonth = month.atEndOfMonth() + + val calendarStart = firstOfMonth.minusDays((firstOfMonth.dayOfWeek.value - 1).toLong()) + val calendarEnd = lastOfMonth.plusDays((7 - lastOfMonth.dayOfWeek.value).toLong()) + + return checkpointSessionRepository.findAllByGroupIdAndCreatedDateBetween(groupId, calendarStart, calendarEnd) + .map { it.toDto() } + } + + fun getMonthsWithSessions(groupId: Long): List { + val dates = checkpointSessionRepository.findAllByGroupId(groupId).map { it.createdDate } + + return dates + .map { YearMonth.from(it) } + .distinct() + .sorted() + .map { it.toString() } // formats as "2026-01" + } fun createSession(jwt: Jwt, groupId: Long, ownerId: Long, dto: CheckpointSessionCreationDto): SessionResponseDto { val checkpointSession = dto.toEntity(groupId, ownerId, dto.name) @@ -100,5 +121,7 @@ class CheckpointSessionService( checkpointRepository.save(Checkpoint(session, groupUser)) } return session.toDto() + + } } \ No newline at end of file