Skip to content

Commit 42e1ffc

Browse files
authored
Merge pull request #44 from psuzn/develop
develop -> main
2 parents 937b8d7 + 59ed0a4 commit 42e1ffc

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

backend/src/main/kotlin/me/sujanpoudel/playdeals/repositories/DealRepository.kt

+2
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ interface DealRepository {
1414
suspend fun getPotentiallyExpiredDeals(): List<DealEntity>
1515

1616
suspend fun getNewDeals(since: OffsetDateTime): List<DealEntity>
17+
18+
suspend fun getDealByPackageName(packageName: String): DealEntity?
1719
}

backend/src/main/kotlin/me/sujanpoudel/playdeals/repositories/persistent/PersistentDealRepository.kt

+10
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ class PersistentDealRepository(
7979
.await()
8080
.map { it.asAppDeal() }
8181
}
82+
83+
override suspend fun getDealByPackageName(packageName: String): DealEntity? {
84+
return sqlClient.preparedQuery(
85+
"""
86+
SELECT * FROM "deal" where id = $1
87+
""".trimIndent()
88+
).exec(packageName)
89+
.await()
90+
.firstOrNull()?.asAppDeal()
91+
}
8292
}

backend/src/main/kotlin/me/sujanpoudel/playdeals/usecases/NewDealUseCase.kt

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.sujanpoudel.playdeals.usecases
22

33
import me.sujanpoudel.playdeals.jobs.AppDetailScrapper
4+
import me.sujanpoudel.playdeals.repositories.DealRepository
45
import org.jobrunr.scheduling.JobRequestScheduler
56
import org.kodein.di.DI
67
import org.kodein.di.instance
@@ -11,9 +12,17 @@ class NewDealUseCase(
1112
) : UseCase<String, Unit> {
1213

1314
private val jobRequestScheduler by di.instance<JobRequestScheduler>()
15+
private val dealsRepository by di.instance<DealRepository>()
1416

1517
override suspend fun doExecute(input: String) {
18+
val existingDeal = dealsRepository.getDealByPackageName(input)
19+
20+
if (existingDeal != null) {
21+
return
22+
}
23+
1624
val id = UUID.nameUUIDFromBytes(input.toByteArray())
25+
1726
jobRequestScheduler.enqueue(id, AppDetailScrapper.Request(input))
1827
}
1928
}

backend/src/test/kotlin/me/sujanpoudel/playdeals/api/deals/NewDealApiTest.kt

+38
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import io.kotest.matchers.shouldBe
44
import io.vertx.core.Vertx
55
import io.vertx.kotlin.core.json.jsonObjectOf
66
import io.vertx.kotlin.coroutines.await
7+
import me.sujanpoudel.playdeals.Constants
78
import me.sujanpoudel.playdeals.IntegrationTest
9+
import me.sujanpoudel.playdeals.domain.NewDeal
10+
import me.sujanpoudel.playdeals.domain.entities.DealType
811
import me.sujanpoudel.playdeals.get
12+
import me.sujanpoudel.playdeals.repositories.DealRepository
913
import org.jobrunr.jobs.states.StateName
1014
import org.jobrunr.storage.StorageProvider
1115
import org.junit.jupiter.api.Test
16+
import java.time.OffsetDateTime
1217
import java.util.UUID
1318

1419
class NewDealApiTest(vertx: Vertx) : IntegrationTest(vertx) {
@@ -53,4 +58,37 @@ class NewDealApiTest(vertx: Vertx) : IntegrationTest(vertx) {
5358

5459
response.statusCode() shouldBe 200
5560
}
61+
62+
@Test
63+
fun `should should 200 if the app already exists`() = runTest {
64+
di.get<StorageProvider>()
65+
val repository = di.get<DealRepository>()
66+
67+
val packageName = "com.example.app"
68+
69+
val newDeal = NewDeal(
70+
id = packageName,
71+
name = "name",
72+
icon = "icon",
73+
images = listOf("img0", "img1"),
74+
normalPrice = 12f,
75+
currentPrice = 12f,
76+
currency = "USD",
77+
storeUrl = "store_url",
78+
category = "unknown",
79+
downloads = "12+",
80+
rating = "12",
81+
offerExpiresIn = OffsetDateTime.now(),
82+
type = DealType.ANDROID_APP,
83+
source = Constants.DealSources.APP_DEAL_SUBREDDIT
84+
)
85+
86+
repository.upsert(newDeal)
87+
88+
val response = httpClient.post("/api/deals")
89+
.sendJson(jsonObjectOf("packageName" to packageName))
90+
.await()
91+
92+
response.statusCode() shouldBe 200
93+
}
5694
}

backend/src/test/kotlin/me/sujanpoudel/playdeals/repositories/PersistentDealRepositoryTest.kt

+21
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,25 @@ class PersistentDealRepositoryTest(vertx: Vertx) : IntegrationTest(vertx) {
114114

115115
count.shouldContainAll(deal1, deal2)
116116
}
117+
118+
@Test
119+
fun `getDealByPackageName should return correct deal by packageName`() = runTest {
120+
val deal0 = repository.upsert(newDeal)
121+
122+
repository.upsert(newDeal.copy(id = "id_1"))
123+
repository.upsert(newDeal.copy(id = "id_2"))
124+
125+
val deal01 = repository.getDealByPackageName(deal0.id)
126+
127+
deal0 shouldBe deal01
128+
}
129+
130+
@Test
131+
fun `getDealByPackageName should return null when there is no deal`() = runTest {
132+
val deal0 = repository.upsert(newDeal)
133+
134+
val deal1 = repository.getDealByPackageName("id_3")
135+
136+
deal1 shouldBe null
137+
}
117138
}

0 commit comments

Comments
 (0)