Skip to content

Commit dbcc68d

Browse files
committed
refactor: error handling added
1 parent 13e3985 commit dbcc68d

File tree

7 files changed

+39
-21
lines changed

7 files changed

+39
-21
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ val logback_version: String by project
44
val exposed_version: String by project
55
val postgresql_version: String by project
66
val kotlinx_datetime_version: String by project
7+
val arrow_version: String by project
78

89
plugins {
910
kotlin("jvm") version "1.9.23"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import kotlinx.serialization.Serializable
66
import java.math.BigDecimal
77

88
enum class OrderStatus {
9-
Pending, Started, Processing, Finished, Cancelled
9+
Pending, Started, Finished, Cancelled
1010
}
1111

1212
@Serializable
@@ -16,7 +16,7 @@ data class OrderDto(
1616
val orderTime: Instant,
1717
val orderStatus: OrderStatus = OrderStatus.Pending,
1818
@Serializable(with = BigDecimalSerializer::class) val totalPrice: BigDecimal,
19-
val paymentDetails: String? = null,
19+
val paymentDetails: String? = null
2020
)
2121

2222
@Serializable

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

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

3-
import com.example.dto.FoodDto
43
import com.example.dto.GetOrderWithItemsDto
5-
import com.example.dto.OrderDto
64
import com.example.service.impl.OrderServiceImpl
75
import kotlinx.coroutines.flow.Flow
86
import java.util.*

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import com.example.dto.FoodDto
44
import com.example.service.DatabaseModule.dbQuery
55
import com.example.service.FoodService
66
import com.example.table.Foods
7+
import com.example.util.ServiceException.FoodNotFoundException
78
import com.example.util.ext.toFoodDto
89
import com.example.util.ext.toFoodType
910
import org.jetbrains.exposed.sql.*
1011

1112
class FoodServiceImpl : FoodService {
1213
override suspend fun getFoodDetail(foodId: Int): Result<FoodDto> = dbQuery {
1314
val food = Foods.select { Foods.id eq foodId }.singleOrNull()
14-
food?.toFoodDto() ?: throw Exception("Food not found")
15+
food?.toFoodDto() ?: throw FoodNotFoundException(foodId)
1516
}
1617

1718
override suspend fun searchFood(query: String): Result<List<FoodDto>> = dbQuery {

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

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

3-
import com.example.dto.FoodDto
43
import com.example.dto.GetOrderWithItemsDto
54
import com.example.dto.OrderStatus
65
import com.example.service.DatabaseModule.dbQuery
76
import com.example.service.OrderService
87
import com.example.table.Foods
98
import com.example.table.Items
109
import com.example.table.Orders
10+
import com.example.util.ServiceException.*
1111
import com.example.util.ext.toItemDto
1212
import com.example.util.ext.toOrderDto
1313
import 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

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.example.dto.UserUpdateLocationDto
77
import com.example.service.DatabaseModule.dbQuery
88
import com.example.service.UserService
99
import com.example.table.Users
10+
import com.example.util.ServiceException.*
1011
import com.example.util.ext.toUserDto
1112
import kotlinx.coroutines.flow.Flow
1213
import kotlinx.coroutines.flow.asFlow
@@ -22,15 +23,15 @@ class UserServiceImpl : UserService {
2223
if (user != null) {
2324
user[Users.userId].toString()
2425
} else {
25-
throw Exception("Invalid email or password")
26+
throw InvalidUsernameOrPasswordException()
2627
}
2728
}
2829

2930
override suspend fun register(userRegisterDto: UserRegisterDto): Result<Boolean> = dbQuery {
3031
val registerStatement = Users.select { Users.email eq userRegisterDto.email }
3132
val user = registerStatement.singleOrNull()
3233
if (user != null) {
33-
throw Exception("Email already exists")
34+
throw EmailAlreadyExistsException()
3435
} else {
3536
Users.insert {
3637
it[fullName] = userRegisterDto.fullName
@@ -44,7 +45,7 @@ class UserServiceImpl : UserService {
4445
override suspend fun getProfileFlow(userId: UUID): Result<Flow<UserDto>> = dbQuery {
4546
val user = Users.select { Users.userId eq userId }
4647
if (user.empty()) {
47-
throw Exception("User not found")
48+
throw UserNotFoundException(userId)
4849
} else {
4950
val userFlow = user.map { it.toUserDto() }.asFlow()
5051
userFlow
@@ -54,7 +55,7 @@ class UserServiceImpl : UserService {
5455
override suspend fun getProfile(userId: UUID): Result<UserDto> = dbQuery {
5556
val user = Users.select { Users.userId eq userId }
5657
if (user.empty()) {
57-
throw Exception("User not found")
58+
throw UserNotFoundException(userId)
5859
} else {
5960
user.single().toUserDto()
6061
}
@@ -66,7 +67,7 @@ class UserServiceImpl : UserService {
6667
): Result<Boolean> = dbQuery {
6768
val user = Users.select { Users.userId eq userId }
6869
if (user.empty()) {
69-
throw Exception("User not found")
70+
throw UserNotFoundException(userId)
7071
} else {
7172
Users.update({ Users.userId eq userId }) {
7273
it[latitude] = userUpdateLocationDto.latitude
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.util
2+
3+
import java.util.*
4+
5+
sealed class ServiceException(message: String) : Exception(message) {
6+
class ActiveOrderNotFoundException(userId: UUID) :
7+
ServiceException("No active order found for user ID: $userId")
8+
class FoodNotFoundException(foodId: Int) : ServiceException("Food not found with ID: $foodId")
9+
class NotFoundException(orderId: Int) : ServiceException("Order not found with ID: $orderId")
10+
class ItemNotFoundException(itemId: Int) : ServiceException("Item not found with ID: $itemId")
11+
class InvalidUsernameOrPasswordException : ServiceException("Invalid username or password")
12+
class EmailAlreadyExistsException : ServiceException("Email already exists")
13+
class UserNotFoundException(userId: UUID) : ServiceException("User not found with ID: $userId")
14+
}

0 commit comments

Comments
 (0)