Skip to content

Commit 6be6953

Browse files
committed
feat: delete order and added minus operation to update
1 parent c048a6e commit 6be6953

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,31 @@ fun Application.orderRoute() {
4848
onFailure = { call.respond(HttpStatusCode.NotFound, it.message ?: "No active order found") }
4949
)
5050
}
51+
52+
delete("/orders/{userId}/current") {
53+
val userId = call.parameters["userId"].let { UUID.fromString(it) } ?: run {
54+
call.respond(HttpStatusCode.BadRequest, INVALID_USER_ID)
55+
return@delete
56+
}
57+
orderService.deleteCurrentOrder(userId).fold(
58+
onSuccess = { call.respond(HttpStatusCode.OK, true) },
59+
onFailure = { call.respond(HttpStatusCode.InternalServerError, it.message ?: "Unknown error") }
60+
)
61+
}
62+
63+
delete("/orders/{userId}/{orderId}") {
64+
val userId = call.parameters["userId"].let { UUID.fromString(it) } ?: run {
65+
call.respond(HttpStatusCode.BadRequest, INVALID_USER_ID)
66+
return@delete
67+
}
68+
val orderId = call.parameters["orderId"]?.toIntOrNull() ?: run {
69+
call.respond(HttpStatusCode.BadRequest, "Invalid order id")
70+
return@delete
71+
}
72+
orderService.deleteOrder(userId, orderId).fold(
73+
onSuccess = { call.respond(HttpStatusCode.OK, true) },
74+
onFailure = { call.respond(HttpStatusCode.InternalServerError, it.message ?: "Unknown error") }
75+
)
76+
}
5177
}
5278
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import java.util.*
99
interface OrderService {
1010
suspend fun getActiveOrder(userId: UUID): Result<GetActiveOrderDto>
1111
suspend fun addFoodToOrder(userId: UUID, foodDto: FoodDto, quantity: Int): Result<Int>
12-
suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<ItemDto>
13-
suspend fun clearCurrentOrder(userId: UUID): Result<Boolean>
12+
suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<Boolean>
13+
suspend fun deleteCurrentOrder(userId: UUID): Result<Boolean>
14+
suspend fun deleteOrder(userId: UUID, orderId: Int): Result<Boolean>
1415

1516
companion object {
1617
val orderService: OrderService by lazy { OrderServiceImpl() }

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

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,24 @@ class OrderServiceImpl : OrderService {
5959
itemId
6060
}
6161

62-
override suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<ItemDto> = dbQuery {
62+
override suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<Boolean> = dbQuery {
6363
val item = Items.select { Items.id eq itemId }.singleOrNull() ?: throw Exception("Item not found")
6464
val food = Foods.select { Foods.id eq item[Items.foodId] }.singleOrNull() ?: throw Exception("Food not found")
65-
65+
val order =
66+
Orders.select { Orders.id eq item[Items.orderId] }.singleOrNull() ?: throw Exception("Order not found")
6667
val newQuantity = item[Items.quantity] + quantity
68+
69+
if (newQuantity < 0) {
70+
Items.deleteWhere { Items.id eq itemId }
71+
72+
val newPrice = order[Orders.totalPrice] - (food[Foods.price] * item[Items.quantity]).toBigDecimal()
73+
Orders.update({ Orders.id eq item[Items.orderId] }) {
74+
it[Orders.totalPrice] = newPrice
75+
}
76+
77+
return@dbQuery true
78+
}
79+
6780
val newPrice = food[Foods.price] * newQuantity
6881
Items.update({ Items.id eq itemId }) {
6982
it[Items.quantity] = newQuantity
@@ -73,17 +86,31 @@ class OrderServiceImpl : OrderService {
7386
it[Orders.totalPrice] = newPrice.toBigDecimal()
7487
}
7588

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-
)
89+
true
90+
}
91+
92+
override suspend fun deleteCurrentOrder(userId: UUID): Result<Boolean> = dbQuery {
93+
val order = Orders.select {
94+
(Orders.userId eq userId) and (Orders.orderStatus eq OrderStatus.Started)
95+
}.singleOrNull() ?: throw Exception("No active order found")
96+
97+
Items.deleteWhere { Items.orderId eq order[Orders.id] }
98+
Orders.update({ Orders.id eq order[Orders.id] }) {
99+
it[orderStatus] = OrderStatus.Cancelled
100+
}
101+
102+
true
83103
}
84104

85-
override suspend fun clearCurrentOrder(userId: UUID): Result<Boolean> {
86-
TODO("Not yet implemented")
105+
override suspend fun deleteOrder(userId: UUID, orderId: Int): Result<Boolean> = dbQuery {
106+
val order = Orders.select {
107+
(Orders.userId eq userId) and (Orders.id eq orderId)
108+
}.singleOrNull() ?: throw Exception("Order not found")
109+
110+
Items.deleteWhere { Items.orderId eq order[Orders.id] }
111+
Orders.deleteWhere { Orders.id eq order[Orders.id] }
112+
113+
true
87114
}
88115

89116
private fun createOrder(userId: UUID) {

0 commit comments

Comments
 (0)