Skip to content

Commit 52fb233

Browse files
committed
refactor: general
1 parent 6be6953 commit 52fb233

File tree

10 files changed

+69
-74
lines changed

10 files changed

+69
-74
lines changed

.README.md.un~

-973 Bytes
Binary file not shown.

Dockerfile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
# Use a base image with the desired JDK version
22
FROM adoptopenjdk:11-jdk-hotspot
33

4-
# Set the working directory inside the container
54
WORKDIR /app
65

7-
# Copy the gradlew file and the gradle directory to the container
86
COPY gradlew .
97
COPY gradle ./gradle
108

11-
# Give execute permission to gradlew
129
RUN chmod +x ./gradlew
1310

14-
# Copy the build.gradle.kts and settings.gradle.kts files to the container
1511
COPY build.gradle.kts settings.gradle.kts ./
1612

17-
# Copy the source code to the container
1813
COPY src ./src
1914

2015
RUN ./gradlew build
2116

2217
EXPOSE 8080
2318

24-
# Start the application when the container starts
25-
CMD ["java", "-jar", "build/libs/food-api-all.jar"]
19+
CMD ["java", "-jar", "build/libs/food-delivery-backend-all.jar"]

README.md~

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.example.service.FoodService.Companion.foodService
55
import com.example.util.Constants.GENERIC_ERROR
66
import com.example.util.Constants.INVALID_FOOD_ID
77
import com.example.util.Constants.INVALID_SEARCH_QUERY
8+
import com.example.util.ext.getValueFromParameters
89
import io.ktor.http.*
910
import io.ktor.server.application.*
1011
import io.ktor.server.response.*
@@ -13,13 +14,7 @@ import io.ktor.server.routing.*
1314
fun Application.foodRoute() {
1415
routing {
1516
get("/foods/{foodId}") {
16-
val foodId = call.parameters["foodId"]?.toIntOrNull() ?: run {
17-
call.respondText(
18-
text = INVALID_FOOD_ID,
19-
status = HttpStatusCode.BadRequest
20-
)
21-
return@get
22-
}
17+
val foodId = call.getValueFromParameters("foodId", String::toInt)
2318
foodService.getFoodDetail(foodId).fold(
2419
onSuccess = { foodDto -> call.respond(foodDto) },
2520
onFailure = { throwable ->
@@ -91,11 +86,5 @@ fun Application.foodRoute() {
9186
val stock = foodDtos.groupBy { it.category }.mapValues { it.value.size }
9287
call.respond(stock)
9388
}
94-
95-
get("fake/foods/saveFood") {
96-
foodDtos.forEach {
97-
foodService.registerFood(it)
98-
}
99-
}
10089
}
10190
}

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

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ package com.example.route
22

33
import com.example.dto.FoodDto
44
import com.example.service.OrderService.Companion.orderService
5-
import com.example.util.Constants.INVALID_ORDER_ITEM_ID
6-
import com.example.util.Constants.INVALID_USER_ID
5+
import com.example.util.ext.getValueFromParameters
76
import io.ktor.http.*
87
import io.ktor.server.application.*
98
import io.ktor.server.request.*
109
import io.ktor.server.response.*
1110
import io.ktor.server.routing.*
12-
import java.util.UUID
11+
import java.util.*
1312

1413
fun Application.orderRoute() {
1514
routing {
1615
post("/orders/{userId}/items") {
17-
val userId = call.parameters["userId"].let { UUID.fromString(it) } ?: run {
18-
call.respond(HttpStatusCode.BadRequest, INVALID_USER_ID)
19-
return@post
20-
}
16+
val userId = call.getValueFromParameters("userId", UUID::fromString)
2117
val foodDto = call.receive<FoodDto>()
2218
orderService.addFoodToOrder(userId, foodDto, 1).fold(
2319
onSuccess = { orderId -> call.respond(HttpStatusCode.OK, orderId) },
@@ -26,11 +22,7 @@ fun Application.orderRoute() {
2622
}
2723

2824
post("/orders/items/{itemId}") {
29-
val itemId = call.parameters["itemId"]?.toIntOrNull() ?: run {
30-
call.respond(HttpStatusCode.BadRequest, INVALID_ORDER_ITEM_ID)
31-
return@post
32-
}
33-
25+
val itemId = call.getValueFromParameters("itemId", String::toInt)
3426
val quantity = call.receive<Int>()
3527
orderService.updateItemInOrder(itemId, quantity).fold(
3628
onSuccess = { item -> call.respond(HttpStatusCode.OK, item) },
@@ -39,36 +31,32 @@ fun Application.orderRoute() {
3931
}
4032

4133
get("/orders/{userId}") {
42-
val userId = call.parameters["userId"].let { UUID.fromString(it) } ?: run {
43-
call.respond(HttpStatusCode.BadRequest, INVALID_USER_ID)
44-
return@get
45-
}
34+
val userId = call.getValueFromParameters("userId", UUID::fromString)
4635
orderService.getActiveOrder(userId).fold(
4736
onSuccess = { getActiveOrderDto -> call.respond(getActiveOrderDto) },
4837
onFailure = { call.respond(HttpStatusCode.NotFound, it.message ?: "No active order found") }
4938
)
5039
}
5140

41+
get("/orders/{userId}/complete") {
42+
val userId = call.getValueFromParameters("userId", UUID::fromString)
43+
orderService.completeCurrentOrder(userId).fold(
44+
onSuccess = { call.respond(HttpStatusCode.OK, true) },
45+
onFailure = { call.respond(HttpStatusCode.InternalServerError, it.message ?: "Unknown error") }
46+
)
47+
}
48+
5249
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-
}
50+
val userId = call.getValueFromParameters("userId", UUID::fromString)
5751
orderService.deleteCurrentOrder(userId).fold(
5852
onSuccess = { call.respond(HttpStatusCode.OK, true) },
5953
onFailure = { call.respond(HttpStatusCode.InternalServerError, it.message ?: "Unknown error") }
6054
)
6155
}
6256

6357
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-
}
58+
val userId = call.getValueFromParameters("userId", UUID::fromString)
59+
val orderId = call.getValueFromParameters("orderId", String::toInt)
7260
orderService.deleteOrder(userId, orderId).fold(
7361
onSuccess = { call.respond(HttpStatusCode.OK, true) },
7462
onFailure = { call.respond(HttpStatusCode.InternalServerError, it.message ?: "Unknown error") }

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.example.dto.UserUpdateLocationDto
66
import com.example.service.UserService.Companion.userService
77
import com.example.util.Constants.GENERIC_ERROR
88
import com.example.util.Constants.INVALID_USER_ID
9+
import com.example.util.ext.getValueFromParameters
910
import io.ktor.http.*
1011
import io.ktor.server.application.*
1112
import io.ktor.server.request.*
@@ -42,13 +43,7 @@ fun Application.userRoute() {
4243
}
4344

4445
get("/profile/{userId}") {
45-
val userId = call.parameters["userId"]?.let { UUID.fromString(it) } ?: run {
46-
call.respond(
47-
status = HttpStatusCode.BadRequest,
48-
message = INVALID_USER_ID
49-
)
50-
return@get
51-
}
46+
val userId = call.getValueFromParameters("userId", UUID::fromString)
5247
userService.getProfile(userId).fold(
5348
onSuccess = { userDto -> call.respond(userDto) },
5449
onFailure = { throwable ->
@@ -61,13 +56,7 @@ fun Application.userRoute() {
6156
}
6257

6358
get("/profile/{userId}/flow") {
64-
val userId = call.parameters["userId"]?.let { UUID.fromString(it) } ?: run {
65-
call.respond(
66-
status = HttpStatusCode.BadRequest,
67-
message = INVALID_USER_ID
68-
)
69-
return@get
70-
}
59+
val userId = call.getValueFromParameters("userId", UUID::fromString)
7160
userService.getProfileFlow(userId).fold(
7261
onSuccess = { userDtoFlow -> call.respond(userDtoFlow) },
7362
onFailure = { throwable ->
@@ -80,13 +69,7 @@ fun Application.userRoute() {
8069
}
8170

8271
post("/profile/{userId}/location") {
83-
val userId = call.parameters["userId"]?.let { UUID.fromString(it) } ?: run {
84-
call.respond(
85-
status = HttpStatusCode.BadRequest,
86-
message = INVALID_USER_ID
87-
)
88-
return@post
89-
}
72+
val userId = call.getValueFromParameters("userId", UUID::fromString)
9073
val userUpdateLocationDto = call.receive<UserUpdateLocationDto>()
9174
userService.updateUserLocation(userId, userUpdateLocationDto).fold(
9275
onSuccess = { call.respond(HttpStatusCode.OK) },

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ import org.jetbrains.exposed.sql.Database
99
import org.jetbrains.exposed.sql.SchemaUtils
1010
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
1111
import org.jetbrains.exposed.sql.transactions.transaction
12+
import java.net.URI
1213

1314

1415
object DatabaseModule {
1516
fun init() {
16-
val driverClassName = "org.postgresql.Driver"
17-
val jdbcURL = System.getenv("JDBC_URL") ?: "DEFAULT_JDBC_URL"
18-
val database = Database.connect(jdbcURL, driverClassName)
17+
val databaseUrl = System.getenv("DATABASE_URL") ?: "DEFAULT_DATABASE_URL"
18+
val dbUri = URI(databaseUrl)
19+
20+
val username = dbUri.userInfo.split(":")[0]
21+
val password = dbUri.userInfo.split(":")[1]
22+
val dbUrl = "jdbc:postgresql://" + dbUri.host + ':' + dbUri.port + dbUri.path + "?sslmode=require"
23+
24+
val database = Database.connect(dbUrl, driver = "org.postgresql.Driver", user = username, password = password)
1925
transaction(database) {
2026
SchemaUtils.create(Users)
2127
SchemaUtils.create(Foods)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ interface OrderService {
1212
suspend fun updateItemInOrder(itemId: Int, quantity: Int): Result<Boolean>
1313
suspend fun deleteCurrentOrder(userId: UUID): Result<Boolean>
1414
suspend fun deleteOrder(userId: UUID, orderId: Int): Result<Boolean>
15-
15+
suspend fun completeCurrentOrder(userId: UUID): Result<Boolean>
16+
suspend fun cancelOrder(userId: UUID, orderId: Int): Result<Boolean>
1617
companion object {
1718
val orderService: OrderService by lazy { OrderServiceImpl() }
1819
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ class OrderServiceImpl : OrderService {
113113
true
114114
}
115115

116+
override suspend fun completeCurrentOrder(userId: UUID): Result<Boolean> = dbQuery {
117+
val order = Orders.select {
118+
(Orders.userId eq userId) and (Orders.orderStatus eq OrderStatus.Started)
119+
}.singleOrNull() ?: throw Exception("No active order found")
120+
121+
Orders.update({ Orders.id eq order[Orders.id] }) {
122+
it[orderStatus] = OrderStatus.Finished
123+
}
124+
125+
true
126+
}
127+
128+
override suspend fun cancelOrder(userId: UUID, orderId: Int): Result<Boolean> = dbQuery {
129+
val order = Orders.select {
130+
(Orders.userId eq userId) and (Orders.id eq orderId)
131+
}.singleOrNull() ?: throw Exception("Order not found")
132+
133+
Orders.update({ Orders.id eq order[Orders.id] }) {
134+
it[orderStatus] = OrderStatus.Cancelled
135+
}
136+
137+
true
138+
}
139+
116140
private fun createOrder(userId: UUID) {
117141
Orders.insert {
118142
it[Orders.userId] = userId
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example.util.ext
2+
3+
import io.ktor.http.*
4+
import io.ktor.server.application.*
5+
import io.ktor.server.response.*
6+
7+
suspend fun <T> ApplicationCall.getValueFromParameters(parameterName: String, transform: (String) -> T): T =
8+
parameters[parameterName]?.let(transform) ?: run {
9+
respond(HttpStatusCode.BadRequest, "Invalid $parameterName")
10+
throw IllegalArgumentException("Invalid $parameterName")
11+
}

0 commit comments

Comments
 (0)