Skip to content

Commit a8ea5a6

Browse files
committed
Endpoint for calendar overview
1 parent a4d9c6f commit a8ea5a6

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

src/main/kotlin/be/sgl/backend/controller/CalendarController.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package be.sgl.backend.controller
33
import be.sgl.backend.config.security.OnlyAdmin
44
import be.sgl.backend.config.security.OnlyStaff
55
import be.sgl.backend.config.security.Public
6-
import be.sgl.backend.dto.CalendarDTO
7-
import be.sgl.backend.dto.CalendarItemWithCalendarsDTO
8-
import be.sgl.backend.dto.CalendarPeriodDTO
9-
import be.sgl.backend.dto.CalendarUpdateDTO
6+
import be.sgl.backend.dto.*
107
import be.sgl.backend.service.CalendarService
118
import io.github.wimdeblauwe.errorhandlingspringbootstarter.ApiErrorResponse
129
import io.swagger.v3.oas.annotations.Operation
@@ -20,6 +17,7 @@ import org.springframework.http.HttpStatus
2017
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
2118
import org.springframework.http.ResponseEntity
2219
import org.springframework.web.bind.annotation.*
20+
import java.time.LocalDate
2321

2422
@RestController
2523
@RequestMapping("/calendars")
@@ -156,6 +154,12 @@ class CalendarController {
156154
return ResponseEntity.ok(calendarService.mergeCalendarDTOChanges(id, calendarDTO))
157155
}
158156

157+
@GetMapping("/items")
158+
@Public
159+
fun getCalendarOverview(@RequestParam from: LocalDate, @RequestParam to: LocalDate): ResponseEntity<Map<String, List<CalendarItemDTO>>> {
160+
return ResponseEntity.ok(calendarService.getCalendarItemOverviewBetween(from, to))
161+
}
162+
159163
@GetMapping("/items/{id}")
160164
@Public
161165
@Operation(

src/main/kotlin/be/sgl/backend/repository/calendar/CalendarItemRepository.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package be.sgl.backend.repository.calendar
33
import be.sgl.backend.entity.calendar.CalendarItem
44
import org.springframework.data.jpa.repository.JpaRepository
55
import org.springframework.stereotype.Repository
6+
import java.time.LocalDateTime
67

78
@Repository
8-
interface CalendarItemRepository : JpaRepository<CalendarItem, Int>
9+
interface CalendarItemRepository : JpaRepository<CalendarItem, Int> {
10+
fun findByClosedFalseAndStartAfterAndEndBefore(from: LocalDateTime, to: LocalDateTime): List<CalendarItem>
11+
}

src/main/kotlin/be/sgl/backend/service/CalendarService.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package be.sgl.backend.service
22

3-
import be.sgl.backend.dto.CalendarDTO
4-
import be.sgl.backend.dto.CalendarItemWithCalendarsDTO
5-
import be.sgl.backend.dto.CalendarPeriodDTO
6-
import be.sgl.backend.dto.CalendarUpdateDTO
3+
import be.sgl.backend.dto.*
74
import be.sgl.backend.entity.calendar.Calendar
85
import be.sgl.backend.entity.calendar.CalendarItem
96
import be.sgl.backend.entity.calendar.CalendarPeriod
@@ -24,6 +21,8 @@ import org.springframework.data.repository.findByIdOrNull
2421
import org.springframework.stereotype.Service
2522
import java.time.DayOfWeek
2623
import java.time.LocalDate
24+
import java.time.LocalTime
25+
import java.time.format.DateTimeFormatter
2726

2827
@Service
2928
@Transactional
@@ -138,6 +137,15 @@ class CalendarService {
138137
deleteCalendar(getCalendarById(id))
139138
}
140139

140+
fun getCalendarItemOverviewBetween(from: LocalDate, to: LocalDate): Map<String, List<CalendarItemWithCalendarsDTO>> {
141+
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
142+
return itemRepository.findByClosedFalseAndStartAfterAndEndBefore(from.atStartOfDay(), to.atTime(LocalTime.MAX))
143+
.map(mapper::toDtoWithCalendars)
144+
.groupBy { item ->
145+
"${formatter.format(item.start)} - ${formatter.format(item.end)}"
146+
}
147+
}
148+
141149
fun getCalendarItemDTOById(id: Int): CalendarItemWithCalendarsDTO {
142150
return mapper.toDtoWithCalendars(getItemById(id))
143151
}

0 commit comments

Comments
 (0)