@@ -3,11 +3,14 @@ package com.example.service.impl
33import com.example.dto.*
44import com.example.service.DatabaseModule.dbQuery
55import com.example.service.OrderService
6+ import com.example.table.Foods
67import com.example.table.Items
78import com.example.table.Orders
9+ import com.example.util.ext.toFoodDto
810import com.example.util.ext.toItemDto
911import com.example.util.ext.toOrderDto
1012import org.jetbrains.exposed.sql.*
13+ import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
1114import java.util.*
1215
1316class 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