From 8e0800f7dfd6f446e9d09935506186c9583fd811 Mon Sep 17 00:00:00 2001 From: Cotel Date: Fri, 28 Apr 2017 18:15:52 +0200 Subject: [PATCH] Added some try for Template pattern --- src/main/kotlin/oop/Template/HttpRequest.kt | 10 +++++ src/main/kotlin/oop/Template/Service.kt | 8 ++++ .../kotlin/oop/Template/StringController.kt | 11 +++++ src/main/kotlin/oop/Template/StringService.kt | 25 +++++++++++ .../kotlin/oop/Template/TemplateController.kt | 43 +++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 src/main/kotlin/oop/Template/HttpRequest.kt create mode 100644 src/main/kotlin/oop/Template/Service.kt create mode 100644 src/main/kotlin/oop/Template/StringController.kt create mode 100644 src/main/kotlin/oop/Template/StringService.kt create mode 100644 src/main/kotlin/oop/Template/TemplateController.kt diff --git a/src/main/kotlin/oop/Template/HttpRequest.kt b/src/main/kotlin/oop/Template/HttpRequest.kt new file mode 100644 index 0000000..2ad063e --- /dev/null +++ b/src/main/kotlin/oop/Template/HttpRequest.kt @@ -0,0 +1,10 @@ +package oop.Template + +data class Body(val endpoint: String, + val parameter: String = "") + +sealed class HttpRequest(val body: Body) { + class Get(body: Body) : HttpRequest(body) + class Post(body: Body) : HttpRequest(body) + class Remove(body: Body) : HttpRequest(body) +} diff --git a/src/main/kotlin/oop/Template/Service.kt b/src/main/kotlin/oop/Template/Service.kt new file mode 100644 index 0000000..f328ea6 --- /dev/null +++ b/src/main/kotlin/oop/Template/Service.kt @@ -0,0 +1,8 @@ +package oop.Template + +interface Service { + fun findOne(id: U): T? + fun findAll() : Collection + fun add(item: T): T + fun remove(id: U): Boolean +} diff --git a/src/main/kotlin/oop/Template/StringController.kt b/src/main/kotlin/oop/Template/StringController.kt new file mode 100644 index 0000000..e3b9fcb --- /dev/null +++ b/src/main/kotlin/oop/Template/StringController.kt @@ -0,0 +1,11 @@ +package oop.Template + +class StringController(val service: StringService) : TemplateController(service) { + override fun findOne(id: String): String? = service.findOne(id) + + override fun findAll(): Collection = service.findAll() + + override fun add(item: String): String = service.add(item) + + override fun remove(id: String): Boolean = service.remove(id) +} diff --git a/src/main/kotlin/oop/Template/StringService.kt b/src/main/kotlin/oop/Template/StringService.kt new file mode 100644 index 0000000..0a18b52 --- /dev/null +++ b/src/main/kotlin/oop/Template/StringService.kt @@ -0,0 +1,25 @@ +package oop.Template + +class StringService : Service { + + private val fakeList = mutableListOf( + Pair("1", "Test 1"), + Pair("2", "Test 2"), + Pair("3", "Test 3") + ) + + override fun findOne(id: String): String? = + fakeList.firstOrNull { it.first == id }?.second + + override fun findAll(): Collection = + fakeList.map { it.second } + + override fun add(item: String): String { + val lastId = fakeList.last().first.toInt() + 1 + fakeList.add(Pair(lastId.toString(), item)) + return item + } + + override fun remove(id: String): Boolean = + fakeList.remove(fakeList.find { it.first == id }) +} diff --git a/src/main/kotlin/oop/Template/TemplateController.kt b/src/main/kotlin/oop/Template/TemplateController.kt new file mode 100644 index 0000000..af9e43b --- /dev/null +++ b/src/main/kotlin/oop/Template/TemplateController.kt @@ -0,0 +1,43 @@ +package oop.Template + +abstract class TemplateController(private val service: Service) { + + abstract fun findOne(id: E) : T? + abstract fun findAll() : Collection + abstract fun add(item: T) : T + abstract fun remove(id: E) : Boolean + + fun handleRequest(request: HttpRequest) : String { + when (request) { + + is HttpRequest.Get -> { + return handleGetRequest(request.body) + } + + is HttpRequest.Post -> { + return handlePostRequest(request.body) + } + + is HttpRequest.Remove -> { + return handleRemoveRequest(request.body) + } + + } + } + + private fun handleGetRequest(body: Body) : String = + when (body.endpoint) { + "findOne" -> findOne(body.parameter as E).toString() + "findAll" -> findAll().toString() + else -> "Error 404: Not Found" + } + + private fun handlePostRequest(body: Body) : String = + if (body.endpoint == "add") add(body.parameter as T).toString() + else "Error 404: Not Found" + + private fun handleRemoveRequest(body: Body) : String = + if (body.endpoint == "remove") remove(body.parameter as E).toString() + else "Error 404: Not Found" + +}