From eda6d6a3643bf919d7035f0126322459ebaddcf3 Mon Sep 17 00:00:00 2001 From: Aaron Davies <168211160+adaviesMOJ@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:22:31 +0000 Subject: [PATCH] Convert request params to DTO and improve logging (#132) * add more logging * add logger * Convert from request params to dto for easier usage --- .../controller/VisitAdminController.kt | 77 +++++-------------- .../admin/CreateSessionTemplateRequestDto.kt | 27 +++++++ 2 files changed, 47 insertions(+), 57 deletions(-) create mode 100644 src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/dto/admin/CreateSessionTemplateRequestDto.kt diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/controller/VisitAdminController.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/controller/VisitAdminController.kt index 31ccbf1..7d1be3f 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/controller/VisitAdminController.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/controller/VisitAdminController.kt @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.hmppsprisonvisitstestinghelperapi.controlle import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.media.Schema import io.swagger.v3.oas.annotations.responses.ApiResponse +import jakarta.validation.Valid import org.slf4j.Logger import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired @@ -14,13 +15,14 @@ import org.springframework.http.ResponseEntity import org.springframework.security.access.prepost.PreAuthorize import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PutMapping +import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestController import uk.gov.justice.digital.hmpps.hmppsprisonvisitstestinghelperapi.client.VisitSchedulerClient +import uk.gov.justice.digital.hmpps.hmppsprisonvisitstestinghelperapi.dto.admin.CreateSessionTemplateRequestDto import uk.gov.justice.digital.hmpps.hmppsprisonvisitstestinghelperapi.service.SessionService import java.time.LocalDate -import java.time.LocalDateTime const val ADD_PRISON_EXCLUDE_DATE: String = "/test/prison/{prisonCode}/add/exclude-date/{excludeDate}" const val REMOVE_PRISON_EXCLUDE_DATE: String = "/test/prison/{prisonCode}/remove/exclude-date/{excludeDate}" @@ -108,70 +110,31 @@ class VisitAdminController { ], ) fun addSessionTemplate( - @Schema(description = "prison code", example = "MDI", required = true) - @PathVariable - prisonCode: String, - @Schema(description = "sessionStartDateTime", example = "2007-12-03T10:15:30", required = false) - @RequestParam(required = false) - sessionStartDateTime: LocalDateTime = LocalDateTime.now().plusDays(2), - @Schema(description = "weeklyFrequency", example = "1", required = false) - @RequestParam(required = false) - weeklyFrequency: Int = 1, - @Schema(description = "closedCapacity", example = "1", required = false) - @RequestParam(required = false) - closedCapacity: Int = 1, - @Schema(description = "openCapacity", example = "1", required = false) - @RequestParam(required = false) - openCapacity: Int = 1, - @Schema(description = "Location level string", example = "A-1-3-007", required = false) - @RequestParam(required = false) - locationLevels: String?, - @Schema(description = "incentive string", example = "ENHANCED", required = false) - @RequestParam(required = false) - incentive: String?, - @Schema(description = "category string", example = "A_EXCEPTIONAL", required = false) - @RequestParam(required = false) - category: String?, - @Schema(description = "disable all other sessions for slot and prison", example = "false", required = false) - @RequestParam(required = false) - disableAllOtherSessionsForSlotAndPrison: Boolean = false, - @Schema(description = "Custom session name - if needed", example = "Saturday Mornings", required = false) - @RequestParam(required = false) - sessionName: String? = null, + @RequestBody + @Valid + createSessionTemplateRequestDto: CreateSessionTemplateRequestDto, ): ResponseEntity { - val startTime = sessionStartDateTime.toLocalTime() + val startTime = createSessionTemplateRequestDto.sessionStartDateTime.toLocalTime() val endTime = startTime.plusHours(2) - val slotDate = sessionStartDateTime.toLocalDate() - val validToDate = slotDate.plusDays(((1 * weeklyFrequency) + 1).toLong()) + val slotDate = createSessionTemplateRequestDto.sessionStartDateTime.toLocalDate() + val validToDate = slotDate.plusDays(((1 * createSessionTemplateRequestDto.weeklyFrequency) + 1).toLong()) - logger.debug( - "createSessionTemplate for slot:{}, weeklyFrequency: {}, prison:{}, slotDate:{}, validToDate: {}, openCapacity: {}, locationLevels: {}, closedCapacity: {}, incentive:{}, category: {}", - sessionStartDateTime, - weeklyFrequency, - prisonCode, - slotDate, - validToDate, - openCapacity, - locationLevels, - closedCapacity, - incentive, - category, - ) + logger.debug("createSessionTemplateRequestDto received - {}", createSessionTemplateRequestDto) val result = sessionService.createSessionTemplate( - sessionStartDateTime, + createSessionTemplateRequestDto.sessionStartDateTime, endTime, slotDate, validToDate, - prisonCode, - closedCapacity, - openCapacity, - weeklyFrequency, - locationLevels = locationLevels, - category = category, - incentive = incentive, - disableAllOtherSessionsForSlotAndPrison = disableAllOtherSessionsForSlotAndPrison, - customSessionName = sessionName, + createSessionTemplateRequestDto.prisonCode, + createSessionTemplateRequestDto.closedCapacity, + createSessionTemplateRequestDto.openCapacity, + createSessionTemplateRequestDto.weeklyFrequency, + locationLevels = createSessionTemplateRequestDto.locationLevels, + category = createSessionTemplateRequestDto.category, + incentive = createSessionTemplateRequestDto.incentive, + disableAllOtherSessionsForSlotAndPrison = createSessionTemplateRequestDto.disableAllOtherSessionsForSlotAndPrison, + customSessionName = createSessionTemplateRequestDto.sessionName, ) return ResponseEntity(result, CREATED) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/dto/admin/CreateSessionTemplateRequestDto.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/dto/admin/CreateSessionTemplateRequestDto.kt new file mode 100644 index 0000000..59372e1 --- /dev/null +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsprisonvisitstestinghelperapi/dto/admin/CreateSessionTemplateRequestDto.kt @@ -0,0 +1,27 @@ +package uk.gov.justice.digital.hmpps.hmppsprisonvisitstestinghelperapi.dto.admin + +import io.swagger.v3.oas.annotations.media.Schema +import java.time.LocalDateTime + +data class CreateSessionTemplateRequestDto( + @Schema(description = "prison code", example = "MDI", required = true) + val prisonCode: String, + @Schema(description = "sessionStartDateTime", example = "2007-12-03T10:15:30", required = false) + val sessionStartDateTime: LocalDateTime = LocalDateTime.now().plusDays(2), + @Schema(description = "weeklyFrequency", example = "1", required = false) + val weeklyFrequency: Int = 1, + @Schema(description = "closedCapacity", example = "1", required = false) + val closedCapacity: Int = 1, + @Schema(description = "openCapacity", example = "1", required = false) + val openCapacity: Int = 1, + @Schema(description = "Location level string", example = "A-1-3-007", required = false) + val locationLevels: String?, + @Schema(description = "incentive string", example = "ENHANCED", required = false) + val incentive: String?, + @Schema(description = "category string", example = "A_EXCEPTIONAL", required = false) + val category: String?, + @Schema(description = "disable all other sessions for slot and prison", example = "false", required = false) + val disableAllOtherSessionsForSlotAndPrison: Boolean = false, + @Schema(description = "Custom session name - if needed", example = "Saturday Mornings", required = false) + val sessionName: String? = null, +)