Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 배너 이미지 조회 #18

Merged
merged 14 commits into from
Jan 26, 2024
Merged
9 changes: 4 additions & 5 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ jobs:
tags: ${{ secrets.DOCKER_HUB_REPOSITORY }}:prod

deployment:
runs-on: ubuntu-latest
needs: [ build-docker-image-and-push ]
steps:
runs-on: ubuntu-latest
needs: [ build-docker-image-and-push ]
steps:
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

도커 공부 해야하는데 언제하죠ㅋㅋㅋㅎ

- name: Deploy
uses: appleboy/ssh-action@master
with:
Expand All @@ -57,6 +57,5 @@ jobs:
script: |
echo ${{ secrets.DOCKERHUB_TOKEN }} | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
cd api/bin/
sudo docker compose down
sudo docker compose down --rmi all
sudo docker compose up -d
sudo docker image prune -af
2 changes: 0 additions & 2 deletions .github/workflows/gradle-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: CI With Pull Request

on:
push:
pull_request:
types: [opened, reopened]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion backend-submodule
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ dependencies {
implementation("com.linecorp.kotlin-jdsl:jpql-dsl:3.3.0")
implementation("com.linecorp.kotlin-jdsl:jpql-render:3.3.0")

implementation("org.springframework.boot:spring-boot-starter-cache")

runtimeOnly("com.mysql:mysql-connector-j")
runtimeOnly("com.h2database:h2")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.kotest:kotest-runner-junit5:5.4.2")
testImplementation("io.kotest:kotest-assertions-core:5.4.2")
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/com/petqua/application/banner/BannerDtos.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.petqua.application.banner

import com.petqua.domain.banner.Banner
import java.time.LocalDateTime

data class FindBannerResult(
val id: Long,
val imageUrl: String,
val linkUrl: String,
val createAt: LocalDateTime,
val updateAt: LocalDateTime,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createAt, updateAt 도 응답하네요! 클라이언트에게 아마도 필요하지 않을 정보를 응답하는 이유가 궁금합니다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제없는 범위 내에서 createdAt을 자주 응답하긴 했었습니다.. 추후에 추가되는 경우가 종종 있었거든요!
굳이 응답할 필요 없는 것 같은데 제거해두겠습니다~!

) {
companion object {
fun from(banner: Banner): FindBannerResult {
return FindBannerResult(
id = banner.id,
imageUrl = banner.imageUrl,
linkUrl = banner.linkUrl,
createAt = banner.createdAt,
updateAt = banner.updatedAt,
)
}
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/com/petqua/application/banner/BannerService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.petqua.application.banner

import com.petqua.domain.banner.BannerRepository
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Transactional
@Service
class BannerService(
private val bannerRepository: BannerRepository,
) {

@Cacheable("banners")
Comment on lines +14 to +15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

캐시 좋네요! 이거 나중에 레디스로 이사하나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 cacheConfig에서 구현체만 수정하면 될 것 같군요!
레디스를 생각하진 않았는데, 레디스가 도입되면 얘도 거기서 처리하면 되겠네요

@Transactional(readOnly = true)
fun getBannerList(): List<FindBannerResult> {
val banners = bannerRepository.findAll()
return banners.map { FindBannerResult.from(it) }
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/com/petqua/common/cofig/CacheConfiguration.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.petqua.common.cofig

import org.springframework.cache.CacheManager
import org.springframework.cache.annotation.EnableCaching
import org.springframework.cache.concurrent.ConcurrentMapCacheManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration


@EnableCaching
@Configuration
class CacheConfiguration {

@Bean
fun cacheManager(): CacheManager {
val cacheManager = ConcurrentMapCacheManager()
cacheManager.setCacheNames(listOf("banners"))
return cacheManager
}
}
16 changes: 16 additions & 0 deletions src/main/kotlin/com/petqua/domain/banner/Banner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.petqua.domain.banner

import com.petqua.common.domain.BaseEntity
import jakarta.persistence.*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* 👻 귀신이다아아


@Entity
class Banner(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0L,

@Column(nullable = false)
val imageUrl: String,

@Column(nullable = false)
val linkUrl: String,
) : BaseEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.petqua.domain.banner

import org.springframework.data.jpa.repository.JpaRepository

interface BannerRepository: JpaRepository<Banner, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.petqua.presentation.banner

import com.petqua.application.banner.BannerService
import com.petqua.application.banner.FindBannerResult
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RequestMapping("/banner")
@RestController
class BannerController(
private val bannerService: BannerService
) {

@GetMapping
fun getBanners(): ResponseEntity<List<FindBannerResult>> {
val bannerList = bannerService.getBannerList()
return ResponseEntity.ok(bannerList)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.petqua.application.banner

import com.petqua.domain.banner.Banner
import com.petqua.domain.banner.BannerRepository
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import org.mockito.Mockito.atMost
import org.mockito.Mockito.verify
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE
import org.springframework.boot.test.mock.mockito.SpyBean
import org.springframework.test.context.TestConstructor
import org.springframework.test.context.TestConstructor.AutowireMode.ALL

@TestConstructor(autowireMode = ALL)
@SpringBootTest(webEnvironment = NONE)
class BannerServiceTest(
private var bannerService: BannerService,
@SpyBean private var bannerRepository: BannerRepository,
) : BehaviorSpec({

Given("Banner 조회 테스트") {
bannerRepository.saveAll(
listOf(
Banner(imageUrl = "imageUrlA", linkUrl = "linkUrlA"),
Banner(imageUrl = "imageUrlB", linkUrl = "linkUrlB"),
Banner(imageUrl = "imageUrlC", linkUrl = "linkUrlC"),
)
)

When("Banner를 전체 조회 하면") {
val results = bannerService.getBannerList()

Then("모든 Banner가 조회 된다") {
results.size shouldBe 3
}
}

When("Banner가 캐싱 되어 있으면") {
repeat(5) { bannerService.getBannerList() }

Then("퀴리가 발생 하지 않는다") {
verify(bannerRepository, atMost(1)).findAll()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

꼼꼼 테스트 좋아요 👍

}
}
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.petqua.presentation.banner

import com.petqua.application.banner.FindBannerResult
import com.petqua.domain.banner.Banner
import com.petqua.domain.banner.BannerRepository
import com.petqua.test.ApiTestConfig
import io.restassured.module.kotlin.extensions.Extract
import io.restassured.module.kotlin.extensions.Given
import io.restassured.module.kotlin.extensions.Then
import io.restassured.module.kotlin.extensions.When
import org.assertj.core.api.Assertions.assertThat
import org.springframework.http.HttpStatus

class BannerControllerTest(
private val bannerRepository: BannerRepository
) : ApiTestConfig() {
init {
Given("배너가 등록되어 있다.") {
val banner = bannerRepository.saveAll(
listOf(
Banner(imageUrl = "imageUrlC", linkUrl = "linkUrlA"),
Banner(imageUrl = "imageUrlB", linkUrl = "linkUrlB")
)
)

When("배너 목록을 조회한다.") {
val response = Given {
log().all()
} When {
get("/banner")
} Then {
log().all()
} Extract {
response()
}

Then("배너 목록을 응답한다.") {
val findBannerResponse = response.`as`(Array<FindBannerResult>::class.java)
assertThat(response.statusCode).isEqualTo(HttpStatus.OK.value())
assertThat(findBannerResponse.size).isEqualTo(2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SoftAssertions 안 쓰면 홍고가 화내요ㅠ

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 이걸 놓쳤네;;;;;;;;;;;

}
}
}
}
}
24 changes: 24 additions & 0 deletions src/test/kotlin/com/petqua/test/ApiTestConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.petqua.test

import io.kotest.core.spec.style.BehaviorSpec
import io.restassured.RestAssured
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.boot.test.web.server.LocalServerPort

@SpringBootTest(webEnvironment = RANDOM_PORT)
abstract class ApiTestConfig : BehaviorSpec() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조아요~~


@LocalServerPort
protected val port: Int = RestAssured.port

@Autowired
private lateinit var dataCleaner: DataCleaner

init {
afterContainer {
dataCleaner.clean()
}
}
}
Comment on lines +10 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 너무 좋은데요! 한결 편해질 것 같아요!!!

Loading