-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTemplateController.kt
More file actions
43 lines (32 loc) · 1.12 KB
/
TemplateController.kt
File metadata and controls
43 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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"
}