11package com.example.service.impl
22
3- import com.example.dto.*
3+ import com.example.dto.FoodDto
4+ import com.example.dto.GetOrderWithItemsDto
5+ import com.example.dto.OrderStatus
46import com.example.service.DatabaseModule.dbQuery
57import com.example.service.OrderService
68import com.example.table.Foods
79import com.example.table.Items
810import com.example.table.Orders
911import com.example.util.ext.toItemDto
1012import com.example.util.ext.toOrderDto
11- import kotlinx.coroutines.flow.*
13+ import kotlinx.coroutines.flow.Flow
14+ import kotlinx.coroutines.flow.asFlow
1215import org.jetbrains.exposed.sql.*
1316import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
1417import java.util.*
@@ -56,11 +59,11 @@ class OrderServiceImpl : OrderService {
5659 it[Items .orderId] = activeOrder[Orders .id]
5760 it[Items .foodId] = foodDto.id
5861 it[Items .quantity] = quantity
59- it[Items .price] = foodDto.price.toBigDecimal()
62+ it[Items .price] = foodDto.price
6063 }[Items .id]
6164 }
6265
63- val totalPrice = activeOrder[Orders .totalPrice] + ( foodDto.price * quantity) .toBigDecimal()
66+ val totalPrice = activeOrder[Orders .totalPrice] + foodDto.price * quantity.toBigDecimal()
6467 Orders .update({ Orders .id eq activeOrder[Orders .id] }) {
6568 it[Orders .totalPrice] = totalPrice
6669 }
@@ -75,26 +78,27 @@ class OrderServiceImpl : OrderService {
7578 Orders .select { Orders .id eq item[Items .orderId] }.singleOrNull() ? : throw Exception (" Order not found" )
7679 val newQuantity = item[Items .quantity] + quantity
7780
78- if (newQuantity < 0 ) {
79- Items .deleteWhere { Items .id eq itemId }
80-
81- val newPrice = order[Orders .totalPrice] - (food[Foods .price] * item[Items .quantity]).toBigDecimal()
82- Orders .update({ Orders .id eq item[Items .orderId] }) {
83- it[Orders .totalPrice] = newPrice
81+ when {
82+ newQuantity < 0 -> {
83+ Items .deleteWhere { Items .id eq itemId }
84+ val newPrice = order[Orders .totalPrice] - (food[Foods .price] * item[Items .quantity].toBigDecimal())
85+ Orders .update({ Orders .id eq item[Items .orderId] }) {
86+ it[Orders .totalPrice] = newPrice
87+ }
8488 }
8589
86- return @dbQuery true
87- }
90+ else -> {
91+ val newPrice = food[Foods .price] * newQuantity.toBigDecimal()
92+ Items .update({ Items .id eq itemId }) {
93+ it[Items .quantity] = newQuantity
94+ }
8895
89- val newPrice = food[Foods .price] * newQuantity
90- Items .update({ Items .id eq itemId }) {
91- it[Items .quantity] = newQuantity
92- }
96+ Orders .update({ Orders .id eq item[Items .orderId] }) {
97+ it[Orders .totalPrice] = newPrice
98+ }
9399
94- Orders .update({ Orders .id eq item[Items .orderId] }) {
95- it[Orders .totalPrice] = newPrice.toBigDecimal()
100+ }
96101 }
97-
98102 true
99103 }
100104
0 commit comments