@@ -6,9 +6,8 @@ import com.example.dto.OrderStatus
66import com.example.service.DatabaseModule.dbQuery
77import com.example.service.OrderService
88import com.example.table.Foods
9- import com.example.table.OrderItems
9+ import com.example.table.Items
1010import com.example.table.Orders
11- import com.example.util.ext.toFoodDto
1211import com.example.util.ext.toOrderDto
1312import org.jetbrains.exposed.sql.and
1413import org.jetbrains.exposed.sql.insert
@@ -40,12 +39,12 @@ class OrderServiceImpl : OrderService {
4039 (Orders .userId eq userId) and (Orders .orderStatus eq OrderStatus .Started )
4140 }.forUpdate().single()
4241
43- val orderId = OrderItems .insert {
44- it[OrderItems .orderId] = activeOrder[Orders .id]
45- it[OrderItems .foodId] = foodDto.id
46- it[OrderItems .quantity] = quantity
47- it[OrderItems .price] = foodDto.price.toBigDecimal()
48- }[OrderItems .id]
42+ val orderId = Items .insert {
43+ it[Items .orderId] = activeOrder[Orders .id]
44+ it[Items .foodId] = foodDto.id
45+ it[Items .quantity] = quantity
46+ it[Items .price] = foodDto.price.toBigDecimal()
47+ }[Items .id]
4948
5049 val totalPrice = activeOrder[Orders .totalPrice] + (foodDto.price * quantity).toBigDecimal()
5150 Orders .update({ Orders .id eq activeOrder[Orders .id] }) {
@@ -56,21 +55,21 @@ class OrderServiceImpl : OrderService {
5655 }
5756
5857 override suspend fun updateItemInOrder (orderId : Int , quantity : Int ): Result <Boolean > = dbQuery {
59- val orderItem = OrderItems .select { OrderItems .id eq orderId }.singleOrNull()
58+ val orderItem = Items .select { Items .id eq orderId }.singleOrNull()
6059 if (orderItem == null ) {
6160 throw Exception (" Order item not found" )
6261 }
6362
64- val food = Foods .select { Foods .id eq orderItem[OrderItems .foodId] }.single()
63+ val food = Foods .select { Foods .id eq orderItem[Items .foodId] }.single()
6564 val totalPrice = (food[Foods .price] * quantity).toBigDecimal()
66- OrderItems .update({ OrderItems .id eq orderId }) {
67- it[OrderItems .quantity] = quantity
68- it[OrderItems .price] = totalPrice
65+ Items .update({ Items .id eq orderId }) {
66+ it[Items .quantity] = quantity
67+ it[Items .price] = totalPrice
6968 }
7069
71- val activeOrder = Orders .select { Orders .id eq orderItem[OrderItems .orderId] }.single()
72- val orderItems = OrderItems .select { OrderItems .orderId eq activeOrder[Orders .id] }
73- val newTotalPrice = orderItems.sumOf { it[OrderItems .price].toDouble() }.toBigDecimal()
70+ val activeOrder = Orders .select { Orders .id eq orderItem[Items .orderId] }.single()
71+ val orderItems = Items .select { Items .orderId eq activeOrder[Orders .id] }
72+ val newTotalPrice = orderItems.sumOf { it[Items .price].toDouble() }.toBigDecimal()
7473 Orders .update({ Orders .id eq activeOrder[Orders .id] }) {
7574 it[Orders .totalPrice] = newTotalPrice
7675 }
0 commit comments