Skip to content

Commit

Permalink
- Code Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
piashcse committed Sep 2, 2024
1 parent 9219f2d commit 972d712
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/com/piashcse/controller/OrderController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/piashcse/controller/WishListController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/com/piashcse/database/ConfigureDataBase.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,7 +39,7 @@ fun configureDataBase() {
ProductSubCategoryTable,
BrandTable,
CartItemTable,
OrdersTable,
OrderTable,
OrderItemTable,
WishListTable,
ShippingTable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) : BaseIntEntity(id, CouponsTable) {
companion object : BaseIntEntityClass<CouponsEntity>(CouponsTable)
class CouponsEntity(id: EntityID<String>) : BaseIntEntity(id, CouponTable) {
companion object : BaseIntEntityClass<CouponsEntity>(CouponTable)

var coupons by CouponsTable.coupon
var discount by CouponsTable.discount
var coupons by CouponTable.coupon
var discount by CouponTable.discount
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/piashcse/entities/orders/Cart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -22,21 +21,21 @@ object OrdersTable : BaseIntIdTable("orders") {
val statusCode = integer("status_code").clientDefault { 0 }
}

class OrderEntity(id: EntityID<String>) : BaseIntEntity(id, OrdersTable) {
companion object : BaseIntEntityClass<OrderEntity>(OrdersTable)
class OrderEntity(id: EntityID<String>) : BaseIntEntity(id, OrderTable) {
companion object : BaseIntEntityClass<OrderEntity>(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,
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/piashcse/entities/orders/OrderItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/piashcse/entities/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions src/main/kotlin/com/piashcse/route/BrandRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fun Route.brandRoute(brandController: BrandController) {
put("", {
tags("Brand")
request {
queryParameter<String>("brandId"){
queryParameter<String>("id"){
required = true
}
queryParameter<String>("brandName"){
Expand All @@ -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<String>("brandId"){
queryParameter<String>("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
)
)
}
Expand Down

0 comments on commit 972d712

Please sign in to comment.