Skip to content

Commit

Permalink
Merge pull request #11 from piashcse/payment
Browse files Browse the repository at this point in the history
- Implemented payment route
  • Loading branch information
piashcse authored Sep 28, 2024
2 parents 3d4e989 + 5ebf4ae commit 1984f7d
Show file tree
Hide file tree
Showing 23 changed files with 185 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/com/piashcse/controller/CartController.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.piashcse.controller

import com.piashcse.entities.orders.Cart
import com.piashcse.entities.orders.CartItemEntity
import com.piashcse.entities.orders.CartItemTable
import com.piashcse.entities.Cart
import com.piashcse.entities.CartItemEntity
import com.piashcse.entities.CartItemTable
import com.piashcse.entities.product.Product
import com.piashcse.entities.product.ProductEntity
import com.piashcse.entities.product.ProductTable
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/piashcse/controller/OrderController.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.piashcse.controller

import com.piashcse.entities.CartItemEntity
import com.piashcse.entities.CartItemTable
import com.piashcse.entities.orders.*
import com.piashcse.models.order.AddOrder
import com.piashcse.repository.OrderRepo
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/com/piashcse/controller/PaymentController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.piashcse.controller

import com.piashcse.entities.Payment
import com.piashcse.entities.PaymentEntity
import com.piashcse.entities.PaymentTable
import com.piashcse.entities.orders.OrderEntity
import com.piashcse.entities.orders.OrderTable
import com.piashcse.models.AddPayment
import com.piashcse.repository.PaymentRepo
import com.piashcse.utils.extension.notFoundException
import com.piashcse.utils.extension.query
import org.jetbrains.exposed.dao.id.EntityID

