Last active
June 11, 2020 10:02
-
-
Save KunalChaubal/21d6f256210807959f9734fa81469ce2 to your computer and use it in GitHub Desktop.
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
fun Route.customer(customerRepo: CustomerRepo) { | |
route("/customer") { | |
// http://127.0.0.1:8080/customer | |
get { | |
call.respond(HttpStatusCode.OK, customerRepo.customerList) | |
} | |
// http://127.0.0.1:8080/customer/1 | |
get("/{customerId}") { | |
val customerId = call.parameters["customerId"]?.toInt() | |
val requestCustomer = customerRepo.customerList.firstOrNull { it.id == customerId } | |
if (requestCustomer != null) { | |
call.respond(HttpStatusCode.OK, requestCustomer) | |
} else { | |
call.respond(HttpStatusCode.NotFound, Response("Record Not Found")) | |
} | |
} | |
// http://127.0.0.1:8080/customer/create | |
post("/create") { | |
val customer = call.receive<Customer>() | |
customerRepo.customerList.add(customer) | |
call.respond(HttpStatusCode.OK, Response("Record Created")) | |
} | |
// http://127.0.0.1:8080/customer/delete/1 | |
delete("/delete/{customerId}") { | |
val customerId = call.parameters["customerId"]?.toInt() | |
val isRemoved = customerRepo.customerList.removeIf { it.id == customerId } | |
if (isRemoved) { | |
call.respond(Response("Record Deleted")) | |
} else { | |
call.respond(Response("No Such Customer Found")) | |
} | |
} | |
// http://127.0.0.1:8080/customer/update/1 | |
put("/update/{customerId}") { | |
val customerId = call.parameters["customerId"]?.toInt() | |
val customerModel = call.receive<Customer>() | |
var isUpdated = false | |
customerRepo.customerList.mapIndexed { index, customer -> | |
if (customer.id == customerId) { | |
customerRepo.customerList[index] = customerModel | |
isUpdated = true | |
} | |
} | |
if (isUpdated) { | |
call.respond(Response("Record Updated")) | |
} else { | |
call.respond(Response("No Such Customer Found")) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment