diff --git a/src/main/kotlin/com/piashcse/route/BrandRoute.kt b/src/main/kotlin/com/piashcse/route/BrandRoute.kt index 20a900a..da6da20 100644 --- a/src/main/kotlin/com/piashcse/route/BrandRoute.kt +++ b/src/main/kotlin/com/piashcse/route/BrandRoute.kt @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/src/main/kotlin/com/piashcse/route/CartRoute.kt b/src/main/kotlin/com/piashcse/route/CartRoute.kt index 9b04d4f..b24d785 100644 --- a/src/main/kotlin/com/piashcse/route/CartRoute.kt +++ b/src/main/kotlin/com/piashcse/route/CartRoute.kt @@ -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 @@ -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( @@ -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()), @@ -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), diff --git a/src/main/kotlin/com/piashcse/route/OrderRoute.kt b/src/main/kotlin/com/piashcse/route/OrderRoute.kt index 824e7df..8994150 100644 --- a/src/main/kotlin/com/piashcse/route/OrderRoute.kt +++ b/src/main/kotlin/com/piashcse/route/OrderRoute.kt @@ -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 @@ -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( @@ -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), @@ -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), @@ -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), @@ -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), diff --git a/src/main/kotlin/com/piashcse/route/PaymentRoute.kt b/src/main/kotlin/com/piashcse/route/PaymentRoute.kt index 57b6043..b915648 100644 --- a/src/main/kotlin/com/piashcse/route/PaymentRoute.kt +++ b/src/main/kotlin/com/piashcse/route/PaymentRoute.kt @@ -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.* @@ -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( diff --git a/src/main/kotlin/com/piashcse/route/ProductCategoryRoute.kt b/src/main/kotlin/com/piashcse/route/ProductCategoryRoute.kt index 1091223..dc6befa 100644 --- a/src/main/kotlin/com/piashcse/route/ProductCategoryRoute.kt +++ b/src/main/kotlin/com/piashcse/route/ProductCategoryRoute.kt @@ -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 @@ -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( @@ -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( @@ -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( diff --git a/src/main/kotlin/com/piashcse/route/ProductRoute.kt b/src/main/kotlin/com/piashcse/route/ProductRoute.kt index d137a6f..5763285 100644 --- a/src/main/kotlin/com/piashcse/route/ProductRoute.kt +++ b/src/main/kotlin/com/piashcse/route/ProductRoute.kt @@ -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 @@ -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({ @@ -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"], @@ -79,7 +81,7 @@ fun Route.productRoute(productController: ProductController) { queryParameter("offset") { required = true } - queryParameter("productName"){ + queryParameter("productName") { required = true } queryParameter("categoryId") @@ -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(), @@ -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"], @@ -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 @@ -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 @@ -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 diff --git a/src/main/kotlin/com/piashcse/route/ProductSubCategoryRoute.kt b/src/main/kotlin/com/piashcse/route/ProductSubCategoryRoute.kt index 1cf2f55..e1082ad 100644 --- a/src/main/kotlin/com/piashcse/route/ProductSubCategoryRoute.kt +++ b/src/main/kotlin/com/piashcse/route/ProductSubCategoryRoute.kt @@ -5,6 +5,7 @@ import com.piashcse.models.subcategory.AddProductSubCategory 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 @@ -32,11 +33,7 @@ fun Route.productSubCategoryRoute(subCategoryController: ProductSubCategoryContr } apiResponse() }) { - val requiredParams = listOf("categoryId", "limit", "offset") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (categoryId, limit, offset) = requiredParams.map { call.parameters[it]!! } + val (categoryId, limit, offset) = call.requiredParameters("categoryId", "limit", "offset") ?: return@get call.respond( ApiResponse.success( subCategoryController.getProductSubCategory(categoryId, limit.toInt(), offset.toLong()), @@ -72,11 +69,7 @@ fun Route.productSubCategoryRoute(subCategoryController: ProductSubCategoryContr } 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( subCategoryController.updateProductSubCategory( @@ -95,11 +88,7 @@ fun Route.productSubCategoryRoute(subCategoryController: ProductSubCategoryContr } 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( subCategoryController.deleteProductSubCategory( diff --git a/src/main/kotlin/com/piashcse/route/ReviewRatingRoute.kt b/src/main/kotlin/com/piashcse/route/ReviewRatingRoute.kt index 5f94a80..09ff8b9 100644 --- a/src/main/kotlin/com/piashcse/route/ReviewRatingRoute.kt +++ b/src/main/kotlin/com/piashcse/route/ReviewRatingRoute.kt @@ -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 @@ -34,11 +35,7 @@ fun Route.reviewRatingRoute(reviewRatingController: ReviewRatingController) { } apiResponse() }) { - val requiredParams = listOf("productId", "limit", "offset") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (productId, limit, offset) = requiredParams.map { call.parameters[it]!! } + val (productId, limit, offset) = call.requiredParameters("productId", "limit", "offset") ?: return@get call.respond( ApiResponse.success( reviewRatingController.getReviewRating(productId, limit.toInt(), offset.toLong()), @@ -58,7 +55,8 @@ fun Route.reviewRatingRoute(reviewRatingController: ReviewRatingController) { val requestBody = call.receive() call.respond( ApiResponse.success( - reviewRatingController.addReviewRating(call.currentUser().userId, requestBody), HttpStatusCode.OK + reviewRatingController.addReviewRating(call.currentUser().userId, requestBody), + HttpStatusCode.OK ) ) } @@ -77,11 +75,7 @@ fun Route.reviewRatingRoute(reviewRatingController: ReviewRatingController) { } apiResponse() }) { - val requiredParams = listOf("reviewId", "review", "rating") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (reviewId, review, rating) = requiredParams.map { call.parameters[it]!! } + val (reviewId, review, rating) = call.requiredParameters("reviewId", "review", "rating") ?: return@put call.respond( ApiResponse.success( reviewRatingController.updateReviewRating( @@ -101,11 +95,7 @@ fun Route.reviewRatingRoute(reviewRatingController: ReviewRatingController) { } apiResponse() }) { - val requiredParams = listOf("reviewId") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (reviewId) = requiredParams.map { call.parameters[it]!! } + val (reviewId) = call.requiredParameters("reviewId") ?: return@delete call.respond( ApiResponse.success( reviewRatingController.deleteReviewRating(reviewId), HttpStatusCode.OK diff --git a/src/main/kotlin/com/piashcse/route/ShippingRoute.kt b/src/main/kotlin/com/piashcse/route/ShippingRoute.kt index 44356c7..0f9e901 100644 --- a/src/main/kotlin/com/piashcse/route/ShippingRoute.kt +++ b/src/main/kotlin/com/piashcse/route/ShippingRoute.kt @@ -7,6 +7,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 @@ -43,11 +44,7 @@ fun Route.shippingRoute(shippingController: ShippingController) { } apiResponse() }) { - val requiredParams = listOf("orderId") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (orderId) = requiredParams.map { call.parameters[it]!! } + val (orderId) = call.requiredParameters("orderId") ?: return@get call.respond( ApiResponse.success( shippingController.getShipping(call.currentUser().userId, orderId), HttpStatusCode.OK @@ -70,11 +67,7 @@ fun Route.shippingRoute(shippingController: ShippingController) { } 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 val params = UpdateShipping( id = id, shipAddress = call.parameters["shipAddress"], @@ -100,11 +93,7 @@ fun Route.shippingRoute(shippingController: ShippingController) { } 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( shippingController.deleteShipping(call.currentUser().userId, id), HttpStatusCode.OK diff --git a/src/main/kotlin/com/piashcse/route/ShopCategoryRoute.kt b/src/main/kotlin/com/piashcse/route/ShopCategoryRoute.kt index 7745cb5..e630a1d 100644 --- a/src/main/kotlin/com/piashcse/route/ShopCategoryRoute.kt +++ b/src/main/kotlin/com/piashcse/route/ShopCategoryRoute.kt @@ -5,6 +5,7 @@ import com.piashcse.models.shop.AddShopCategory 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 @@ -43,11 +44,7 @@ fun Route.shopCategoryRoute(shopCategoryController: ShopCategoryController) { } 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( shopCategoryController.getShopCategories( @@ -65,11 +62,7 @@ fun Route.shopCategoryRoute(shopCategoryController: ShopCategoryController) { } 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( shopCategoryController.deleteShopCategory(id), HttpStatusCode.OK @@ -88,12 +81,7 @@ fun Route.shopCategoryRoute(shopCategoryController: ShopCategoryController) { } 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( shopCategoryController.updateShopCategory(id, name), HttpStatusCode.OK diff --git a/src/main/kotlin/com/piashcse/route/ShopRoute.kt b/src/main/kotlin/com/piashcse/route/ShopRoute.kt index 523b80b..dba4355 100644 --- a/src/main/kotlin/com/piashcse/route/ShopRoute.kt +++ b/src/main/kotlin/com/piashcse/route/ShopRoute.kt @@ -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 @@ -46,12 +47,7 @@ fun Route.shopRoute(shopController: ShopController) { } 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 shopController.getShop( call.currentUser().userId, limit.toInt(), offset.toLong() ).let { @@ -70,11 +66,7 @@ fun Route.shopRoute(shopController: ShopController) { } apiResponse() }) { - val requiredParams = listOf("id", "shopName") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (id, shopName) = requiredParams.map { call.parameters[it]!! } + val (id, shopName) = call.requiredParameters("id", "shopName") ?: return@put shopController.updateShop( call.currentUser().userId, id, shopName ).let { @@ -90,12 +82,7 @@ fun Route.shopRoute(shopController: ShopController) { } 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 shopController.deleteShop( call.currentUser().userId, id ).let { diff --git a/src/main/kotlin/com/piashcse/route/UserProfileRoute.kt b/src/main/kotlin/com/piashcse/route/UserProfileRoute.kt index 41483d6..66a5e7c 100644 --- a/src/main/kotlin/com/piashcse/route/UserProfileRoute.kt +++ b/src/main/kotlin/com/piashcse/route/UserProfileRoute.kt @@ -93,10 +93,6 @@ fun Route.userProfileRoute(userProfileController: UserProfileController) { 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 diff --git a/src/main/kotlin/com/piashcse/route/UserRoute.kt b/src/main/kotlin/com/piashcse/route/UserRoute.kt index e05d36b..89dcca9 100644 --- a/src/main/kotlin/com/piashcse/route/UserRoute.kt +++ b/src/main/kotlin/com/piashcse/route/UserRoute.kt @@ -7,6 +7,7 @@ import com.piashcse.plugins.RoleManagement import com.piashcse.utils.ApiResponse import com.piashcse.utils.AppConstants import com.piashcse.utils.extension.apiResponse +import com.piashcse.utils.extension.requiredParameters import com.piashcse.utils.sendEmail import io.github.smiley4.ktorswaggerui.dsl.routing.get import io.github.smiley4.ktorswaggerui.dsl.routing.post @@ -29,8 +30,7 @@ fun Route.userRoute(userController: UserController) { val requestBody = call.receive() call.respond( ApiResponse.success( - userController.login(requestBody), - HttpStatusCode.OK + userController.login(requestBody), HttpStatusCode.OK ) ) } @@ -54,11 +54,7 @@ fun Route.userRoute(userController: UserController) { } apiResponse() }) { - val requiredParams = listOf("email") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (email) = requiredParams.map { call.parameters[it]!! } + val (email) = call.requiredParameters("email") ?: return@get val requestBody = ForgetPasswordEmail(email) userController.forgetPasswordSendCode(requestBody).let { sendEmail(requestBody.email, it.verificationCode) @@ -86,11 +82,9 @@ fun Route.userRoute(userController: UserController) { } apiResponse() }) { - val requiredParams = listOf("email", "verificationCode", "newPassword") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (email, verificationCode, newPassword) = requiredParams.map { call.parameters[it]!! } + val (email, verificationCode, newPassword) = call.requiredParameters( + "email", "verificationCode", "newPassword" + ) ?: return@get UserController().forgetPasswordVerificationCode( ConfirmPassword( @@ -131,13 +125,9 @@ fun Route.userRoute(userController: UserController) { } apiResponse() }) { - val requiredParams = listOf("oldPassword", "newPassword") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (oldPassword, newPassword) = requiredParams.map { call.parameters[it]!! } + val (oldPassword, newPassword) = call.requiredParameters("oldPassword", "newPassword") ?: return@put val loginUser = call.principal() - userController.changePassword(loginUser?.userId!!, ChangePassword(oldPassword, newPassword))?.let { + userController.changePassword(loginUser?.userId!!, ChangePassword(oldPassword, newPassword)).let { if (it) call.respond( ApiResponse.success( "Password has been changed", HttpStatusCode.OK diff --git a/src/main/kotlin/com/piashcse/route/WishListRoute.kt b/src/main/kotlin/com/piashcse/route/WishListRoute.kt index 9af51ba..2a966ce 100644 --- a/src/main/kotlin/com/piashcse/route/WishListRoute.kt +++ b/src/main/kotlin/com/piashcse/route/WishListRoute.kt @@ -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 @@ -28,7 +29,8 @@ fun Route.wishListRoute(wishlistController: WishListController) { val requestBody = call.receive() call.respond( ApiResponse.success( - wishlistController.addToWishList(call.currentUser().userId, requestBody.productId), HttpStatusCode.OK + wishlistController.addToWishList(call.currentUser().userId, requestBody.productId), + HttpStatusCode.OK ) ) } @@ -44,16 +46,11 @@ fun Route.wishListRoute(wishlistController: WishListController) { } 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( - wishlistController.getWishList(call.currentUser().userId, limit.toInt(), offset.toLong()), HttpStatusCode.OK + wishlistController.getWishList(call.currentUser().userId, limit.toInt(), offset.toLong()), + HttpStatusCode.OK ) ) } @@ -66,11 +63,7 @@ fun Route.wishListRoute(wishlistController: WishListController) { } 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( wishlistController.deleteWishList(call.currentUser().userId, productId), HttpStatusCode.OK diff --git a/src/main/kotlin/com/piashcse/utils/CustomException.kt b/src/main/kotlin/com/piashcse/utils/CustomException.kt index 10bbfd1..d6d651d 100644 --- a/src/main/kotlin/com/piashcse/utils/CustomException.kt +++ b/src/main/kotlin/com/piashcse/utils/CustomException.kt @@ -1,7 +1,6 @@ package com.piashcse.utils class UserNotExistException : Exception() -class UserTypeException : Exception() class EmailNotExist : Exception() class PasswordNotMatch : Exception() class CommonException(itemName: String) : Exception(itemName) diff --git a/src/main/kotlin/com/piashcse/utils/extension/CommonExtension.kt b/src/main/kotlin/com/piashcse/utils/extension/CommonExtension.kt index c94ea88..3d5747d 100644 --- a/src/main/kotlin/com/piashcse/utils/extension/CommonExtension.kt +++ b/src/main/kotlin/com/piashcse/utils/extension/CommonExtension.kt @@ -1,12 +1,14 @@ package com.piashcse.utils.extension import com.piashcse.models.user.body.JwtTokenBody +import com.piashcse.utils.ApiResponse import com.piashcse.utils.CommonException import com.piashcse.utils.Response import io.github.smiley4.ktorswaggerui.dsl.routes.OpenApiRoute import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.auth.* +import io.ktor.server.response.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.jetbrains.exposed.sql.transactions.transaction @@ -40,4 +42,13 @@ suspend fun query(block: () -> T): T = withContext(Dispatchers.IO) { } fun ApplicationCall.currentUser(): JwtTokenBody { return this.principal() ?: throw IllegalStateException("No authenticated user found") +} + +suspend fun ApplicationCall.requiredParameters(vararg requiredParams: String): List? { + val missingParams = requiredParams.filterNot { this.parameters.contains(it) } + if (missingParams.isNotEmpty()) { + this.respond(ApiResponse.success("Missing parameters: $missingParams", HttpStatusCode.BadRequest)) + return null + } + return requiredParams.map { this.parameters[it]!! } } \ No newline at end of file