From 16784e4afff836e37491dd38bf08e48913e1a8d3 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Tue, 19 Nov 2024 14:11:19 +0100 Subject: [PATCH] Add addType to TypeController --- http/type.http | 9 +++++++++ .../ltd/hlaeja/controller/TypeController.kt | 8 ++++++++ .../hlaeja/controller/TypeControllerTest.kt | 19 +++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/http/type.http b/http/type.http index 279964d..b307552 100644 --- a/http/type.http +++ b/http/type.http @@ -1,2 +1,11 @@ ### let all types GET {{hostname}}/types + + +### add type by name +POST {{hostname}}/type +Content-Type: application/json + +{ + "name": "Test C" +} diff --git a/src/main/kotlin/ltd/hlaeja/controller/TypeController.kt b/src/main/kotlin/ltd/hlaeja/controller/TypeController.kt index 5c0d5e0..4b155b4 100644 --- a/src/main/kotlin/ltd/hlaeja/controller/TypeController.kt +++ b/src/main/kotlin/ltd/hlaeja/controller/TypeController.kt @@ -4,8 +4,11 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map import ltd.hlaeja.library.deviceRegistry.Type import ltd.hlaeja.service.TypeService +import ltd.hlaeja.util.toTypeEntity import ltd.hlaeja.util.toTypeResponse import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RestController @RestController @@ -15,4 +18,9 @@ class TypeController( @GetMapping("/types") fun getTypes(): Flow = service.getTypes().map { it.toTypeResponse() } + + @PostMapping("/type") + suspend fun addType( + @RequestBody register: Type.Request, + ): Type.Response = service.addType(register.toTypeEntity()).toTypeResponse() } diff --git a/src/test/kotlin/ltd/hlaeja/controller/TypeControllerTest.kt b/src/test/kotlin/ltd/hlaeja/controller/TypeControllerTest.kt index 87a6e3b..8aa153d 100644 --- a/src/test/kotlin/ltd/hlaeja/controller/TypeControllerTest.kt +++ b/src/test/kotlin/ltd/hlaeja/controller/TypeControllerTest.kt @@ -1,5 +1,7 @@ package ltd.hlaeja.controller +import io.mockk.coEvery +import io.mockk.coVerify import io.mockk.every import io.mockk.mockk import io.mockk.verify @@ -11,6 +13,7 @@ import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.single import kotlinx.coroutines.test.runTest import ltd.hlaeja.entity.TypeEntity +import ltd.hlaeja.library.deviceRegistry.Type import ltd.hlaeja.service.TypeService import ltd.hlaeja.assertj.assertThat import org.assertj.core.api.Assertions.assertThat @@ -46,4 +49,20 @@ class TypeControllerTest { assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000000") assertThat(response.name).isEqualTo("name") } + + @Test + fun `add types`() = runTest { + // given + val request = Type.Request("name") + coEvery { service.addType(any()) } returns TypeEntity(id, timestamp, "name") + + // when + val response = controller.addType(request) + + // then + coVerify(exactly = 1) { service.addType(any()) } + + assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000000") + assertThat(response.name).isEqualTo("name") + } }