Skip to content

Commit

Permalink
Merge pull request #31 from piashcse/enhance-param-validation
Browse files Browse the repository at this point in the history
- Optimize param validation with extension function
  • Loading branch information
piashcse authored Nov 17, 2024
2 parents e95a7d2 + 592a01f commit cadbb31
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 228 deletions.
19 changes: 4 additions & 15 deletions src/main/kotlin/com/piashcse/route/BrandRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.piashcse.models.bands.AddBrand
import com.piashcse.plugins.RoleManagement
import com.piashcse.utils.ApiResponse
import com.piashcse.utils.extension.apiResponse
import com.piashcse.utils.extension.requiredParameters
import io.github.smiley4.ktorswaggerui.dsl.routing.delete
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.post
Expand All @@ -30,11 +31,7 @@ fun Route.brandRoute(brandController: BrandController) {
}
apiResponse()
}) {
val requiredParams = listOf("limit", "offset")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (limit, offset) = requiredParams.map { call.parameters[it]!! }
val (limit, offset) = call.requiredParameters("limit", "offset") ?: return@get
call.respond(
ApiResponse.success(
brandController.getBrands(limit.toInt(), offset.toLong()), HttpStatusCode.OK
Expand Down Expand Up @@ -69,11 +66,7 @@ fun Route.brandRoute(brandController: BrandController) {
}
apiResponse()
}) {
val requiredParams = listOf("id", "name")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id, name) = requiredParams.map { call.parameters[it]!! }
val (id, name) = call.requiredParameters("id", "name") ?: return@put
call.respond(
ApiResponse.success(
brandController.updateBrand(id, name), HttpStatusCode.OK
Expand All @@ -89,11 +82,7 @@ fun Route.brandRoute(brandController: BrandController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@delete
call.respond(
ApiResponse.success(
brandController.deleteBrand(id), HttpStatusCode.OK
Expand Down
19 changes: 4 additions & 15 deletions src/main/kotlin/com/piashcse/route/CartRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.piashcse.plugins.RoleManagement
import com.piashcse.utils.ApiResponse
import com.piashcse.utils.extension.apiResponse
import com.piashcse.utils.extension.currentUser
import com.piashcse.utils.extension.requiredParameters
import io.github.smiley4.ktorswaggerui.dsl.routing.delete
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.post
Expand Down Expand Up @@ -49,11 +50,7 @@ fun Route.cartRoute(cartController: CartController) {
}
apiResponse()
}) {
val requiredParams = listOf("limit", "offset")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (limit, offset) = requiredParams.map { call.parameters[it]!! }
val (limit, offset) = call.requiredParameters("limit", "offset") ?: return@get
call.respond(
ApiResponse.success(
cartController.getCartItems(
Expand All @@ -76,11 +73,7 @@ fun Route.cartRoute(cartController: CartController) {
}
apiResponse()
}) {
val requiredParams = listOf("productId", "quantity")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (productId, quantity) = requiredParams.map { call.parameters[it]!! }
val (productId, quantity) = call.requiredParameters("productId", "quantity") ?: return@put
call.respond(
ApiResponse.success(
cartController.updateCartQuantity(call.currentUser().userId, productId, quantity.toInt()),
Expand All @@ -97,11 +90,7 @@ fun Route.cartRoute(cartController: CartController) {
}
apiResponse()
}) {
val requiredParams = listOf("productId")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (productId) = requiredParams.map { call.parameters[it]!! }
val (productId) = call.requiredParameters("productId") ?: return@delete
call.respond(
ApiResponse.success(
cartController.deleteCartItem(call.currentUser().userId, productId),
Expand Down
31 changes: 6 additions & 25 deletions src/main/kotlin/com/piashcse/route/OrderRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.piashcse.utils.ApiResponse
import com.piashcse.utils.extension.OrderStatus
import com.piashcse.utils.extension.apiResponse
import com.piashcse.utils.extension.currentUser
import com.piashcse.utils.extension.requiredParameters
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.post
import io.github.smiley4.ktorswaggerui.dsl.routing.put
Expand Down Expand Up @@ -46,11 +47,7 @@ fun Route.orderRoute(orderController: OrderController) {
}
apiResponse()
}) {
val requiredParams = listOf("limit", "offset")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (limit, offset) = requiredParams.map { call.parameters[it]!! }
val (limit, offset) = call.requiredParameters("limit", "offset") ?: return@get
call.respond(
ApiResponse.success(
orderController.getOrders(
Expand All @@ -68,11 +65,7 @@ fun Route.orderRoute(orderController: OrderController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@put
call.respond(
ApiResponse.success(
orderController.updateOrder(call.currentUser().userId, id, OrderStatus.CANCELED),
Expand All @@ -89,11 +82,7 @@ fun Route.orderRoute(orderController: OrderController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@put
call.respond(
ApiResponse.success(
orderController.updateOrder(call.currentUser().userId, id, OrderStatus.RECEIVED),
Expand All @@ -112,11 +101,7 @@ fun Route.orderRoute(orderController: OrderController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@put
call.respond(
ApiResponse.success(
orderController.updateOrder(call.currentUser().userId, id, OrderStatus.CANCELED),
Expand All @@ -133,11 +118,7 @@ fun Route.orderRoute(orderController: OrderController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@put
call.respond(
ApiResponse.success(
orderController.updateOrder(call.currentUser().userId, id, OrderStatus.DELIVERED),
Expand Down
7 changes: 2 additions & 5 deletions src/main/kotlin/com/piashcse/route/PaymentRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.piashcse.models.AddPayment
import com.piashcse.plugins.RoleManagement
import com.piashcse.utils.ApiResponse
import com.piashcse.utils.extension.apiResponse
import com.piashcse.utils.extension.requiredParameters
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.post
import io.ktor.http.*
Expand Down Expand Up @@ -39,11 +40,7 @@ fun Route.paymentRoute(paymentController: PaymentController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@get
call.respond(
ApiResponse.success(
paymentController.getPayment(
Expand Down
24 changes: 4 additions & 20 deletions src/main/kotlin/com/piashcse/route/ProductCategoryRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.piashcse.models.category.AddProductCategory
import com.piashcse.plugins.RoleManagement
import com.piashcse.utils.ApiResponse
import com.piashcse.utils.extension.apiResponse
import com.piashcse.utils.extension.requiredParameters
import io.github.smiley4.ktorswaggerui.dsl.routing.delete
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.post
Expand All @@ -29,15 +30,7 @@ fun Route.productCategoryRoute(productCategoryController: ProductCategoryControl
}
apiResponse()
}) {
val requiredParams = listOf("limit", "offset")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(
ApiResponse.success(
"Missing parameters: $it", HttpStatusCode.OK
)
)
}
val (limit, offset) = requiredParams.map { call.parameters[it]!! }
val (limit, offset) = call.requiredParameters("limit", "offset") ?: return@get
call.respond(
ApiResponse.success(
productCategoryController.getProductCategory(
Expand Down Expand Up @@ -77,11 +70,7 @@ fun Route.productCategoryRoute(productCategoryController: ProductCategoryControl
}
apiResponse()
}) {
val requiredParams = listOf("id", "categoryName")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id, categoryName) = requiredParams.map { call.parameters[it]!! }
val (id, categoryName) = call.requiredParameters("id", "categoryName") ?: return@put
call.respond(
ApiResponse.success(
productCategoryController.updateProductCategory(
Expand All @@ -99,12 +88,7 @@ fun Route.productCategoryRoute(productCategoryController: ProductCategoryControl
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }

val (id) = call.requiredParameters("id") ?: return@delete
call.respond(
ApiResponse.success(
productCategoryController.deleteProductCategory(
Expand Down
47 changes: 15 additions & 32 deletions src/main/kotlin/com/piashcse/route/ProductRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.piashcse.utils.AppConstants
import com.piashcse.utils.extension.apiResponse
import com.piashcse.utils.extension.fileExtension
import com.piashcse.utils.extension.currentUser
import com.piashcse.utils.extension.requiredParameters
import io.github.smiley4.ktorswaggerui.dsl.routing.delete
import io.github.smiley4.ktorswaggerui.dsl.routing.get
import io.github.smiley4.ktorswaggerui.dsl.routing.post
Expand Down Expand Up @@ -38,7 +39,7 @@ fun Route.productRoute(productController: ProductController) {
}
apiResponse()
}) {
val productId = call.parameters["id"]!!
val (productId) = call.requiredParameters("productId") ?: return@get
call.respond(ApiResponse.success(productController.productDetail(productId), HttpStatusCode.OK))
}
get({
Expand All @@ -58,9 +59,10 @@ fun Route.productRoute(productController: ProductController) {
}
apiResponse()
}) {
val (limit, offset) = call.requiredParameters("limit", "offset") ?: return@get
val params = ProductWithFilter(
limit = call.parameters["limit"]?.toInt() ?: 0,
offset = call.parameters["offset"]?.toLong() ?: 0L,
limit = limit.toInt(),
offset = offset.toLong(),
maxPrice = call.parameters["maxPrice"]?.toDoubleOrNull(),
minPrice = call.parameters["minPrice"]?.toDoubleOrNull(),
categoryId = call.parameters["categoryId"],
Expand All @@ -79,7 +81,7 @@ fun Route.productRoute(productController: ProductController) {
queryParameter<Long>("offset") {
required = true
}
queryParameter<String>("productName"){
queryParameter<String>("productName") {
required = true
}
queryParameter<String>("categoryId")
Expand All @@ -88,9 +90,10 @@ fun Route.productRoute(productController: ProductController) {
}
apiResponse()
}) {
val (limit, offset) = call.requiredParameters("limit", "offset") ?: return@get
val queryParams = ProductSearch(
limit = call.parameters["limit"]?.toInt() ?: 0,
offset = call.parameters["offset"]?.toLong() ?: 0L,
limit = limit.toInt(),
offset = offset.toLong(),
productName = call.parameters["productName"]!!,
maxPrice = call.parameters["maxPrice"]?.toDoubleOrNull(),
minPrice = call.parameters["minPrice"]?.toDoubleOrNull(),
Expand Down Expand Up @@ -118,9 +121,10 @@ fun Route.productRoute(productController: ProductController) {
}
apiResponse()
}) {
val (limit, offset) = call.requiredParameters("limit", "offset") ?: return@get
val params = ProductWithFilter(
limit = call.parameters["limit"]?.toInt() ?: 0,
offset = call.parameters["offset"]?.toLong() ?: 0L,
limit = limit.toInt(),
offset = offset.toLong(),
maxPrice = call.parameters["maxPrice"]?.toDoubleOrNull(),
minPrice = call.parameters["minPrice"]?.toDoubleOrNull(),
categoryId = call.parameters["categoryId"],
Expand Down Expand Up @@ -198,11 +202,7 @@ fun Route.productRoute(productController: ProductController) {
imageOne = call.parameters["imageOne"],
imageTwo = call.parameters["imageTwo"],
)
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@put
call.respond(
ApiResponse.success(
productController.updateProduct(call.currentUser().userId, id, params), HttpStatusCode.OK
Expand All @@ -216,11 +216,7 @@ fun Route.productRoute(productController: ProductController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK))
}
val (id) = requiredParams.map { call.parameters[it]!! }
val (id) = call.requiredParameters("id") ?: return@delete
call.respond(
ApiResponse.success(
productController.deleteProduct(call.currentUser().userId, id), HttpStatusCode.OK
Expand All @@ -244,23 +240,10 @@ fun Route.productRoute(productController: ProductController) {
}
apiResponse()
}) {
val requiredParams = listOf("id")
requiredParams.filterNot { call.parameters.contains(it) }.let {
if (it.isNotEmpty()) call.respond(
ApiResponse.success(
"Missing parameters: $it", HttpStatusCode.OK
)
)
}
val (id) = requiredParams.map { call.parameters[it]!! }

val (id) = call.requiredParameters("id") ?: return@post
val multipartData = call.receiveMultipart()
multipartData.forEachPart { part ->
when (part) {
is PartData.FormItem -> {
val fileDescription = part.value
}

is PartData.FileItem -> {
UUID.randomUUID()?.let { imageId ->
val fileName = part.originalFileName as String
Expand Down
Loading

0 comments on commit cadbb31

Please sign in to comment.