Skip to content

Commit

Permalink
Merge pull request #31 from psuzn/develop
Browse files Browse the repository at this point in the history
develop -> main
  • Loading branch information
psuzn authored Oct 3, 2023
2 parents 0bc4a96 + 044d562 commit 919972c
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 81 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @psuzn
70 changes: 70 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: play-deals-backend 1.0 CD
on:
push:
tags:
- '*'
jobs:
build_image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@ccb4328a959376b642e027874838f60f8e596de3

- name: Build Project with Gradle
uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629
with:
arguments: build

- uses: extractions/setup-just@v1

- name: Build and publish image with jib
if: ${{ github.ref == 'refs/heads/main' }}
run: just build-push-image
env:
DOCKER_USER: ${{ github.actor }}
DOCKER_PASSWORD: ${{ github.token }}

deploy_on_k8:
runs-on: ubuntu-latest
needs: build_image
steps:
- uses: actions/checkout@v3

- uses: extractions/setup-just@v1

- uses: azure/setup-helm@v3

- uses: azure/setup-kubectl@v3
id: install
name: Setup kubectl client

- name: create KUBECONFIG
if: ${{ github.ref == 'refs/heads/main' }}
run: |
echo ${{ secrets.KUBE_CONFIG }} | base64 --decode > kube-config;
chmod 600 kube-config;
echo "KUBECONFIG=$(pwd)/kube-config" >> "$GITHUB_ENV"
- name: Deploy on k8
if: ${{ github.ref == 'refs/heads/main' }}
run: |
kubectl config current-context;
just helm-upgrade;
env:
DB_HOST: ${{ secrets.DB_HOST }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_NAME: ${{ secrets.DB_NAME }}
DASHBOARD: ${{ secrets.DASHBOARD }}
DASHBOARD_USER: ${{ secrets.DASHBOARD_USER }}
DASHBOARD_PASS: ${{ secrets.DASHBOARD_PASS }}


48 changes: 7 additions & 41 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,64 +1,30 @@
name: play-deals-backend 1.0 CI

name: play-deals-backend 1.1 CI
on:
push:
pull_request:
workflow_dispatch:

jobs:
build:
checks:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
cache: gradle

- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@ccb4328a959376b642e027874838f60f8e596de3
uses: gradle/wrapper-validation-action@v1.1.0

- name: Build Project with Gradle
uses: gradle/gradle-build-action@749f47bda3e44aa060e82d7b3ef7e40d953bd629
with:
arguments: build

- uses: extractions/setup-just@v1

- name: Build and publish image with jib
if: ${{ github.ref == 'refs/heads/main' }}
run: just build-push-image
env:
DOCKER_USER: ${{ github.actor }}
DOCKER_PASSWORD: ${{ github.token }}

- uses: azure/setup-helm@v3
- uses: azure/setup-kubectl@v3
id: install
name: Setup kubectl client

- name: create KUBECONFIG
if: ${{ github.ref == 'refs/heads/main' }}
run: |
echo ${{ secrets.KUBE_CONFIG }} | base64 --decode > kube-config;
chmod 600 kube-config;
echo "KUBECONFIG=$(pwd)/kube-config" >> "$GITHUB_ENV"
- name: Deploy on k8
if: ${{ github.ref == 'refs/heads/main' }}
run: |
kubectl config current-context;
just helm-upgrade;
env:
DB_HOST: ${{ secrets.DB_HOST }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_PORT: ${{ secrets.DB_PORT }}
DB_NAME: ${{ secrets.DB_NAME }}
DASHBOARD: ${{ secrets.DASHBOARD }}
DASHBOARD_USER: ${{ secrets.DASHBOARD_USER }}
DASHBOARD_PASS: ${{ secrets.DASHBOARD_PASS }}




This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package me.sujanpoudel.playdeals.domain.entities

import io.vertx.core.json.Json
import io.vertx.sqlclient.Row
import me.sujanpoudel.playdeals.common.asEnum
import me.sujanpoudel.playdeals.common.get
import kotlin.reflect.KClass

fun Row.asAppDeal(): DealEntity {
return DealEntity(
Expand All @@ -28,11 +26,5 @@ fun Row.asAppDeal(): DealEntity {
)
}

fun <T : Any> Row.asKeyValue(clazz: KClass<out T>): KeyValueEntity<T> {
return KeyValueEntity(
get("key"),
getString("value").let {
Json.CODEC.fromValue(it, clazz.java)
}
)
}
fun Row?.valueOrNull() = this?.getString("value")
fun Row.value(): String = getString("value")
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import me.sujanpoudel.playdeals.common.SIMPLE_NAME
import me.sujanpoudel.playdeals.common.loggingExecutionTime
import me.sujanpoudel.playdeals.log
import me.sujanpoudel.playdeals.repositories.KeyValuesRepository
import me.sujanpoudel.playdeals.repositories.get
import org.jobrunr.jobs.lambdas.JobRequest
import org.jobrunr.scheduling.JobRequestScheduler
import org.jobrunr.scheduling.RecurringJobBuilder
Expand Down Expand Up @@ -45,7 +44,7 @@ class RedditPostsScrapper(
override suspend fun handleRequest(jobRequest: Request): Unit = loggingExecutionTime(
"$SIMPLE_NAME:: handleRequest"
) {
val lastPostTime = keyValueRepository.get<String>(LAST_REDDIT_POST_TIME)?.let(OffsetDateTime::parse)
val lastPostTime = keyValueRepository.get(LAST_REDDIT_POST_TIME)?.let(OffsetDateTime::parse)

val posts = loggingExecutionTime(
"$SIMPLE_NAME:: Fetched reddit post, last created post was at : '$lastPostTime'"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package me.sujanpoudel.playdeals.repositories

import me.sujanpoudel.playdeals.domain.entities.KeyValueEntity
import kotlin.reflect.KClass

interface KeyValuesRepository {
suspend fun <T : Any> set(key: String, value: T, clazz: KClass<out T> = value::class): KeyValueEntity<T>
suspend fun set(key: String, value: String): String

suspend fun <T : Any> get(key: String, clazz: KClass<T>): T?
suspend fun get(key: String): String?

suspend fun delete(key: String)
}

suspend inline fun <reified T : Any> KeyValuesRepository.set(key: String, value: T): KeyValueEntity<T> {
return set(key, value, T::class)
}

suspend inline fun <reified T : Any> KeyValuesRepository.get(key: String) = get(key, T::class)
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
package me.sujanpoudel.playdeals.repositories.persistent

import io.vertx.core.json.Json
import io.vertx.kotlin.coroutines.await
import io.vertx.sqlclient.SqlClient
import me.sujanpoudel.playdeals.common.exec
import me.sujanpoudel.playdeals.domain.entities.KeyValueEntity
import me.sujanpoudel.playdeals.domain.entities.asKeyValue
import me.sujanpoudel.playdeals.domain.entities.value
import me.sujanpoudel.playdeals.domain.entities.valueOrNull
import me.sujanpoudel.playdeals.repositories.KeyValuesRepository
import kotlin.reflect.KClass

private fun Any.serializeToString(): String = Json.CODEC.toString(this)

class PersistentKeyValuesRepository(
private val sqlClient: SqlClient
) : KeyValuesRepository {

override suspend fun <T : Any> set(key: String, value: T, clazz: KClass<out T>): KeyValueEntity<T> {
override suspend fun set(key: String, value: String): String {
return sqlClient.preparedQuery(
"""
INSERT INTO "key_value_store" VALUES ($1,$2)
ON CONFLICT(key) DO UPDATE SET value = $2
RETURNING *
""".trimIndent()
).exec(key, value.serializeToString())
).exec(key, value)
.await()
.first()
.asKeyValue<T>(clazz)
.value()
}

override suspend fun <T : Any> get(key: String, clazz: KClass<T>): T? {
override suspend fun get(key: String): String? {
return sqlClient.preparedQuery(
"""
SELECT * FROM "key_value_store" WHERE key = $1
""".trimIndent()
).exec(key)
.await()
.firstOrNull()?.asKeyValue(clazz)?.value
.firstOrNull()
.valueOrNull()
}

override suspend fun delete(key: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package me.sujanpoudel.playdeals.repositories

import io.kotest.matchers.shouldBe
import io.vertx.core.Vertx
import io.vertx.kotlin.coroutines.await
import io.vertx.sqlclient.SqlClient
import me.sujanpoudel.playdeals.IntegrationTest
import me.sujanpoudel.playdeals.common.exec
import me.sujanpoudel.playdeals.domain.entities.value
import me.sujanpoudel.playdeals.get
import org.junit.jupiter.api.Test
import java.time.OffsetDateTime

class PersistentKeyValueRepositoryTest(vertx: Vertx) : IntegrationTest(vertx) {

private val repository by lazy { di.get<KeyValuesRepository>() }
private val sqlClient by lazy { di.get<SqlClient>() }

@Test
fun `should create new entry on db`() = runTest {
val value = repository.set(KEY, "test")

val valueFromDb = sqlClient.preparedQuery(""" SELECT * from "key_value_store" where key=$1""")
.exec(KEY)
.await()
.first()
.getString("value")

value shouldBe valueFromDb
}

@Test
fun `should perform update if item with id already exists`() = runTest {
repository.set(KEY, "test")

val updated = repository.set(KEY, "test1")

val fromDb = sqlClient.preparedQuery(""" SELECT * from "key_value_store" where key=$1""")
.exec(KEY)
.await()
.first()
.value()

fromDb shouldBe updated
}

@Test
fun `should be able to serialize unknown types`() = runTest {
val value = OffsetDateTime.now()

repository.set(KEY, value.toString())

val fromDb = repository.get(KEY).let(OffsetDateTime::parse)

fromDb shouldBe value
}

companion object {
const val KEY = "test_key"
}
}

0 comments on commit 919972c

Please sign in to comment.