Skip to content

Commit f00d10e

Browse files
committed
fix: add order fixed for multiple items
1 parent 6ac7592 commit f00d10e

File tree

5 files changed

+59
-54
lines changed

5 files changed

+59
-54
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package com.example.dto
22

33
import kotlinx.datetime.Instant
44
import kotlinx.serialization.Serializable
5-
import java.math.BigDecimal
5+
6+
enum class OrderStatus {
7+
Pending, Started, Processing, Finished, Cancelled
8+
}
69

710
@Serializable
811
data class OrderDto(
@@ -14,6 +17,8 @@ data class OrderDto(
1417
val paymentDetails: String? = null,
1518
)
1619

17-
enum class OrderStatus {
18-
Pending, Started, Processing, Finished, Cancelled
19-
}
20+
@Serializable
21+
data class GetActiveOrderDto(
22+
val order: OrderDto,
23+
val items: List<ItemDto>
24+
)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fun Application.orderRoute() {
3232
}
3333
val quantity = call.receive<Int>()
3434
orderService.updateItemInOrder(itemId, quantity).fold(
35-
onSuccess = { call.respond(HttpStatusCode.OK) },
35+
onSuccess = { item -> call.respond(HttpStatusCode.OK, item) },
3636
onFailure = { call.respond(HttpStatusCode.InternalServerError, it.message ?: "Unknown error") }
3737
)
3838
}
@@ -43,7 +43,7 @@ fun Application.orderRoute() {
4343
return@get
4444
}
4545
orderService.getActiveOrder(userId).fold(
46-
onSuccess = { orderDto -> call.respond(orderDto) },
46+
onSuccess = { getActiveOrderDto -> call.respond(getActiveOrderDto) },
4747
onFailure = { call.respond(HttpStatusCode.NotFound, it.message ?: "No active order found") }
4848
)
4949
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.example.service
22

33
import com.example.dto.FoodDto
4-
import com.example.dto.OrderDto
4+
import com.example.dto.GetActiveOrderDto
5+
import com.example.dto.ItemDto
56
import com.example.service.impl.OrderServiceImpl
67
import java.util.*
78

