This repository was archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathDelete.kt
45 lines (39 loc) · 1.51 KB
/
Delete.kt
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
44
45
package org.jetbrains.demo.thinkter
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.locations.get
import io.ktor.locations.post
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Route
import io.ktor.sessions.get
import io.ktor.sessions.sessions
import io.ktor.util.ValuesMap
import org.jetbrains.demo.thinkter.dao.ThinkterStorage
import org.jetbrains.demo.thinkter.model.PostThoughtToken
import org.jetbrains.demo.thinkter.model.RpcData
fun Route.delete(dao: ThinkterStorage, hashFunction: (String) -> String) {
get<ThoughtDelete> {
val user = call.sessions.get<Session>()?.let { dao.user(it.userId) }
val date = System.currentTimeMillis()
if (user == null) {
call.respond(HttpStatusCode.Forbidden)
} else {
val code = call.securityCode(date, user, hashFunction)
call.respond(PostThoughtToken(user.userId, date, code))
}
}
post<ThoughtDelete> {
val user = call.sessions.get<Session>()?.let { dao.user(it.userId) }
val thought = dao.getThought(it.id)
val form = call.receive<ValuesMap>()
val date = form["date"]?.toLong() ?: -1
val code = form["code"] ?: ""
if (user == null || thought.userId != user.userId || !call.verifyCode(date, user, code, hashFunction)) {
call.respond(HttpStatusCode.Forbidden)
} else {
dao.deleteThought(it.id)
call.respond(object : RpcData {})
}
}
}