-
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.
[customers] create customer routes (#1)
* feat: add customer routing * feat: implement all customer routes * feat: create customer model * [order route] implement new route order (#2) * feat: create order route * build: add cross-origin resource sharing (CORS) * test: create order route tests
- Loading branch information
1 parent
68b8f6a
commit 7e75716
Showing
9 changed files
with
183 additions
and
21 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
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,13 @@ | ||
package com.example.models | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class Customer( | ||
val id : String, | ||
val firstName : String, | ||
val lastName : String, | ||
val email : String | ||
) | ||
|
||
val customerStorage = mutableListOf<Customer>() |
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,31 @@ | ||
package com.example.models | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class Order( | ||
val number : String, | ||
val contents : List<OrderItem> | ||
) | ||
|
||
@Serializable | ||
data class OrderItem( | ||
val item : String, | ||
val amount : Int, | ||
val price : Double | ||
) | ||
|
||
val orderStorage = listOf(Order( | ||
"2020-04-06-01", listOf( | ||
OrderItem("Ham Sandwich", 2, 5.50), | ||
OrderItem("Water", 1, 1.50), | ||
OrderItem("Beer", 3, 2.30), | ||
OrderItem("Cheesecake", 1, 3.75) | ||
)), | ||
Order("2020-04-03-01", listOf( | ||
OrderItem("Cheeseburger", 1, 8.50), | ||
OrderItem("Water", 2, 1.50), | ||
OrderItem("Coke", 2, 1.76), | ||
OrderItem("Ice Cream", 1, 2.35) | ||
)) | ||
) |
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,17 @@ | ||
package com.example.plugins | ||
|
||
import com.example.routes.customerRouting | ||
import com.example.routes.getOrderRoute | ||
import com.example.routes.listOrdersRoute | ||
import com.example.routes.totalizeOrderRoute | ||
import io.ktor.server.application.* | ||
import io.ktor.server.routing.* | ||
|
||
fun Application.configureRouting() { | ||
routing { | ||
customerRouting() | ||
listOrdersRoute() | ||
getOrderRoute() | ||
totalizeOrderRoute() | ||
} | ||
} |
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,53 @@ | ||
package com.example.routes | ||
|
||
import com.example.models.Customer | ||
import com.example.models.customerStorage | ||
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.customerRouting() { | ||
route("/customer") { | ||
get { | ||
if (customerStorage.isNotEmpty()) { | ||
call.respond(customerStorage) | ||
} else { | ||
call.respondText("No customers found", status = HttpStatusCode.OK) | ||
} | ||
} | ||
get("{id?}") { | ||
|
||
val id = call.parameters["id"] ?: return@get call.respondText( | ||
"Missing id", | ||
status = HttpStatusCode.BadRequest | ||
) | ||
|
||
val customer = customerStorage.find { it.id == id } ?: return@get call.respondText( | ||
"No customer with id $id", | ||
status = HttpStatusCode.NotFound | ||
) | ||
call.respond(customer) | ||
} | ||
post { | ||
|
||
val customer = call.receive<Customer>() | ||
customerStorage.add(customer) | ||
call.respondText("Custom stored correctly", status = HttpStatusCode.Created) | ||
|
||
} | ||
delete("{id?}") { | ||
|
||
val id = call.parameters["id"] ?: return@delete call.respond(HttpStatusCode.BadRequest) | ||
if (customerStorage.removeIf { it.id == id }) { | ||
call.respondText( | ||
"Customer removed!", | ||
status = HttpStatusCode.Accepted | ||
) | ||
} else { | ||
call.respondText("Not Found", status = HttpStatusCode.NotFound) | ||
} | ||
} | ||
} | ||
} |
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,42 @@ | ||
package com.example.routes | ||
|
||
import com.example.models.orderStorage | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
|
||
fun Route.listOrdersRoute() { | ||
get("/order") { | ||
if (orderStorage.isNotEmpty()) { | ||
call.respond(orderStorage) | ||
} | ||
} | ||
} | ||
|
||
fun Route.getOrderRoute() { | ||
get("/order/{id?}") { | ||
val id = call.parameters["id"] ?: return@get call.respondText( | ||
"Bad Request", status = HttpStatusCode.BadRequest | ||
) | ||
val order = orderStorage.find { it.number == id } ?: return@get call.respondText( | ||
"Order Not Found", | ||
status = HttpStatusCode.NotFound | ||
) | ||
call.respond(order) | ||
} | ||
} | ||
|
||
fun Route.totalizeOrderRoute() { | ||
get("/order/{id?}/total") { | ||
val id = call.parameters["id"] ?: return@get call.respondText( | ||
"Bad Request", status = HttpStatusCode.BadRequest | ||
) | ||
val order = orderStorage.find { it.number == id } ?: return@get call.respondText( | ||
"Not found", | ||
status = HttpStatusCode.NotFound | ||
) | ||
val total = order.contents.sumOf { it.price * it.amount } | ||
call.respond(total) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
package com.example | ||
|
||
import com.example.plugins.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import io.ktor.server.testing.* | ||
import kotlin.test.* | ||
|
||
class OrderRouteTests { | ||
|
||
@Test | ||
fun testGetOrder() = testApplication { | ||
val response = client.get("/order/2020-04-06-01") | ||
assertEquals( | ||
"""{"number":"2020-04-06-01","contents":[{"item":"Ham Sandwich","amount":2,"price":5.5},{"item":"Water","amount":1,"price":1.5},{"item":"Beer","amount":3,"price":2.3},{"item":"Cheesecake","amount":1,"price":3.75}]}""", | ||
response.bodyAsText() | ||
) | ||
assertEquals(HttpStatusCode.OK, response.status) | ||
} | ||
} |