Skip to content

Commit c048a6e

Browse files
committed
feat: update item in order added
1 parent f00d10e commit c048a6e

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/main/kotlin/com/example/route/OrderRoute.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ fun Application.orderRoute() {
2525
)
2626
}
2727

28-
post("/orders/items/{orderItemId}") {
29-
val itemId = call.parameters["orderItemId"]?.toIntOrNull() ?: run {
28+
post("/orders/items/{itemId}") {
29+
val itemId = call.parameters["itemId"]?.toIntOrNull() ?: run {
3030
call.respond(HttpStatusCode.BadRequest, INVALID_ORDER_ITEM_ID)
3131
return@post
3232
}
33+
3334
val quantity = call.receive<Int>()
3435
orderService.updateItemInOrder(itemId, quantity).fold(
3536
onSuccess = { item -> call.respond(HttpStatusCode.OK, item) },

src/main/kotlin/com/example/service/impl/OrderServiceImpl.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package com.example.service.impl
33
import com.example.dto.*
44
import com.example.service.DatabaseModule.dbQuery
55
import com.example.service.OrderService
6+
import com.example.table.Foods
67
import com.example.table.Items
78
import com.example.table.Orders
9+
import com.example.util.ext.toFoodDto
810
import com.example.util.ext.toItemDto
911
import com.example.util.ext.toOrderDto
1012
import org.jetbrains.exposed.sql.*
13+
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
1114
import java.util.*
1215

1316
class OrderServiceImpl : OrderService {
@@ -58,12 +61,25 @@ class OrderServiceImpl : OrderService {
5861

5962
override suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<ItemDto> = dbQuery {
6063
val item = Items.select { Items.id eq itemId }.singleOrNull() ?: throw Exception("Item not found")
64+
val food = Foods.select { Foods.id eq item[Items.foodId] }.singleOrNull() ?: throw Exception("Food not found")
6165

66+
val newQuantity = item[Items.quantity] + quantity
67+
val newPrice = food[Foods.price] * newQuantity
6268
Items.update({ Items.id eq itemId }) {
63-
it[Items.quantity] = quantity
69+
it[Items.quantity] = newQuantity
6470
}
6571

66-
item.toItemDto()
72+
Orders.update({ Orders.id eq item[Items.orderId] }) {
73+
it[Orders.totalPrice] = newPrice.toBigDecimal()
74+
}
75+
76+
ItemDto(
77+
id = item[Items.id],
78+
orderId = item[Items.orderId],
79+
foodId = item[Items.foodId],
80+
quantity = newQuantity,
81+
price = food[Foods.price]
82+
)
6783
}
6884

6985
override suspend fun clearCurrentOrder(userId: UUID): Result<Boolean> {

0 commit comments

Comments
 (0)