-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
105 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cafe.nes.dao | ||
|
||
import cafe.nes.models.Message | ||
|
||
interface MessageDAO { | ||
suspend fun getMessages(): List<Message> | ||
suspend fun boardMessage(msg: Message): Message? | ||
suspend fun deleteMessage(id: Int): Boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package cafe.nes.dao | ||
|
||
import cafe.nes.dao.DatabaseSingleton.dbQuery | ||
import cafe.nes.models.Message | ||
import cafe.nes.models.Messages | ||
import org.jetbrains.exposed.sql.* | ||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq | ||
|
||
class MessageDAOImpl : MessageDAO { | ||
private fun resultRowToMessage(row: ResultRow) = Message( | ||
id = row[Messages.id], | ||
content = row[Messages.content], | ||
userId = row[Messages.userId] | ||
) | ||
|
||
override suspend fun getMessages(): List<Message> = dbQuery { | ||
Messages.selectAll().map { resultRowToMessage(it) } | ||
} | ||
|
||
override suspend fun boardMessage(msg: Message): Message? = dbQuery { | ||
val insert = Messages.insert { | ||
it[content] = msg.content | ||
it[userId] = msg.userId | ||
} | ||
insert.resultedValues?.singleOrNull()?.let { resultRowToMessage(it) } | ||
} | ||
|
||
override suspend fun deleteMessage(id: Int): Boolean = dbQuery { | ||
Messages.deleteWhere { Messages.id eq id } > 0 | ||
} | ||
} | ||
|
||
val messageSvc: MessageDAO = MessageDAOImpl() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cafe.nes.models | ||
|
||
import kotlinx.serialization.Serializable | ||
import org.jetbrains.exposed.sql.* | ||
import java.time.LocalDateTime | ||
|
||
@Serializable | ||
data class Message( | ||
val id: Int, | ||
val content: String, | ||
val userId: Int?, | ||
val time: String = LocalDateTime.now().toString(), | ||
) | ||
|
||
object Messages : Table() { | ||
val id = integer("id").autoIncrement() | ||
val content = varchar("content", 255) | ||
val userId = integer("userId").references(Users.id).nullable() | ||
val time: LocalDateTime = LocalDateTime.now() | ||
|
||
override val primaryKey = PrimaryKey(Users.id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cafe.nes.routes | ||
|
||
import cafe.nes.dao.messageSvc | ||
import cafe.nes.models.Message | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
|
||
fun Route.messageRouting() { | ||
route("/message") { | ||
|
||
get("/") { | ||
call.respondText(messageSvc.getMessages().toString()) | ||
} | ||
|
||
post("/") { | ||
val received = call.receive<Message>() | ||
val msg = messageSvc.boardMessage(msg = received) | ||
call.respondText(msg.toString()) | ||
} | ||
|
||
delete("/{id}") { | ||
val id = call.parameters["id"]?.toInt() | ||
if (id == null) { | ||
call.respond(HttpStatusCode.BadRequest, "id is required") | ||
return@delete | ||
} | ||
val res = messageSvc.deleteMessage(id) | ||
call.respondText(res.toString()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters