From f2ab81e0f43e3d8d765344f092620ad7eae1f45f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Tue, 17 Aug 2021 16:16:37 +0200 Subject: [PATCH] add pagination and device id query --- src/api/route/SampleServiceServer.kt | 16 ++++++++++++++++ src/database/service/ISampleObjectService.kt | 3 +++ src/database/service/SampleObjectService.kt | 14 ++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/api/route/SampleServiceServer.kt b/src/api/route/SampleServiceServer.kt index d737110..83da1e2 100644 --- a/src/api/route/SampleServiceServer.kt +++ b/src/api/route/SampleServiceServer.kt @@ -25,6 +25,22 @@ class SampleServiceServer { call.respond(sampleObjectService.getAllSampleObjects()) } + get("/sample/count") { + call.respond(sampleObjectService.getCount()) + } + + get("/sample/page/{page}") { + val page = call.parameters["page"] ?: error(HttpStatusCode.NotAcceptable) + val pageNum = page.toLong() + call.respond(sampleObjectService.getPage(pageNum)) + } + + get("/sample/device/{device_id}") { + val deviceId = call.parameters["device_id"] ?: error(HttpStatusCode.NotAcceptable) + val deviceIdNum = deviceId.toInt() + call.respond(sampleObjectService.getSampleObjectbyDeviceID(deviceIdNum)) + } + get("/sample/after/{dateAfter}") { val dateAfter = call.parameters["dateAfter"] ?: error(HttpStatusCode.NotAcceptable) val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE diff --git a/src/database/service/ISampleObjectService.kt b/src/database/service/ISampleObjectService.kt index 4ee926f..0209e13 100644 --- a/src/database/service/ISampleObjectService.kt +++ b/src/database/service/ISampleObjectService.kt @@ -7,6 +7,9 @@ import java.util.* interface ISampleObjectService{ fun addOne(apiObject: ApiObject) suspend fun getAllSampleObjects(): List + suspend fun getCount(): Long + suspend fun getPage(page: Long): List + suspend fun getSampleObjectbyDeviceID(device_id: Int): List suspend fun getSampleObjectbyTag(tag: String): ApiObject? suspend fun getSampleObjectbyDate(date: LocalDate): List suspend fun getSampleObjectbeforeDate(date: LocalDate): List diff --git a/src/database/service/SampleObjectService.kt b/src/database/service/SampleObjectService.kt index 6b55294..898ffc0 100644 --- a/src/database/service/SampleObjectService.kt +++ b/src/database/service/SampleObjectService.kt @@ -29,6 +29,20 @@ class SampleObjectService : ISampleObjectService { SampleObjects.selectAll().map { toSampleObject(it) } } + override suspend fun getCount(): Long = dbQuery { + SampleObjects.selectAll().count() + } + + override suspend fun getPage(page: Long): List = dbQuery { + SampleObjects.selectAll().limit(10, page * 10).map { toSampleObject(it) } + } + + override suspend fun getSampleObjectbyDeviceID(device_id: Int): List = dbQuery { + SampleObjects.select { + (SampleObjects.device_id eq device_id) + }.mapNotNull { toSampleObject(it) } + } + override suspend fun getSampleObjectbyTag(tag: String): ApiObject? = dbQuery { SampleObjects.select { (SampleObjects.tag eq tag)