Skip to content

Commit 6ac7592

Browse files
committed
feat: decimal problem fixed
1 parent 7fe5f4b commit 6ac7592

File tree

7 files changed

+35
-40
lines changed

7 files changed

+35
-40
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.dto
2+
3+
import kotlinx.serialization.Serializable
4+
5+
@Serializable
6+
data class ItemDto(
7+
val id: Int,
8+
val orderId: Int,
9+
val foodId: Int,
10+
val quantity: Int,
11+
val price: Double
12+
)

src/main/kotlin/com/example/dto/OrderDto.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.example.dto
22

3-
import com.example.util.BigDecimalSerializer
43
import kotlinx.datetime.Instant
54
import kotlinx.serialization.Serializable
65
import java.math.BigDecimal
7-
import java.util.UUID
86

9-
@Serializable(with = BigDecimalSerializer::class)
7+
@Serializable
108
data class OrderDto(
119
val id: Int,
12-
val userId: UUID,
10+
val userId: String,
1311
val orderTime: Instant,
1412
val orderStatus: OrderStatus = OrderStatus.Pending,
15-
val totalPrice: BigDecimal,
13+
val totalPrice: Double,
1614
val paymentDetails: String? = null,
1715
)
1816

src/main/kotlin/com/example/dto/OrderItemDto.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/main/kotlin/com/example/service/DatabaseModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.example.service
22

33
import com.example.table.Foods
4-
import com.example.table.OrderItems
4+
import com.example.table.Items
55
import com.example.table.Orders
66
import com.example.table.Users
77
import kotlinx.coroutines.Dispatchers
@@ -20,7 +20,7 @@ object DatabaseModule {
2020
SchemaUtils.create(Users)
2121
SchemaUtils.create(Foods)
2222
SchemaUtils.create(Orders)
23-
SchemaUtils.create(OrderItems)
23+
SchemaUtils.create(Items)
2424
}
2525
}
2626

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import com.example.dto.OrderStatus
66
import com.example.service.DatabaseModule.dbQuery
77
import com.example.service.OrderService
88
import com.example.table.Foods
9-
import com.example.table.OrderItems
9+
import com.example.table.Items
1010
import com.example.table.Orders
11-
import com.example.util.ext.toFoodDto
1211
import com.example.util.ext.toOrderDto
1312
import org.jetbrains.exposed.sql.and
1413
import 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
}

src/main/kotlin/com/example/table/OrderItems.kt renamed to src/main/kotlin/com/example/table/Items.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.example.table
22

33
import org.jetbrains.exposed.sql.Table
44

5-
object OrderItems : Table() {
5+
object Items : Table() {
66
val id = integer("id").autoIncrement()
77
val orderId = integer("orderId").references(Orders.id)
88
val foodId = integer("foodId").references(Foods.id)

src/main/kotlin/com/example/util/ext/ResultRow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ fun ResultRow.toFoodDto(): FoodDto = FoodDto(
2323

2424
fun ResultRow.toOrderDto(): OrderDto = OrderDto(
2525
id = this[Orders.id],
26-
userId = this[Orders.userId],
26+
userId = this[Orders.userId].toString(),
2727
orderTime = this[Orders.orderTime].toKotlinInstant(),
2828
orderStatus = this[Orders.orderStatus],
29-
totalPrice = this[Orders.totalPrice],
29+
totalPrice = this[Orders.totalPrice].toDouble(),
3030
paymentDetails = this[Orders.paymentDetails]
3131
)
3232

0 commit comments

Comments
 (0)