Blog Infos
Author
Published
Topics
, ,
Published
Topics
, ,
Endpoints
POST http://0.0.0.0:3536/notes

2. Fetch all notes

GET http://0.0.0.0:3536/notes

3. Fetch a particular note

GET http://0.0.0.0:3536/notes/{id}

4. Delete a particular note

DELETE http://0.0.0.0:3536/notes/{id}

5. Update a particular note

PUT http://0.0.0.0:3536/notes/{id}
Database
database.insert(Notes) {
 set(it.id, "id")
 set(it.note, "note")
}
view raw KtormQuery.kt hosted with ❤ by GitHub

Generated SQL:

insert into t_note (id,note) values (?, ?)
view raw RawDBQuery.kt hosted with ❤ by GitHub

Job Offers

Job Offers


    Android Developer

    Small and Modern GmbH
    Hamburg, Remote (Germany)
    • Full Time
    apply now

    Android Build Engineer

    Pinterest
    San Francisco, CA | Seattle, WA
    • Full Time
    apply now

    Senior Android Developer (Remote)

    Komoot
    Europe
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

Jobs

Routing
routing {
get("/notes") {
val notes = arraylistof("Note 1","Note 2")
call.respond(notes)
}
}
view raw Routing.kt hosted with ❤ by GitHub
Request and Response
1. Request Body
@Serializable
data class NoteRequest(val note: String)
view raw NoteRequest.kt hosted with ❤ by GitHub

b) Get the Request Body parameters in the Routing Function:

val request = call.receive<NoteRequest>()
val result = db.insert(NotesEntity) {
set(it.note, request.note)
}
view raw InsertNote.kt hosted with ❤ by GitHub
2. Response Body
@Serializable
data class NoteResponse<T>(
val data: T,
val success: Boolean
)
view raw NoteResponse.kt hosted with ❤ by GitHub

b) Handle returning the response body on API call by the client.

get("/notes") {
val notes = db.from(NotesResponse).select().map {
val id = it[NotesEntity.id]
val note = it[NotesEntity.note]
Note(id ?: -1, note ?: "")
}
// Returns the response to the User
call.respond(notes)
}
view raw FetchNotes.kt hosted with ❤ by GitHub
Connecting the dots
fun Application.notesRoutes() {
val db = DatabaseConnection.database
routing {
get("/notes") {
val notes = db.from(NotesEntity).select().map {
val id = it[NotesEntity.id]
val note = it[NotesEntity.note]
Note(id ?: -1, note ?: "")
}
call.respond(notes)
}
post("/notes") {
val request = call.receive<NoteRequest>()
val result = db.insert(NotesEntity) {
set(it.note, request.note)
}
// Verify only 1 row has been inserted
if (result == 1) {
// To Successful response to the client
call.respond(
HttpStatusCode.OK,
NoteResponse(
success = true,
data = "Value has been successfully inserted"
)
)
} else {
// Send Failure response to the client
call.respond(
HttpStatusCode.BadRequest,
NoteResponse(
success = false,
data = "Failed to Insert Value"
)
)
}
}
get("/notes/{id}") {
val id: Int = call.parameters["id"]?.toInt() ?: -1
val note = db.from(NotesEntity)
.select()
.where { NotesEntity.id eq id }
.map {
val note = it[NotesEntity.note]!!
Note(id = id, note = note)
}.firstOrNull()
if (note == null) {
call.respond(
HttpStatusCode.NotFound,
NoteResponse(success = false, data = "Could not find note with that id = $id")
)
} else {
call.respond(
HttpStatusCode.OK,
NoteResponse(success = true, data = note)
)
}
}
put("/notes/{id}") {
val id = call.parameters["id"]?.toInt() ?: -1
val updatedNote = call.receive<NoteRequest>()
val rowsEffected = db.update(NotesEntity) {
set(it.note, updatedNote.note)
where {
it.id eq id
}
}
if (rowsEffected != 1) {
call.respond(
HttpStatusCode.NotFound,
NoteResponse(success = false, data = "Could not find note with that id = $id")
)
} else {
call.respond(
HttpStatusCode.OK,
NoteResponse(success = true, data = "Success")
)
}
}
delete("/notes/{id}") {
val id: Int = call.parameters["id"]?.toInt() ?: -1
val rowsEffected = db.delete(NotesEntity) {
it.id eq id
}
if (rowsEffected != 1) {
call.respond(
HttpStatusCode.NotFound,
NoteResponse(success = false, data = "Could not find note with that id = $id")
)
} else {
call.respond(
HttpStatusCode.OK,
NoteResponse(success = true, data = "Success")
)
}
}
}
}
view raw NotesRoute.kt hosted with ❤ by GitHub

For complete code, check out the repo ktor-api
Thanks for your patience, will be back soon with a new article related to Ktor and continue the project. Stay tuned.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
This tutorial is the second part of the series. It’ll be focussed on developing…
READ MORE
blog
Ktor is an asynchronous framework used in development of microservices and web applications. Kotlin…
READ MORE
blog
Retrofit is every Android developer’s favourite library for making API calls and there is…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu