Skip to content

Added some try for Template pattern #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/kotlin/oop/Template/HttpRequest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 8 additions & 0 deletions src/main/kotlin/oop/Template/Service.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package oop.Template

interface Service<T, U> {
fun findOne(id: U): T?
fun findAll() : Collection<T>
fun add(item: T): T
fun remove(id: U): Boolean
}
11 changes: 11 additions & 0 deletions src/main/kotlin/oop/Template/StringController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oop.Template

class StringController(val service: StringService) : TemplateController<String, String>(service) {
override fun findOne(id: String): String? = service.findOne(id)

override fun findAll(): Collection<String> = service.findAll()

override fun add(item: String): String = service.add(item)

override fun remove(id: String): Boolean = service.remove(id)
}
25 changes: 25 additions & 0 deletions src/main/kotlin/oop/Template/StringService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package oop.Template

class StringService : Service<String, String> {

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<String> =
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 })
}
43 changes: 43 additions & 0 deletions src/main/kotlin/oop/Template/TemplateController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package oop.Template

abstract class TemplateController<T, E>(private val service: Service<T, E>) {

abstract fun findOne(id: E) : T?
abstract fun findAll() : Collection<T>
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"

}