diff --git a/.gitignore b/.gitignore index 1414057..6862cc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Project exclude paths /.gradle/ /build/ +/.idea/ /src/main/resources/hikari.properties \ No newline at end of file diff --git a/src/main/kotlin/com/piashcse/models/user/body/LoginBody.kt b/src/main/kotlin/com/piashcse/models/user/body/LoginBody.kt index 12679d1..c925713 100644 --- a/src/main/kotlin/com/piashcse/models/user/body/LoginBody.kt +++ b/src/main/kotlin/com/piashcse/models/user/body/LoginBody.kt @@ -12,7 +12,7 @@ data class LoginBody( val password: String, val userType: String ) { - init{ + fun validate() { validate(this) { validate(LoginBody::email).isNotNull().isEmail() validate(LoginBody::password).isNotNull().hasSize(4, 10) diff --git a/src/main/kotlin/com/piashcse/plugins/ConfigureRequestValidation.kt b/src/main/kotlin/com/piashcse/plugins/ConfigureRequestValidation.kt index 2e8d165..ede6c3b 100644 --- a/src/main/kotlin/com/piashcse/plugins/ConfigureRequestValidation.kt +++ b/src/main/kotlin/com/piashcse/plugins/ConfigureRequestValidation.kt @@ -11,11 +11,16 @@ import com.piashcse.models.shipping.AddShipping import com.piashcse.models.shop.AddShop import com.piashcse.models.shop.AddShopCategory import com.piashcse.models.subcategory.AddProductSubCategory +import com.piashcse.models.user.body.LoginBody import io.ktor.server.application.* import io.ktor.server.plugins.requestvalidation.* fun Application.configureRequestValidation() { install(RequestValidation) { + validate { login -> + login.validate() + ValidationResult.Valid + } validate { productCategory -> productCategory.validation() ValidationResult.Valid diff --git a/src/main/kotlin/com/piashcse/route/UserRoute.kt b/src/main/kotlin/com/piashcse/route/UserRoute.kt index f8f38ba..4606b32 100644 --- a/src/main/kotlin/com/piashcse/route/UserRoute.kt +++ b/src/main/kotlin/com/piashcse/route/UserRoute.kt @@ -20,29 +20,18 @@ import io.ktor.server.routing.* fun Route.userRoute(userController: UserController) { route("user") { - get("Login", { + post("Login", { tags("User") request { - queryParameter("email") { - required = true - } - queryParameter("password") { - required = true - } - queryParameter("userType") { - required = true - } + body() } apiResponse() }) { - val requiredParams = listOf("email", "password", "userType") - requiredParams.filterNot { call.parameters.contains(it) }.let { - if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) - } - val (email, password, userType) = requiredParams.map { call.parameters[it]!! } + val requestBody = call.receive() call.respond( ApiResponse.success( - userController.login(LoginBody(email, password, userType)), HttpStatusCode.OK + userController.login(LoginBody(requestBody.email, requestBody.password, requestBody.userType)), + HttpStatusCode.OK ) ) }