Skip to content

Commit 13e3985

Browse files
committed
refactor: general
1 parent efd33e8 commit 13e3985

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

README.md

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ responses, [check out the DTOs here](/src/main/kotlin/com/example/dto).
6969
- **POST** `/orders/{userId}/items`
7070
- Adds a food item to the user's order.
7171
- **Path**: `userId` (UUID as string)
72-
- **Body**: `FoodDto` (JSON)
72+
- **Body**: `foodId` (Integer)
7373
- **Response**: `200 OK` with order ID, `500 Internal Server Error` with error message.
7474

7575
### Update Order Item Quantity
@@ -87,6 +87,24 @@ responses, [check out the DTOs here](/src/main/kotlin/com/example/dto).
8787
- **Path**: `userId` (UUID as string)
8888
- **Response**: `200 OK` with order details, `404 Not Found` with error message.
8989

90+
### Get All Order
91+
- **GET** `/orders/{userId}/all`
92+
- Retrieves all orders for the specified user.
93+
- **Path**: `userId` (UUID as string)
94+
- **Response**: `200 OK` with a list of all orders, `404 Not Found` with error message if no orders are found.
95+
96+
### Get All Order as Flow
97+
- **GET** `/orders/{userId}/all/flow`
98+
- Retrieves a real-time flow of all orders for the specified user.
99+
- **Path**: `userId` (UUID as string)
100+
- **Response**: `200 OK` with a flow of orders, `404 Not Found` with error message if no orders are found.
101+
102+
### Get Active Order as Flow
103+
- **GET** `/orders/{userId}/order/flow`
104+
- Retrieves a real-time flow of the user's active order.
105+
- **Path**: `userId` (UUID as string)
106+
- **Response**: `200 OK` with a flow of the active order, `404 Not Found` with error message if no active order is found.
107+
90108
### Complete Order
91109

92110
- **GET** `/orders/{userId}/complete`
@@ -108,6 +126,20 @@ responses, [check out the DTOs here](/src/main/kotlin/com/example/dto).
108126
- **Path**: `userId` (UUID as string), `orderId` (Integer)
109127
- **Response**: `200 OK` with deletion status, `500 Internal Server Error` with error message.
110128

129+
### Get All Orders
130+
131+
- **GET** `/orders/{userId}/all`
132+
- Retrieves all orders for the specified user.
133+
- **Path**: `userId` (UUID as string)
134+
- **Response**: `200 OK` with a list of all orders, `404 Not Found` with error message if no orders are found.
135+
136+
### Get All Orders as Flow
137+
138+
- **GET** `/orders/{userId}/all/flow`
139+
- Retrieves a real-time flow of all orders for the specified user.
140+
- **Path**: `userId` (UUID as string)
141+
- **Response**: `200 OK` with a flow of orders, `404 Not Found` with error message if no orders are found.
142+
111143
## Food API Endpoints
112144

113145
### Get Food Details
@@ -131,16 +163,6 @@ responses, [check out the DTOs here](/src/main/kotlin/com/example/dto).
131163
- **Query**: `type` (String, optional) - Category type, defaults to "all".
132164
- **Response**: `200 OK` with foods by category, `404 Not Found` with error message.
133165

134-
### Get All Orders
135-
136-
- **GET** `/orders/{userId}/all`
137-
- Retrieves all orders for the specified user.
138-
- **Path**: `userId` (UUID as string)
139-
- **Response**: `200 OK` with a list of all orders, `404 Not Found` with error message if no orders are found.
140-
141-
### Get All Orders as Flow
142-
143-
- **GET** `/orders/{userId}/all/flow`
144-
- Retrieves a real-time flow of all orders for the specified user.
145-
- **Path**: `userId` (UUID as string)
146-
- **Response**: `200 OK` with a flow of orders, `404 Not Found` with error message if no orders are found.
166+
- **GET** `/foods/categories/list`
167+
- Retrieves a list of all food categories.
168+
- **Response**: `200 OK` with list of food categories, `404 Not Found` with error message.

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

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

