11package com.example.service.impl
22
3- import com.example.dto.FoodDto
43import com.example.dto.GetOrderWithItemsDto
54import com.example.dto.OrderStatus
65import com.example.service.DatabaseModule.dbQuery
76import com.example.service.OrderService
87import com.example.table.Foods
98import com.example.table.Items
109import com.example.table.Orders
10+ import com.example.util.ServiceException.*
1111import com.example.util.ext.toItemDto
1212import com.example.util.ext.toOrderDto
1313import kotlinx.coroutines.flow.Flow
@@ -20,7 +20,7 @@ class OrderServiceImpl : OrderService {
2020 override suspend fun getActiveOrder (userId : UUID ): Result <GetOrderWithItemsDto > = dbQuery {
2121 val order = Orders .select {
2222 (Orders .userId eq userId) and (Orders .orderStatus eq OrderStatus .Started )
23- }.singleOrNull() ? : throw Exception ( " No active order found " )
23+ }.singleOrNull() ? : throw ActiveOrderNotFoundException (userId )
2424
2525 val items = Items .select { Items .orderId eq order[Orders .id] }.map { it.toItemDto() }
2626 GetOrderWithItemsDto (order.toOrderDto(), items)
@@ -36,7 +36,7 @@ class OrderServiceImpl : OrderService {
3636 }
3737
3838 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 " )
39+ val food = Foods .select { Foods .id eq foodId }.singleOrNull() ? : throw FoodNotFoundException (foodId )
4040
4141 Orders .select {
4242 (Orders .userId eq userId) and (Orders .orderStatus eq OrderStatus .Started )
@@ -74,10 +74,13 @@ class OrderServiceImpl : OrderService {
7474 }
7575
7676 override suspend fun updateItemInOrder (itemId : Int , quantity : Int ): Result <Boolean > = dbQuery {
77- val item = Items .select { Items .id eq itemId }.singleOrNull() ? : throw Exception (" Item not found" )
78- val food = Foods .select { Foods .id eq item[Items .foodId] }.singleOrNull() ? : throw Exception (" Food not found" )
79- val order =
80- Orders .select { Orders .id eq item[Items .orderId] }.singleOrNull() ? : throw Exception (" Order not found" )
77+ val item = Items .select { Items .id eq itemId }.singleOrNull()
78+ ? : throw ItemNotFoundException (itemId)
79+ val food = Foods .select { Foods .id eq item[Items .foodId] }.singleOrNull()
80+ ? : throw FoodNotFoundException (item[Items .foodId])
81+ val order = Orders .select { Orders .id eq item[Items .orderId] }.singleOrNull()
82+ ? : throw NotFoundException (item[Items .orderId])
83+
8184 val newQuantity = item[Items .quantity] + quantity
8285
8386 when {
@@ -107,7 +110,7 @@ class OrderServiceImpl : OrderService {
107110 override suspend fun deleteCurrentOrder (userId : UUID ): Result <Boolean > = dbQuery {
108111 val order = Orders .select {
109112 (Orders .userId eq userId) and (Orders .orderStatus eq OrderStatus .Started )
110- }.singleOrNull() ? : throw Exception ( " No active order found " )
113+ }.singleOrNull() ? : throw ActiveOrderNotFoundException (userId )
111114
112115 Items .deleteWhere { Items .orderId eq order[Orders .id] }
113116 Orders .update({ Orders .id eq order[Orders .id] }) {
@@ -120,7 +123,7 @@ class OrderServiceImpl : OrderService {
120123 override suspend fun deleteOrder (userId : UUID , orderId : Int ): Result <Boolean > = dbQuery {
121124 val order = Orders .select {
122125 (Orders .userId eq userId) and (Orders .id eq orderId)
123- }.singleOrNull() ? : throw Exception ( " Order not found " )
126+ }.singleOrNull() ? : throw NotFoundException (orderId )
124127
125128 Items .deleteWhere { Items .orderId eq order[Orders .id] }
126129 Orders .deleteWhere { Orders .id eq order[Orders .id] }
@@ -131,7 +134,7 @@ class OrderServiceImpl : OrderService {
131134 override suspend fun completeCurrentOrder (userId : UUID ): Result <Boolean > = dbQuery {
132135 val order = Orders .select {
133136 (Orders .userId eq userId) and (Orders .orderStatus eq OrderStatus .Started )
134- }.singleOrNull() ? : throw Exception ( " No active order found " )
137+ }.singleOrNull() ? : throw ActiveOrderNotFoundException (userId )
135138
136139 Orders .update({ Orders .id eq order[Orders .id] }) {
137140 it[orderStatus] = OrderStatus .Finished
@@ -143,7 +146,7 @@ class OrderServiceImpl : OrderService {
143146 override suspend fun cancelOrder (userId : UUID , orderId : Int ): Result <Boolean > = dbQuery {
144147 val order = Orders .select {
145148 (Orders .userId eq userId) and (Orders .id eq orderId)
146- }.singleOrNull() ? : throw Exception ( " Order not found " )
149+ }.singleOrNull() ? : throw NotFoundException (orderId )
147150
148151 Orders .update({ Orders .id eq order[Orders .id] }) {
149152 it[orderStatus] = OrderStatus .Cancelled
0 commit comments