Skip to content

Commit 7acd3fe

Browse files
committed
Store generated products in database via Exposed ORM
1 parent bf61796 commit 7acd3fe

File tree

22 files changed

+301
-31
lines changed

22 files changed

+301
-31
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ val kotlin_version: String by project
33
val logback_version: String by project
44
val exposed_version: String by project
55
val faker_version: String by project
6+
val postgresql_version: String by project
67

78
plugins {
89
application
@@ -26,6 +27,7 @@ dependencies {
2627
implementation("org.jetbrains.exposed:exposed-dao:$exposed_version")
2728
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
2829
implementation("org.jetbrains.exposed:exposed-jodatime:$exposed_version")
30+
implementation("org.postgresql:postgresql:$postgresql_version")
2931
implementation("io.github.serpro69:kotlin-faker:$faker_version")
3032
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
3133
implementation("io.ktor:ktor-client-serialization:$ktor_version")

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ ktor_version=2.0.0-beta-1
22
kotlin_version=1.6.10
33
logback_version=1.2.3
44
exposed_version=0.37.3
5+
postgresql_version=42.3.3
56
faker_version=1.10.0
67
kotlin.code.style=official

src/main/kotlin/com/narcissus/marketplace/api/Application.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.narcissus.marketplace.api
22

3+
import com.narcissus.marketplace.api.data.db.initDatabase
34
import io.ktor.server.engine.*
45
import io.ktor.server.netty.*
56
import com.narcissus.marketplace.api.route.*
@@ -25,6 +26,7 @@ fun main() {
2526
}
2627

2728
initConfig()
29+
initDatabase()
2830
configureRouting()
2931
}
3032
}
Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
package com.narcissus.marketplace.api.data
22

3+
import com.narcissus.marketplace.api.model.Product
34
import org.jetbrains.exposed.dao.UUIDEntity
45
import org.jetbrains.exposed.dao.UUIDEntityClass
56
import org.jetbrains.exposed.dao.id.EntityID
67
import java.util.*
78

89

910
class EntityProduct(id: EntityID<UUID>) : UUIDEntity(id) {
11+
fun toProduct(): Product =
12+
Product(
13+
id.value.toString(),
14+
name,
15+
productImg150,
16+
productImg300,
17+
productImg600,
18+
departmentId,
19+
departmentName,
20+
type,
21+
price,
22+
saleInfo?.toSaleInfo(),
23+
inStock,
24+
color,
25+
material,
26+
rating,
27+
sales,
28+
description,
29+
reviews.map(EntityReview::toReview),
30+
similarProducts.map(EntitySimilarProducts::similarProductId),
31+
)
32+
1033
companion object : UUIDEntityClass<EntityProduct>(Products)
1134

1235
var productImg150 by Products.productImg150
1336
var productImg300 by Products.productImg300
1437
var productImg600 by Products.productImg600
1538
var name by Products.name
1639
var price by Products.price
17-
var saleInfo by EntitySaleInfo referencedOn Products.saleInfo
1840
var type by Products.type
1941
var departmentId by Products.departmentId
2042
var departmentName by Products.departmentName
@@ -24,5 +46,7 @@ class EntityProduct(id: EntityID<UUID>) : UUIDEntity(id) {
2446
var rating by Products.rating
2547
var sales by Products.sales
2648
var description by Products.description
27-
var reviews by EntityReview referencedOn Products.reviews
49+
val saleInfo by EntitySaleInfo optionalReferencedOn Products.saleInfo
50+
val reviews by EntityReview optionalReferrersOn Reviews.product
51+
val similarProducts by EntitySimilarProducts optionalReferrersOn SimilarProducts.product
2852
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.narcissus.marketplace.api.data
22

3+
import com.narcissus.marketplace.api.model.Review
34
import org.jetbrains.exposed.dao.UUIDEntity
45
import org.jetbrains.exposed.dao.UUIDEntityClass
56
import org.jetbrains.exposed.dao.id.EntityID
@@ -8,9 +9,19 @@ import java.util.*
89
class EntityReview(id: EntityID<UUID>) : UUIDEntity(id) {
910
companion object : UUIDEntityClass<EntityReview>(Reviews)
1011

11-
val product by EntityProduct referencedOn Reviews.product
12+
var product by EntityProduct optionalReferencedOn Reviews.product
1213
var author by Reviews.author
1314
var details by Reviews.details
1415
var rating by Reviews.rating
1516
var avatar by Reviews.avatar
17+
18+
fun toReview(): Review =
19+
Review(
20+
id.value.toString(),
21+
product?.id?.value.toString(),
22+
author,
23+
details,
24+
rating,
25+
avatar,
26+
)
1627
}

src/main/kotlin/com/narcissus/marketplace/api/data/EntitySaleInfo.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
package com.narcissus.marketplace.api.data
22

3+
import com.narcissus.marketplace.api.model.SaleInfo
34
import org.jetbrains.exposed.dao.IntEntity
45
import org.jetbrains.exposed.dao.IntEntityClass
56
import org.jetbrains.exposed.dao.id.EntityID
67

78
class EntitySaleInfo(id: EntityID<Int>) : IntEntity(id) {
9+
fun toSaleInfo(): SaleInfo {
10+
return SaleInfo(
11+
prevPrice,
12+
percentOff,
13+
msRemaining
14+
)
15+
}
16+
817
companion object : IntEntityClass<EntitySaleInfo>(SalesInfo)
918

1019
val product by EntityProduct referencedOn SalesInfo.product
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.narcissus.marketplace.api.data
2+
3+
import org.jetbrains.exposed.dao.IntEntity
4+
import org.jetbrains.exposed.dao.IntEntityClass
5+
import org.jetbrains.exposed.dao.id.EntityID
6+
7+
class EntitySimilarProducts(id: EntityID<Int>) : IntEntity(id) {
8+
companion object : IntEntityClass<EntitySimilarProducts>(SimilarProducts)
9+
10+
var product by EntityProduct optionalReferencedOn SimilarProducts.product
11+
var similarProductId by SimilarProducts.similarProductId
12+
}

src/main/kotlin/com/narcissus/marketplace/api/data/Products.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ object Products : UUIDTable() {
88
var productImg600 = varchar("product_img_600", length = 127)
99
var name = varchar("name", length = 127)
1010
var price = integer("price")
11-
var saleInfo = reference("sale_info", SalesInfo)
12-
var type = varchar("type", length = 30)
13-
var departmentId = varchar("type", length = 30)
14-
var departmentName = varchar("type", length = 30)
11+
var saleInfo = optReference("sale_info", SalesInfo)
12+
var type = varchar("type", length = 127)
13+
var departmentId = varchar("department_id", length = 127)
14+
var departmentName = varchar("department_name", length = 127)
1515
var inStock = integer("in_stock")
16-
var color = varchar("color", length = 15)
17-
var material = varchar("material", length = 30)
16+
var color = varchar("color", length = 127)
17+
var material = varchar("material", length = 127)
1818
var rating = integer("rating")
1919
var sales = integer("sales")
2020
var description = text("description")
21-
var reviews = reference("reviews", Reviews)
21+
var similarProducts = optReference("similar_products", Products)
2222
}

src/main/kotlin/com/narcissus/marketplace/api/data/Reviews.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.narcissus.marketplace.api.data
33
import org.jetbrains.exposed.dao.id.UUIDTable
44

55
object Reviews : UUIDTable() {
6-
val product = SalesInfo.reference("product", Products)
6+
val product = optReference("product", Products)
77
var author = varchar("author", length = 127)
88
var details = text("details")
99
var rating = integer("rating")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.narcissus.marketplace.api.data
2+
3+
import org.jetbrains.exposed.dao.id.IntIdTable
4+
5+
object SimilarProducts : IntIdTable() {
6+
val product = optReference("product", Products)
7+
val similarProductId = varchar("similar", 63)
8+
}

0 commit comments

Comments
 (0)