Commit 2b862a78 authored by Patrick Schlindwein's avatar Patrick Schlindwein
Browse files

[feat, #36] adds faq resource endpoints

parent ab7f2598
Pipeline #78326 passed with stages
in 2 minutes and 48 seconds
......@@ -2,6 +2,7 @@ package de.h_da.fbi.smebt.intentfinder.server.routing.upload
import com.papsign.ktor.openapigen.route.info
import com.papsign.ktor.openapigen.route.path.normal.NormalOpenAPIRoute
import com.papsign.ktor.openapigen.route.path.normal.get
import com.papsign.ktor.openapigen.route.path.normal.post
import com.papsign.ktor.openapigen.route.path.normal.put
import com.papsign.ktor.openapigen.route.response.respond
......@@ -11,23 +12,69 @@ import de.h_da.fbi.smebt.intentfinder.server.routing.upload.dto.requests.DocxUpl
import de.h_da.fbi.smebt.intentfinder.server.routing.upload.dto.requests.JsonReq
import de.h_da.fbi.smebt.intentfinder.server.routing.upload.dto.requests.BotIDPathParam
import de.h_da.fbi.smebt.intentfinder.server.routing.upload.dto.responses.DocxResp
import de.h_da.fbi.smebt.intentfinder.server.routing.upload.dto.responses.FAQDataOut
import de.h_da.fbi.smebt.intentfinder.server.routing.upload.dto.responses.JsonResp
import de.h_da.fbi.smebt.intentfinder.server.sources.*
import java.nio.file.Paths
fun NormalOpenAPIRoute.uploadRoutes() {
// TODO as soon as those methods are ported remove the `new/` prefix in the routing
route("/") {
route("upload") {
post<Any, Any, Any>(
route("faqResource/{chatbotId}/create") {
post<BotIDPathParam, String, JsonReq>(
info(
summary = "Upload file",
description = "Uploads a file"
)
) { _, body ->
// TODO What to actually do here? Code does nothing
// TODO Use file stream instead of multipart stuff
) { params, body ->
val handler = FileHandler(body.file)
try {
handler.parseJSON("url")
} catch (e: Exception) {
val msg = """
The uploaded file doesn't match the predefined structure. The only valid structure is:
{
"type": "url",
"name": "https://your.url",
"question": {
"type": "text",
"css_selector": "table.category > tbody td.list-title > a"
},
"answer": {
"type": "[text|href]",
"css_selector": "div.item-page > div:nth-child(5)"
}
}
The following field is missing or couldn't be parsed:
""".trimIndent()
respond(msg + e.message.toString())
}
handler.saveToFolder("faqResources/${params.chatbotId}")
}
}
route("files") {
get<Unit, List<FAQDataOut>>(
info(
summary = "Returns all saved .docx files",
description = "Searches for all the .docx files stored in the database and returns them."
),
example = listOf(FAQDataOut.EXAMPLE)
) {
val faqData = FAQDataRepository.instance.getAllFaqData()
.filter { it.type == "docx" }
.map {
FAQDataOut(
name = it.name,
id = it.id.toString(),
status = it.status
)
}
.toList()
respond(faqData)
}
}
......
package de.h_da.fbi.smebt.intentfinder.server.routing.upload.dto.responses
import com.papsign.ktor.openapigen.annotations.Response
import kotlinx.serialization.Serializable
@Serializable
@Response("FAQ data out.", statusCode = 200)
data class FAQDataOut(
val name: String,
val id: String,
val status: String
) {
companion object {
val EXAMPLE = FAQDataOut("https://your.url/", "547341", "New")
}
}
\ No newline at end of file
......@@ -14,6 +14,10 @@ class FAQDataRepository {
private val dbName = "chats"
private val collectionName = "faqData"
companion object {
val instance = FAQDataRepository()
}
init {
val client = KMongo.createClient()
database = client.getDatabase(dbName)
......@@ -71,7 +75,7 @@ class FAQDataRepository {
}
fun addTimestamp(id: String): Boolean {
var getFaqDataById = getFaqDataById(id)
val getFaqDataById = getFaqDataById(id)
var status = true
if (getFaqDataById.toString() == "null") {
status = false
......
......@@ -17,11 +17,12 @@ class FileHandler(private val file: NamedFileInputStream) {
val fileName = this.file.name
val folder = File(Paths.get(baseDir.toString(), folderName).toString())
if (!folder.exists()) {
folder.mkdir()
folder.mkdirs()
}
val file = File(Paths.get(folder.toString(), fileName).toString())
if (!file.exists()) {
file.createNewFile()
file.writeBytes(this.content)
return Status("created", Paths.get(folder.name, file.name).toString())
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment