update for common messages v0.2.0

- change DeviceController addDevice to use toDeviceResponse
- add DeviceEntity.toDeviceResponse to Mapping.kt
This commit is contained in:
2024-12-10 21:24:04 +01:00
parent 5d36099738
commit d5746aa22d
2 changed files with 14 additions and 5 deletions

View File

@@ -3,11 +3,10 @@ package ltd.hlaeja.controller
import ltd.hlaeja.library.deviceRegistry.Device import ltd.hlaeja.library.deviceRegistry.Device
import ltd.hlaeja.service.DeviceService import ltd.hlaeja.service.DeviceService
import ltd.hlaeja.service.JwtService import ltd.hlaeja.service.JwtService
import org.springframework.http.HttpStatus.EXPECTATION_FAILED import ltd.hlaeja.util.toDeviceResponse
import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
@RestController @RestController
class DeviceController( class DeviceController(
@@ -18,7 +17,6 @@ class DeviceController(
@PostMapping("/device") @PostMapping("/device")
suspend fun addDevice( suspend fun addDevice(
@RequestBody request: Device.Request, @RequestBody request: Device.Request,
): Device.Identity = deviceService.addDevice(request.type) ): Device.Response = deviceService.addDevice(request.type)
.let { jwtService.makeIdentity(it.id ?: throw ResponseStatusException(EXPECTATION_FAILED)) } .toDeviceResponse(jwtService)
.let { Device.Identity(it) }
} }

View File

@@ -1,11 +1,14 @@
package ltd.hlaeja.util package ltd.hlaeja.util
import java.time.ZonedDateTime import java.time.ZonedDateTime
import ltd.hlaeja.entity.DeviceEntity
import ltd.hlaeja.entity.NodeEntity import ltd.hlaeja.entity.NodeEntity
import ltd.hlaeja.entity.TypeEntity import ltd.hlaeja.entity.TypeEntity
import ltd.hlaeja.library.deviceRegistry.Device
import ltd.hlaeja.library.deviceRegistry.Identity import ltd.hlaeja.library.deviceRegistry.Identity
import ltd.hlaeja.library.deviceRegistry.Node import ltd.hlaeja.library.deviceRegistry.Node
import ltd.hlaeja.library.deviceRegistry.Type import ltd.hlaeja.library.deviceRegistry.Type
import ltd.hlaeja.service.JwtService
import org.springframework.http.HttpStatus.EXPECTATION_FAILED import org.springframework.http.HttpStatus.EXPECTATION_FAILED
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
@@ -36,3 +39,11 @@ fun NodeEntity.toIdentityResponse(): Identity.Response = Identity.Response(
id ?: throw ResponseStatusException(EXPECTATION_FAILED), id ?: throw ResponseStatusException(EXPECTATION_FAILED),
device, device,
) )
suspend fun DeviceEntity.toDeviceResponse(
jwtService: JwtService,
): Device.Response = Device.Response(
id ?: throw ResponseStatusException(EXPECTATION_FAILED),
type,
jwtService.makeIdentity(id),
)