class PaymentController : PaymentRepo {
override suspend fun addPayment(payment: AddPayment): Payment = query {
val isOrderExist = OrderEntity.find { OrderTable.id eq payment.orderId }.toList().singleOrNull()
isOrderExist?.let {
PaymentEntity.new {
orderId = EntityID(payment.orderId, PaymentTable)
amount = payment.amount
status = payment.status
paymentMethod = payment.paymentMethod
}.response()
} ?: throw payment.orderId.notFoundException()

}

override suspend fun getPayment(paymentId: String): Payment = query {
val isOrderExist = PaymentEntity.find { PaymentTable.id eq paymentId }.toList().firstOrNull()
isOrderExist?.response() ?: throw paymentId.notFoundException()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.piashcse.controller

import com.piashcse.entities.product.category.ProductCategory
import com.piashcse.entities.product.category.ProductCategoryEntity
import com.piashcse.entities.product.category.ProductCategoryTable
import com.piashcse.entities.product.ProductCategory
import com.piashcse.entities.product.ProductCategoryEntity
import com.piashcse.entities.product.ProductCategoryTable
import com.piashcse.repository.ProductCategoryRepo
import com.piashcse.utils.extension.alreadyExistException
import com.piashcse.utils.extension.notFoundException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.piashcse.controller

import com.piashcse.entities.product.category.*
import com.piashcse.entities.product.*
import com.piashcse.models.subcategory.AddProductSubCategory
import com.piashcse.repository.ProductSubCategoryRepo
import com.piashcse.utils.extension.alreadyExistException
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/com/piashcse/database/ConfigureDataBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package com.piashcse.database

import com.piashcse.entities.ShippingTable
import com.piashcse.entities.WishListTable
import com.piashcse.entities.orders.CartItemTable
import com.piashcse.entities.CartItemTable
import com.piashcse.entities.PaymentTable
import com.piashcse.entities.orders.OrderItemTable
import com.piashcse.entities.orders.OrderTable
import com.piashcse.entities.product.BrandTable
import com.piashcse.entities.product.ProductImageTable
import com.piashcse.entities.product.ProductTable
import com.piashcse.entities.product.category.ProductCategoryTable
import com.piashcse.entities.product.category.ProductSubCategoryTable
import com.piashcse.entities.product.ProductCategoryTable
import com.piashcse.entities.product.ProductSubCategoryTable
import com.piashcse.entities.shop.ShopCategoryTable
import com.piashcse.entities.shop.ShopTable
import com.piashcse.entities.user.UserProfileTable
Expand Down Expand Up @@ -43,7 +44,8 @@ fun configureDataBase() {
OrderTable,
OrderItemTable,
WishListTable,
ShippingTable
ShippingTable,
PaymentTable
)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/piashcse/di/KoinModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ val controllerModule = module {
single { UserController() }
single { UserProfileController() }
single { WishListController() }
single { PaymentController() }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.piashcse.entities.orders
package com.piashcse.entities

import com.piashcse.entities.base.BaseIntEntity
import com.piashcse.entities.base.BaseIntEntityClass
Expand Down
39 changes: 39 additions & 0 deletions src/main/kotlin/com/piashcse/entities/Payment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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.OrderTable
import org.jetbrains.exposed.dao.id.EntityID

object PaymentTable : BaseIntIdTable("payment") {
val orderId = reference("order_id", OrderTable.id)
val amount = long("amount")
val status = varchar("status", 50) // e.g., "PENDING", "COMPLETED"
val paymentMethod = varchar("payment_method", 50) // e.g., "CREDIT_CARD", "PAYPAL"
}

class PaymentEntity(id: EntityID<String>) : BaseIntEntity(id, PaymentTable) {
companion object : BaseIntEntityClass<PaymentEntity>(PaymentTable) // Reference the Payments table
var paymentId by PaymentTable.id
var orderId by PaymentTable.orderId
var amount by PaymentTable.amount
var status by PaymentTable.status
var paymentMethod by PaymentTable.paymentMethod
fun response() = Payment(
id = paymentId.value,
orderId = orderId.value,
amount = amount,
status = status,
paymentMethod = paymentMethod,
)
}

data class Payment(
val id: String,
val orderId: String,
val amount: Long,
val status: String,
val paymentMethod: String
)

8 changes: 0 additions & 8 deletions src/main/kotlin/com/piashcse/entities/orders/Order.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import org.jetbrains.exposed.dao.id.EntityID

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()
val quantity = integer("quantity") // total number of items
val subTotal = float("sub_total")
val total = float("total")
Expand All @@ -25,8 +23,6 @@ class OrderEntity(id: EntityID<String>) : BaseIntEntity(id, OrderTable) {
companion object : BaseIntEntityClass<OrderEntity>(OrderTable)

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
Expand All @@ -38,8 +34,6 @@ class OrderEntity(id: EntityID<String>) : BaseIntEntity(id, OrderTable) {
var statusCode by OrderTable.statusCode
fun response() = Order(
id.value,
paymentId,
paymentType,
quantity,
subTotal,
total,
Expand All @@ -53,8 +47,6 @@ class OrderEntity(id: EntityID<String>) : BaseIntEntity(id, OrderTable) {
}
data class Order(
val orderId: String,
val paymentId: String?,
val paymentType: String?,
val quantity: Int,
val subTotal: Float,
val total: Float,
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/com/piashcse/entities/product/Product.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.piashcse.entities.product
import com.piashcse.entities.base.BaseIntEntity
import com.piashcse.entities.base.BaseIntEntityClass
import com.piashcse.entities.base.BaseIntIdTable
import com.piashcse.entities.product.category.ProductCategoryTable
import com.piashcse.entities.product.category.ProductSubCategoryTable
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
@@ -1,4 +1,4 @@
package com.piashcse.entities.product.category
package com.piashcse.entities.product

import com.piashcse.entities.base.BaseIntEntity
import com.piashcse.entities.base.BaseIntEntityClass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.piashcse.entities.product.category
package com.piashcse.entities.product

import com.piashcse.entities.base.BaseIntEntity
import com.piashcse.entities.base.BaseIntEntityClass
Expand Down
21 changes: 0 additions & 21 deletions src/main/kotlin/com/piashcse/entities/product/Stock.kt

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/kotlin/com/piashcse/models/AddPayment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.piashcse.models

import org.valiktor.functions.isGreaterThan
import org.valiktor.functions.isNotEmpty
import org.valiktor.functions.isNotNull
import org.valiktor.validate

data class AddPayment(
val orderId: String,
val amount: Long,
val status: String,
val paymentMethod: String
){
fun validation(){
validate(this){
validate(AddPayment::orderId).isNotNull().isNotEmpty()
validate(AddPayment::amount).isNotNull().isGreaterThan(0)
validate(AddPayment::status).isNotNull().isNotEmpty()
validate(AddPayment::paymentMethod).isNotNull().isNotEmpty()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.piashcse.plugins

import com.piashcse.models.AddWisList
import com.piashcse.models.AddPayment
import com.piashcse.models.bands.AddBrand
import com.piashcse.models.cart.AddCart
import com.piashcse.models.category.AddProductCategory
Expand Down Expand Up @@ -55,5 +56,9 @@ fun Application.configureRequestValidation() {
wishlist.validation()
ValidationResult.Valid
}
validate<AddPayment> { payment ->
payment.validation()
ValidationResult.Valid
}
}
}
2 changes: 2 additions & 0 deletions src/main/kotlin/com/piashcse/plugins/ConfigureRouting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fun Application.configureRoute() {
val wishListController: WishListController by inject()
val shippingController: ShippingController by inject()
val orderController: OrderController by inject()
val paymentController: PaymentController by inject()
routing {
route {
userRoute(userController)
Expand All @@ -34,6 +35,7 @@ fun Application.configureRoute() {
wishListRoute(wishListController)
shippingRoute(shippingController)
orderRoute(orderController)
paymentRoute(paymentController)
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/piashcse/repository/CartRepo.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.piashcse.repository

import com.piashcse.entities.orders.Cart
import com.piashcse.entities.Cart
import com.piashcse.entities.product.Product

interface CartRepo {
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/piashcse/repository/PaymentRepo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.piashcse.repository

import com.piashcse.entities.Payment
import com.piashcse.models.AddPayment

interface PaymentRepo {
suspend fun addPayment(payment: AddPayment): Payment
suspend fun getPayment(paymentId:String): Payment
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.piashcse.repository

import com.piashcse.entities.product.category.ProductCategory
import com.piashcse.entities.product.ProductCategory

interface ProductCategoryRepo {
suspend fun addProductCategory(categoryName: String): ProductCategory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.piashcse.repository

import com.piashcse.entities.product.category.ProductSubCategory
import com.piashcse.entities.product.ProductSubCategory
import com.piashcse.models.subcategory.AddProductSubCategory

interface ProductSubCategoryRepo {
Expand Down
17 changes: 0 additions & 17 deletions src/main/kotlin/com/piashcse/route/OrderRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,6 @@ fun Route.orderRoute(orderController: OrderController) {
)
)
}
get("{id}/order-payment", {
tags("Order")
request {
queryParameter<String>("id") {
required = true
}
}
apiResponse()
}) {
val orderId = call.parameters["id"]!!
call.respond(
ApiResponse.success(
orderController.updateOrder(getCurrentUser().userId, orderId, OrderStatus.PAID),
HttpStatusCode.OK
)
)
}
get("{id}/order-cancel", {
tags("Order")
request {
Expand Down
Loading

0 comments on commit 1984f7d

Please sign in to comment.