-
Notifications
You must be signed in to change notification settings - Fork 19
/
ImportContentEntryJobRoute.kt
58 lines (54 loc) · 2.27 KB
/
ImportContentEntryJobRoute.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
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.ustadmobile.lib.rest.domain.contententry.importcontent
import com.ustadmobile.core.db.UmAppDatabase
import com.ustadmobile.core.domain.contententry.importcontent.CancelImportContentEntryServerUseCase
import com.ustadmobile.door.ext.requireRemoteNodeIdAndAuth
import com.ustadmobile.lib.db.composites.ContentEntryImportJobProgress
import io.github.aakira.napier.Napier
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.response.header
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
fun Route.ImportContentEntryJobRoute(
json: Json,
dbFn: (ApplicationCall) -> UmAppDatabase,
cancelImportContentEntryServerUseCase: (ApplicationCall) -> CancelImportContentEntryServerUseCase,
) {
get("importjobs") {
val contentEntryUid = call.request.queryParameters["contententryuid"]?.toLong() ?: 0
val db = dbFn(call)
val inProgressJobs = db.contentEntryImportJobDao.findInProgressJobsByContentEntryUidAsync(
contentEntryUid
)
call.response.header("cache-control", "no-store")
call.respondText(
contentType = ContentType.Application.Json,
text = json.encodeToString(
ListSerializer(ContentEntryImportJobProgress.serializer()),
inProgressJobs
),
)
}
get("cancel") {
val jobUid = call.request.queryParameters["jobUid"]?.toLong() ?: 0
val accountPersonUid = call.request.queryParameters["accountPersonUid"]?.toLong() ?: 0
try {
val (fromNode, auth) = requireRemoteNodeIdAndAuth()
cancelImportContentEntryServerUseCase(call).invoke(
cjiUid = jobUid,
accountPersonUid = accountPersonUid,
remoteNodeId = fromNode,
nodeAuth = auth,
)
call.respond(HttpStatusCode.OK, "")
}catch(e: Throwable) {
Napier.w("CancelImportContentEntryServer: exception with cancel request", e)
}
}
}