From 972d71245834a889960bb5c10568b73624cc2261 Mon Sep 17 00:00:00 2001 From: mehedihasanpiash Date: Mon, 2 Sep 2024 11:37:21 +0600 Subject: [PATCH] - Code Refactor --- .../piashcse/controller/OrderController.kt | 6 ++-- .../piashcse/controller/WishListController.kt | 4 +-- .../piashcse/database/ConfigureDataBase.kt | 5 +-- .../entities/{Settings.kt => Setting.kt} | 2 +- .../entities/{Shippings.kt => Shipping.kt} | 4 +-- .../entities/{product => }/WishList.kt | 4 ++- .../entities/coupon/{Coupons.kt => Coupon.kt} | 10 +++--- .../com/piashcse/entities/orders/Cart.kt | 2 +- .../entities/orders/{Orders.kt => Order.kt} | 31 +++++++++---------- .../com/piashcse/entities/orders/OrderItem.kt | 2 +- .../kotlin/com/piashcse/entities/user/User.kt | 2 +- .../kotlin/com/piashcse/route/BrandRoute.kt | 16 +++++----- 12 files changed, 45 insertions(+), 43 deletions(-) rename src/main/kotlin/com/piashcse/entities/{Settings.kt => Setting.kt} (94%) rename src/main/kotlin/com/piashcse/entities/{Shippings.kt => Shipping.kt} (94%) rename src/main/kotlin/com/piashcse/entities/{product => }/WishList.kt (85%) rename src/main/kotlin/com/piashcse/entities/coupon/{Coupons.kt => Coupon.kt} (63%) rename src/main/kotlin/com/piashcse/entities/orders/{Orders.kt => Order.kt} (72%) diff --git a/src/main/kotlin/com/piashcse/controller/OrderController.kt b/src/main/kotlin/com/piashcse/controller/OrderController.kt index 6550701..5d9b6e3 100644 --- a/src/main/kotlin/com/piashcse/controller/OrderController.kt +++ b/src/main/kotlin/com/piashcse/controller/OrderController.kt @@ -12,7 +12,7 @@ import org.jetbrains.exposed.sql.and class OrderController { suspend fun createOrder(userId: String, addOrder: AddOrder) = query { val order = OrderEntity.new { - this.userId = EntityID(userId, OrdersTable) + this.userId = EntityID(userId, OrderTable) this.quantity = addOrder.quantity this.shippingCharge = addOrder.shippingCharge this.subTotal = addOrder.subTotal @@ -36,14 +36,14 @@ class OrderController { } suspend fun getOrders(userId: String, limit: Int, offset:Long) = query { - OrderEntity.find { OrdersTable.userId eq userId }.limit(limit, offset).map { + OrderEntity.find { OrderTable.userId eq userId }.limit(limit, offset).map { it.response() } } suspend fun updateOrder(userId: String, orderId: String, orderStatus: OrderStatus) = query { val orderExist = - OrderEntity.find { OrdersTable.userId eq userId and (OrdersTable.id eq orderId) }.toList() + OrderEntity.find { OrderTable.userId eq userId and (OrderTable.id eq orderId) }.toList() .singleOrNull() orderExist?.let { it.status = orderStatus.name.lowercase() diff --git a/src/main/kotlin/com/piashcse/controller/WishListController.kt b/src/main/kotlin/com/piashcse/controller/WishListController.kt index 01a2291..2a2e001 100644 --- a/src/main/kotlin/com/piashcse/controller/WishListController.kt +++ b/src/main/kotlin/com/piashcse/controller/WishListController.kt @@ -2,8 +2,8 @@ package com.piashcse.controller import com.piashcse.entities.product.ProductEntity import com.piashcse.entities.product.ProductTable -import com.piashcse.entities.product.WishListEntity -import com.piashcse.entities.product.WishListTable +import com.piashcse.entities.WishListEntity +import com.piashcse.entities.WishListTable import com.piashcse.utils.extension.alreadyExistException import com.piashcse.utils.extension.isNotExistException import com.piashcse.utils.extension.query diff --git a/src/main/kotlin/com/piashcse/database/ConfigureDataBase.kt b/src/main/kotlin/com/piashcse/database/ConfigureDataBase.kt index 5f78f4d..de72fb8 100644 --- a/src/main/kotlin/com/piashcse/database/ConfigureDataBase.kt +++ b/src/main/kotlin/com/piashcse/database/ConfigureDataBase.kt @@ -1,11 +1,12 @@ package com.piashcse.database import com.piashcse.entities.ShippingTable +import com.piashcse.entities.WishListTable import com.piashcse.entities.product.category.ProductCategoryTable import com.piashcse.entities.product.category.ProductSubCategoryTable import com.piashcse.entities.orders.CartItemTable import com.piashcse.entities.orders.OrderItemTable -import com.piashcse.entities.orders.OrdersTable +import com.piashcse.entities.orders.OrderTable import com.piashcse.entities.product.* import com.piashcse.entities.shop.ShopCategoryTable import com.piashcse.entities.shop.ShopTable @@ -38,7 +39,7 @@ fun configureDataBase() { ProductSubCategoryTable, BrandTable, CartItemTable, - OrdersTable, + OrderTable, OrderItemTable, WishListTable, ShippingTable diff --git a/src/main/kotlin/com/piashcse/entities/Settings.kt b/src/main/kotlin/com/piashcse/entities/Setting.kt similarity index 94% rename from src/main/kotlin/com/piashcse/entities/Settings.kt rename to src/main/kotlin/com/piashcse/entities/Setting.kt index 666e837..d502832 100644 --- a/src/main/kotlin/com/piashcse/entities/Settings.kt +++ b/src/main/kotlin/com/piashcse/entities/Setting.kt @@ -5,7 +5,7 @@ import com.piashcse.entities.base.BaseIntEntityClass import com.piashcse.entities.base.BaseIntIdTable import org.jetbrains.exposed.dao.id.EntityID -object SettingTable : BaseIntIdTable("settings") { +object SettingTable : BaseIntIdTable("setting") { val shippingCharge = integer("shipping_charge") val shopName = text("shop_name") val email = varchar("email", 50) diff --git a/src/main/kotlin/com/piashcse/entities/Shippings.kt b/src/main/kotlin/com/piashcse/entities/Shipping.kt similarity index 94% rename from src/main/kotlin/com/piashcse/entities/Shippings.kt rename to src/main/kotlin/com/piashcse/entities/Shipping.kt index 1a84fa8..15e2620 100644 --- a/src/main/kotlin/com/piashcse/entities/Shippings.kt +++ b/src/main/kotlin/com/piashcse/entities/Shipping.kt @@ -3,13 +3,13 @@ package com.piashcse.entities import com.piashcse.entities.base.BaseIntEntity import com.piashcse.entities.base.BaseIntEntityClass import com.piashcse.entities.base.BaseIntIdTable -import com.piashcse.entities.orders.OrdersTable +import com.piashcse.entities.orders.OrderTable import com.piashcse.entities.user.UserTable import org.jetbrains.exposed.dao.id.EntityID object ShippingTable : BaseIntIdTable("Shipping") { val userId = reference("user_id", UserTable.id) - val orderId = reference("order_id", OrdersTable.id) + val orderId = reference("order_id", OrderTable.id) val shippingAddress = varchar("ship_address", 150) val shippingCity = varchar("ship_city", 50) val shippingPhone = integer("ship_phone") diff --git a/src/main/kotlin/com/piashcse/entities/product/WishList.kt b/src/main/kotlin/com/piashcse/entities/WishList.kt similarity index 85% rename from src/main/kotlin/com/piashcse/entities/product/WishList.kt rename to src/main/kotlin/com/piashcse/entities/WishList.kt index 2fdef38..937e408 100644 --- a/src/main/kotlin/com/piashcse/entities/product/WishList.kt +++ b/src/main/kotlin/com/piashcse/entities/WishList.kt @@ -1,8 +1,10 @@ -package com.piashcse.entities.product +package com.piashcse.entities import com.piashcse.entities.base.BaseIntEntity import com.piashcse.entities.base.BaseIntEntityClass import com.piashcse.entities.base.BaseIntIdTable +import com.piashcse.entities.product.Product +import com.piashcse.entities.product.ProductTable import com.piashcse.entities.user.UserTable import org.jetbrains.exposed.dao.id.EntityID diff --git a/src/main/kotlin/com/piashcse/entities/coupon/Coupons.kt b/src/main/kotlin/com/piashcse/entities/coupon/Coupon.kt similarity index 63% rename from src/main/kotlin/com/piashcse/entities/coupon/Coupons.kt rename to src/main/kotlin/com/piashcse/entities/coupon/Coupon.kt index 01c5063..59b7d25 100644 --- a/src/main/kotlin/com/piashcse/entities/coupon/Coupons.kt +++ b/src/main/kotlin/com/piashcse/entities/coupon/Coupon.kt @@ -5,14 +5,14 @@ import com.piashcse.entities.base.BaseIntEntityClass import com.piashcse.entities.base.BaseIntIdTable import org.jetbrains.exposed.dao.id.EntityID -object CouponsTable : BaseIntIdTable("coupons") { +object CouponTable : BaseIntIdTable("coupon") { val coupon = varchar("coupon", 50) val discount = integer("discount") } -class CouponsEntity(id: EntityID) : BaseIntEntity(id, CouponsTable) { - companion object : BaseIntEntityClass(CouponsTable) +class CouponsEntity(id: EntityID) : BaseIntEntity(id, CouponTable) { + companion object : BaseIntEntityClass(CouponTable) - var coupons by CouponsTable.coupon - var discount by CouponsTable.discount + var coupons by CouponTable.coupon + var discount by CouponTable.discount } \ No newline at end of file diff --git a/src/main/kotlin/com/piashcse/entities/orders/Cart.kt b/src/main/kotlin/com/piashcse/entities/orders/Cart.kt index 7fec222..58c77f0 100644 --- a/src/main/kotlin/com/piashcse/entities/orders/Cart.kt +++ b/src/main/kotlin/com/piashcse/entities/orders/Cart.kt @@ -8,7 +8,7 @@ import com.piashcse.entities.product.ProductTable import com.piashcse.entities.user.UserTable import org.jetbrains.exposed.dao.id.EntityID -object CartItemTable : BaseIntIdTable("cart_items") { +object CartItemTable : BaseIntIdTable("cart_item") { val userId = reference("user_id", UserTable.id) val productId = reference("product_id", ProductTable.id) val quantity = integer("quantity") diff --git a/src/main/kotlin/com/piashcse/entities/orders/Orders.kt b/src/main/kotlin/com/piashcse/entities/orders/Order.kt similarity index 72% rename from src/main/kotlin/com/piashcse/entities/orders/Orders.kt rename to src/main/kotlin/com/piashcse/entities/orders/Order.kt index abd22de..a85234e 100644 --- a/src/main/kotlin/com/piashcse/entities/orders/Orders.kt +++ b/src/main/kotlin/com/piashcse/entities/orders/Order.kt @@ -5,9 +5,8 @@ import com.piashcse.entities.base.BaseIntEntityClass import com.piashcse.entities.base.BaseIntIdTable import com.piashcse.entities.user.UserTable import org.jetbrains.exposed.dao.id.EntityID -import java.util.StringJoiner -object OrdersTable : BaseIntIdTable("orders") { +object OrderTable : BaseIntIdTable("order") { val userId = reference("user_id", UserTable.id) val paymentId = varchar("payment_id", 50).nullable() val paymentType = varchar("payment_type", 50).nullable() @@ -22,21 +21,21 @@ object OrdersTable : BaseIntIdTable("orders") { val statusCode = integer("status_code").clientDefault { 0 } } -class OrderEntity(id: EntityID) : BaseIntEntity(id, OrdersTable) { - companion object : BaseIntEntityClass(OrdersTable) +class OrderEntity(id: EntityID) : BaseIntEntity(id, OrderTable) { + companion object : BaseIntEntityClass(OrderTable) - var userId by OrdersTable.userId - var paymentId by OrdersTable.paymentId - var paymentType by OrdersTable.paymentType - var quantity by OrdersTable.quantity - var subTotal by OrdersTable.subTotal - var total by OrdersTable.total - var shippingCharge by OrdersTable.shippingCharge - var vat by OrdersTable.vat - var cancelOrder by OrdersTable.cancelOrder - var coupon by OrdersTable.coupon - var status by OrdersTable.status - var statusCode by OrdersTable.statusCode + var userId by OrderTable.userId + var paymentId by OrderTable.paymentId + var paymentType by OrderTable.paymentType + var quantity by OrderTable.quantity + var subTotal by OrderTable.subTotal + var total by OrderTable.total + var shippingCharge by OrderTable.shippingCharge + var vat by OrderTable.vat + var cancelOrder by OrderTable.cancelOrder + var coupon by OrderTable.coupon + var status by OrderTable.status + var statusCode by OrderTable.statusCode fun orderCreatedResponse() = OrderCreatedPayload(id.value) fun response() = OrderPayload( id.value, diff --git a/src/main/kotlin/com/piashcse/entities/orders/OrderItem.kt b/src/main/kotlin/com/piashcse/entities/orders/OrderItem.kt index 5c4e38e..fe1f135 100644 --- a/src/main/kotlin/com/piashcse/entities/orders/OrderItem.kt +++ b/src/main/kotlin/com/piashcse/entities/orders/OrderItem.kt @@ -7,7 +7,7 @@ import com.piashcse.entities.product.ProductTable import org.jetbrains.exposed.dao.id.EntityID object OrderItemTable : BaseIntIdTable("order_items") { - val orderId = reference("order_id", OrdersTable.id) + val orderId = reference("order_id", OrderTable.id) val productId = reference("product_id", ProductTable.id) val quantity = integer("quantity") } diff --git a/src/main/kotlin/com/piashcse/entities/user/User.kt b/src/main/kotlin/com/piashcse/entities/user/User.kt index 2386248..de1c9fa 100644 --- a/src/main/kotlin/com/piashcse/entities/user/User.kt +++ b/src/main/kotlin/com/piashcse/entities/user/User.kt @@ -7,7 +7,7 @@ import com.piashcse.models.user.body.JwtTokenBody import com.piashcse.controller.JwtController import org.jetbrains.exposed.dao.id.EntityID -object UserTable : BaseIntIdTable("users") { +object UserTable : BaseIntIdTable("user") { val email = varchar("email", 50) val password = varchar("password", 200) val userType = varchar("user_type", 100) diff --git a/src/main/kotlin/com/piashcse/route/BrandRoute.kt b/src/main/kotlin/com/piashcse/route/BrandRoute.kt index 3711f30..e1d1b27 100644 --- a/src/main/kotlin/com/piashcse/route/BrandRoute.kt +++ b/src/main/kotlin/com/piashcse/route/BrandRoute.kt @@ -62,7 +62,7 @@ fun Route.brandRoute(brandController: BrandController) { put("", { tags("Brand") request { - queryParameter("brandId"){ + queryParameter("id"){ required = true } queryParameter("brandName"){ @@ -71,34 +71,34 @@ fun Route.brandRoute(brandController: BrandController) { } apiResponse() }) { - val requiredParams = listOf("brandId", "brandName") + val requiredParams = listOf("id", "brandName") requiredParams.filterNot { call.request.queryParameters.contains(it) }.let { if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) } - val (brandId, brandName) = requiredParams.map { call.parameters[it]!! } + val (id, brandName) = requiredParams.map { call.parameters[it]!! } call.respond( ApiResponse.success( - brandController.updateBrand(brandId, brandName), HttpStatusCode.OK + brandController.updateBrand(id, brandName), HttpStatusCode.OK ) ) } delete("", { tags("Brand") request { - queryParameter("brandId"){ + queryParameter("id"){ required = true } } apiResponse() }) { - val requiredParams = listOf("brandId") + val requiredParams = listOf("id") requiredParams.filterNot { call.request.queryParameters.contains(it) }.let { if (it.isNotEmpty()) call.respond(ApiResponse.success("Missing parameters: $it", HttpStatusCode.OK)) } - val (brandId) = requiredParams.map { call.parameters[it]!! } + val (id) = requiredParams.map { call.parameters[it]!! } call.respond( ApiResponse.success( - brandController.deleteBrand(brandId), HttpStatusCode.OK + brandController.deleteBrand(id), HttpStatusCode.OK ) ) }