3-
import com.example.dto.FoodDto
43
import com.example.service.OrderService.Companion.orderService
54
import com.example.util.ext.getValueFromParameters
65
import io.ktor.http.*
@@ -14,8 +13,8 @@ fun Application.orderRoute() {
1413
routing {
1514
post("/orders/{userId}/items") {
1615
val userId = call.getValueFromParameters("userId", UUID::fromString)
17-
val foodDto = call.receive<FoodDto>()
18-
orderService.addFoodToOrder(userId, foodDto, 1).fold(
16+
val foodId = call.receive<Int>()
17+
orderService.addFoodToOrder(userId, foodId, 1).fold(
1918
onSuccess = { orderId -> call.respond(HttpStatusCode.OK, orderId) },
2019
onFailure = { call.respond(HttpStatusCode.InternalServerError, it.message ?: "Unknown error") }
2120
)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ package com.example.service
22

33
import com.example.dto.FoodDto
44
import com.example.dto.GetOrderWithItemsDto
5+
import com.example.dto.OrderDto
56
import com.example.service.impl.OrderServiceImpl
67
import kotlinx.coroutines.flow.Flow
78
import java.util.*
89

910
interface OrderService {
1011
suspend fun getActiveOrder(userId: UUID): Result<GetOrderWithItemsDto>
1112
suspend fun getActiveOrderFlow(userId: UUID): Result<Flow<GetOrderWithItemsDto>>
12-
suspend fun addFoodToOrder(userId: UUID, foodDto: FoodDto, quantity: Int): Result<Int>
13+
suspend fun addFoodToOrder(userId: UUID, foodId: Int, quantity: Int): Result<Int>
1314
suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<Boolean>
1415
suspend fun deleteCurrentOrder(userId: UUID): Result<Boolean>
1516
suspend fun deleteOrder(userId: UUID, orderId: Int): Result<Boolean>
1617
suspend fun completeCurrentOrder(userId: UUID): Result<Boolean>
1718
suspend fun cancelOrder(userId: UUID, orderId: Int): Result<Boolean>
1819
suspend fun getAllOrders(userId: UUID): Result<List<GetOrderWithItemsDto>>
1920
suspend fun getAllOrderFlow(userId: UUID): Result<Flow<GetOrderWithItemsDto>>
21+
2022
companion object {
2123
val orderService: OrderService by lazy { OrderServiceImpl() }
2224
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class OrderServiceImpl : OrderService {
3535
}.asFlow()
3636
}
3737

38-
override suspend fun addFoodToOrder(userId: UUID, foodDto: FoodDto, quantity: Int): Result<Int> = dbQuery {
38+
override suspend fun addFoodToOrder(userId: UUID, foodId: Int, quantity: Int): Result<Int> = dbQuery {
39+
val food = Foods.select { Foods.id eq foodId }.singleOrNull() ?: throw Exception("Food not found")
40+
3941
Orders.select {
4042
(Orders.userId eq userId) and (Orders.orderStatus eq OrderStatus.Started)
4143
}.forUpdate().singleOrNull() ?: createOrder(userId)
@@ -45,7 +47,7 @@ class OrderServiceImpl : OrderService {
4547
}.forUpdate().single()
4648

4749
val item = Items.select {
48-
(Items.orderId eq activeOrder[Orders.id]) and (Items.foodId eq foodDto.id)
50+
(Items.orderId eq activeOrder[Orders.id]) and (Items.foodId eq foodId)
4951
}.singleOrNull()
5052

5153
val itemId = if (item != null) {
@@ -57,13 +59,13 @@ class OrderServiceImpl : OrderService {
5759
} else {
5860
Items.insert {
5961
it[Items.orderId] = activeOrder[Orders.id]
60-
it[Items.foodId] = foodDto.id
62+
it[Items.foodId] = foodId
6163
it[Items.quantity] = quantity
62-
it[Items.price] = foodDto.price
64+
it[Items.price] = food[Foods.price]
6365
}[Items.id]
6466
}
6567

66-
val totalPrice = activeOrder[Orders.totalPrice] + foodDto.price * quantity.toBigDecimal()
68+
val totalPrice = activeOrder[Orders.totalPrice] + food[Foods.price] * quantity.toBigDecimal()
6769
Orders.update({ Orders.id eq activeOrder[Orders.id] }) {
6870
it[Orders.totalPrice] = totalPrice
6971
}

0 commit comments

Comments
 (0)