89
interface OrderService {
9-
suspend fun getActiveOrder(userId: UUID): Result<OrderDto>
10+
suspend fun getActiveOrder(userId: UUID): Result<GetActiveOrderDto>
1011
suspend fun addFoodToOrder(userId: UUID, foodDto: FoodDto, quantity: Int): Result<Int>
11-
suspend fun updateItemInOrder(orderId: Int, quantity: Int): Result<Boolean>
12+
suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<ItemDto>
1213
suspend fun clearCurrentOrder(userId: UUID): Result<Boolean>
1314

1415
companion object {

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

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,69 @@
11
package com.example.service.impl
22

3-
import com.example.dto.FoodDto
4-
import com.example.dto.OrderDto
5-
import com.example.dto.OrderStatus
3+
import com.example.dto.*
64
import com.example.service.DatabaseModule.dbQuery
75
import com.example.service.OrderService
8-
import com.example.table.Foods
96
import com.example.table.Items
107
import com.example.table.Orders
8+
import com.example.util.ext.toItemDto
119
import com.example.util.ext.toOrderDto
12-
import org.jetbrains.exposed.sql.and
13-
import org.jetbrains.exposed.sql.insert
14-
import org.jetbrains.exposed.sql.select
15-
import org.jetbrains.exposed.sql.update
10+
import org.jetbrains.exposed.sql.*
1611
import java.util.*
1712

1813
class OrderServiceImpl : OrderService {
19-
override suspend fun getActiveOrder(userId: UUID): Result<OrderDto> = dbQuery {
20-
val card = Orders.select {
14+
override suspend fun getActiveOrder(userId: UUID): Result<GetActiveOrderDto> = dbQuery {
15+
val order = Orders.select {
2116
(Orders.userId eq userId) and (Orders.orderStatus eq OrderStatus.Started)
22-
}
23-
if (card.empty()) {
24-
throw Exception("No active order found")
25-
}
26-
card.single().toOrderDto()
17+
}.singleOrNull() ?: throw Exception("No active order found")
18+
19+
val items = Items.select { Items.orderId eq order[Orders.id] }.map { it.toItemDto() }
20+
GetActiveOrderDto(order.toOrderDto(), items)
2721
}
2822

2923
override suspend fun addFoodToOrder(userId: UUID, foodDto: FoodDto, quantity: Int): Result<Int> = dbQuery {
30-
val order = Orders.select {
24+
Orders.select {
3125
(Orders.userId eq userId) and (Orders.orderStatus eq OrderStatus.Started)
32-
}.forUpdate()
33-
34-
if (order.empty()) {
35-
createOrder(userId)
36-
}
26+
}.forUpdate().singleOrNull() ?: createOrder(userId)
3727

3828
val activeOrder = Orders.select {
3929
(Orders.userId eq userId) and (Orders.orderStatus eq OrderStatus.Started)
4030
}.forUpdate().single()
4131

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]
32+
val item = Items.select {
33+
(Items.orderId eq activeOrder[Orders.id]) and (Items.foodId eq foodDto.id)
34+
}.singleOrNull()
35+
36+
val itemId = if (item != null) {
37+
val newQuantity = item[Items.quantity] + quantity
38+
Items.update({ Items.id eq item[Items.id] }) {
39+
it[Items.quantity] = newQuantity
40+
}
41+
item[Items.id]
42+
} else {
43+
Items.insert {
44+
it[Items.orderId] = activeOrder[Orders.id]
45+
it[Items.foodId] = foodDto.id
46+
it[Items.quantity] = quantity
47+
it[Items.price] = foodDto.price.toBigDecimal()
48+
}[Items.id]
49+
}
4850

4951
val totalPrice = activeOrder[Orders.totalPrice] + (foodDto.price * quantity).toBigDecimal()
5052
Orders.update({ Orders.id eq activeOrder[Orders.id] }) {
5153
it[Orders.totalPrice] = totalPrice
5254
}
5355

54-
orderId
56+
itemId
5557
}
5658

57-
override suspend fun updateItemInOrder(orderId: Int, quantity: Int): Result<Boolean> = dbQuery {
58-
val orderItem = Items.select { Items.id eq orderId }.singleOrNull()
59-
if (orderItem == null) {
60-
throw Exception("Order item not found")
61-
}
59+
override suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<ItemDto> = dbQuery {
60+
val item = Items.select { Items.id eq itemId }.singleOrNull() ?: throw Exception("Item not found")
6261

63-
val food = Foods.select { Foods.id eq orderItem[Items.foodId] }.single()
64-
val totalPrice = (food[Foods.price] * quantity).toBigDecimal()
65-
Items.update({ Items.id eq orderId }) {
62+
Items.update({ Items.id eq itemId }) {
6663
it[Items.quantity] = quantity
67-
it[Items.price] = totalPrice
68-
}
69-
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()
73-
Orders.update({ Orders.id eq activeOrder[Orders.id] }) {
74-
it[Orders.totalPrice] = newTotalPrice
7564
}
7665

77-
true
66+
item.toItemDto()
7867
}
7968

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.example.util.ext
22

33
import com.example.dto.FoodDto
4+
import com.example.dto.ItemDto
45
import com.example.dto.OrderDto
56
import com.example.dto.UserDto
67
import com.example.table.Foods
8+
import com.example.table.Items
79
import com.example.table.Orders
810
import com.example.table.Users
911
import kotlinx.datetime.toKotlinInstant
@@ -43,3 +45,11 @@ fun ResultRow.toUserDto(): UserDto = UserDto(
4345
latitude = this[Users.latitude],
4446
longitude = this[Users.longitude]
4547
)
48+
49+
fun ResultRow.toItemDto(): ItemDto = ItemDto(
50+
id = this[Items.id],
51+
orderId = this[Items.orderId],
52+
foodId = this[Items.foodId],
53+
quantity = this[Items.quantity],
54+
price = this[Items.price].toDouble()
55+
)

0 commit comments

Comments
 (0)