-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat : 컨텐츠, 북마크 엔티티 메소드 추가 * feat : 컨텐츠, 북마크 도메인 모델 * feat : 북마크 영속성 계층 구현 * feat : 컨텐츠 영속성 계층 구현 * feat : 북마크 등록 비즈니스 로직 * feat : 북마크 등록 api * feat : 북마크 관련 fixture, errorcode정의 * feat : 컨텐츠 조회 쿼리 수정 * feat : 컨텐츠 조회 쿼리 수정에 따른 테스트코드 수정 * feat : 컨텐츠 조회 쿼리 수정에 따른 코드 수정 * feat : TestContainer 설정 수정 및 불필요 파일 제거
- Loading branch information
1 parent
51cb9d9
commit 3034e3d
Showing
22 changed files
with
285 additions
and
55 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
adapters/in-web/src/main/kotlin/com/pokit/content/ContentController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pokit.content | ||
|
||
import com.pokit.auth.model.PrincipalUser | ||
import com.pokit.auth.model.toDomain | ||
import com.pokit.content.dto.response.BookMarkContentResponse | ||
import com.pokit.content.port.`in`.ContentUseCase | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/content/") | ||
class ContentController( | ||
private val contentUseCase: ContentUseCase | ||
) { | ||
@PostMapping("/{contentId}/bookmark") | ||
fun bookmarkContent( | ||
@AuthenticationPrincipal principalUser: PrincipalUser, | ||
@PathVariable("contentId") contentId: Long | ||
): ResponseEntity<BookMarkContentResponse> { | ||
val user = principalUser.toDomain() | ||
val response = contentUseCase.bookmarkContent(user, contentId = contentId) | ||
return ResponseEntity.ok(response) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ut-persistence/src/main/kotlin/com/pokit/out/persistence/bookmark/impl/BookMarkAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.pokit.out.persistence.bookmark.impl | ||
|
||
import com.pokit.bookmark.model.Bookmark | ||
import com.pokit.bookmark.port.out.BookmarkPort | ||
import com.pokit.out.persistence.bookmark.persist.BookMarkRepository | ||
import com.pokit.out.persistence.bookmark.persist.BookmarkEntity | ||
import com.pokit.out.persistence.bookmark.persist.toDomain | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class BookMarkAdapter( | ||
private val bookMarkRepository: BookMarkRepository | ||
) : BookmarkPort { | ||
override fun persist(bookmark: Bookmark): Bookmark { | ||
val bookmarkEntity = BookmarkEntity.of(bookmark) | ||
val savedBookmark = bookMarkRepository.save(bookmarkEntity) | ||
return savedBookmark.toDomain() | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...sistence/src/main/kotlin/com/pokit/out/persistence/bookmark/persist/BookMarkRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.pokit.out.persistence.bookmark.persist | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface BookMarkRepository : JpaRepository<BookmarkEntity, Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...s/out-persistence/src/main/kotlin/com/pokit/out/persistence/content/impl/ContenAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.pokit.out.persistence.content.impl | ||
|
||
import com.pokit.content.port.out.ContentPort | ||
import com.pokit.out.persistence.content.persist.ContentRepository | ||
import com.pokit.out.persistence.content.persist.toDomain | ||
import org.springframework.stereotype.Repository | ||
|
||
@Repository | ||
class ContenAdapter( | ||
private val contentRepository: ContentRepository | ||
) : ContentPort { | ||
override fun loadByUserIdAndId(userId: Long, id: Long) = contentRepository.findByUserIdAndId(userId, id) | ||
?.run { toDomain() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ersistence/src/main/kotlin/com/pokit/out/persistence/content/persist/ContentRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.pokit.out.persistence.content.persist | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.data.jpa.repository.Query | ||
import org.springframework.data.repository.query.Param | ||
|
||
interface ContentRepository : JpaRepository<ContentEntity, Long> { | ||
@Query( | ||
""" | ||
select co from ContentEntity co | ||
join CategoryEntity ca on co.categoryId = ca.id | ||
join UserEntity u on ca.userId = :userId | ||
where co.id = :id | ||
""" | ||
) | ||
fun findByUserIdAndId( | ||
@Param("userId") userId: Long, | ||
@Param("id") id: Long | ||
): ContentEntity? | ||
} |
8 changes: 0 additions & 8 deletions
8
...persistence/src/test/kotlin/com/pokit/out/persistence/common/config/TestAuditingConfig.kt
This file was deleted.
Oops, something went wrong.
13 changes: 0 additions & 13 deletions
13
...ersistence/src/test/kotlin/com/pokit/out/persistence/common/support/DataJpaTestSupport.kt
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
...s/out-persistence/src/test/kotlin/com/pokit/out/persistence/example/InfraContainerTest.kt
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
adapters/out-persistence/src/testFixtures/kotlin/com/pokit/bookmark/BookmarkFixture.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.pokit.bookmark | ||
|
||
import com.pokit.bookmark.model.Bookmark | ||
|
||
class BookmarkFixture { | ||
companion object { | ||
fun getBookmark(contentId: Long, userId: Long) = Bookmark( | ||
contentId = contentId, | ||
userId = userId | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
adapters/out-persistence/src/testFixtures/kotlin/com/pokit/content/ContentFixture.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.pokit.content | ||
|
||
import com.pokit.content.model.Content | ||
import com.pokit.content.model.ContentType | ||
|
||
class ContentFixture { | ||
companion object { | ||
fun getContent() = Content( | ||
categoryId = 1L, | ||
type = ContentType.LINK, | ||
data = "blahblah.com", | ||
title = "어떤 제목", | ||
memo = "이러한 내용 요약", | ||
alertYn = "YES" | ||
) | ||
} | ||
} |
39 changes: 16 additions & 23 deletions
39
adapters/out-persistence/src/testFixtures/kotlin/com/pokit/support/TestContainerSupport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,25 @@ | ||
package com.pokit.support | ||
|
||
import org.springframework.test.context.DynamicPropertyRegistry | ||
import org.springframework.test.context.DynamicPropertySource | ||
import org.testcontainers.containers.JdbcDatabaseContainer | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase | ||
import org.springframework.boot.test.util.TestPropertyValues | ||
import org.springframework.context.ApplicationContextInitializer | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.testcontainers.containers.MySQLContainer | ||
import org.testcontainers.utility.DockerImageName | ||
|
||
abstract class TestContainerSupport { | ||
class TestContainerSupport : ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
companion object { | ||
private const val MYSQL_IMAGE = "mysql:8.0" | ||
|
||
private const val MYSQL_PORT = 3306 | ||
|
||
private val MYSQL: JdbcDatabaseContainer<*> = | ||
MySQLContainer<Nothing>(DockerImageName.parse(MYSQL_IMAGE)) | ||
.withExposedPorts(MYSQL_PORT) | ||
|
||
init { | ||
MYSQL.start() | ||
val mysqlContainer: MySQLContainer<*> = MySQLContainer("mysql:8.0").apply { | ||
withExposedPorts(3306) | ||
start() | ||
} | ||
} | ||
|
||
@JvmStatic | ||
@DynamicPropertySource | ||
fun overrideProps(registry: DynamicPropertyRegistry) { | ||
registry.add("spring.datasource.driver-class-name") { MYSQL.driverClassName } | ||
registry.add("spring.datasource.url") { MYSQL.jdbcUrl } | ||
registry.add("spring.datasource.username") { MYSQL.username } | ||
registry.add("spring.datasource.password") { MYSQL.password } | ||
} | ||
override fun initialize(applicationContext: ConfigurableApplicationContext) { | ||
TestPropertyValues.of( | ||
"spring.datasource.url=${mysqlContainer.jdbcUrl}", | ||
"spring.datasource.username=${mysqlContainer.username}", | ||
"spring.datasource.password=${mysqlContainer.password}", | ||
"spring.jpa.hibernate.ddl-auto=create" | ||
).applyTo(applicationContext.environment) | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
application/src/main/kotlin/com/pokit/bookmark/port/out/BookmarkPort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.pokit.bookmark.port.out | ||
|
||
import com.pokit.bookmark.model.Bookmark | ||
|
||
interface BookmarkPort { | ||
fun persist(bookmark: Bookmark): Bookmark | ||
} |
8 changes: 8 additions & 0 deletions
8
application/src/main/kotlin/com/pokit/content/port/in/ContentUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.pokit.content.port.`in` | ||
|
||
import com.pokit.content.dto.response.BookMarkContentResponse | ||
import com.pokit.user.model.User | ||
|
||
interface ContentUseCase { | ||
fun bookmarkContent(user: User, contentId: Long): BookMarkContentResponse | ||
} |
7 changes: 7 additions & 0 deletions
7
application/src/main/kotlin/com/pokit/content/port/out/ContentPort.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.pokit.content.port.out | ||
|
||
import com.pokit.content.model.Content | ||
|
||
interface ContentPort { | ||
fun loadByUserIdAndId(userId: Long, id: Long): Content? | ||
} |
30 changes: 30 additions & 0 deletions
30
application/src/main/kotlin/com/pokit/content/port/service/ContentService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.pokit.content.port.service | ||
|
||
import com.pokit.bookmark.model.Bookmark | ||
import com.pokit.bookmark.port.out.BookmarkPort | ||
import com.pokit.common.exception.NotFoundCustomException | ||
import com.pokit.content.dto.response.BookMarkContentResponse | ||
import com.pokit.content.exception.ContentErrorCode | ||
import com.pokit.content.model.Content | ||
import com.pokit.content.port.`in`.ContentUseCase | ||
import com.pokit.content.port.out.ContentPort | ||
import com.pokit.user.model.User | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ContentService( | ||
private val contentPort: ContentPort, | ||
private val bookMarkPort: BookmarkPort | ||
) : ContentUseCase { | ||
override fun bookmarkContent(user: User, contentId: Long): BookMarkContentResponse { | ||
verifyContent(user.id, contentId) | ||
val bookmark = Bookmark(userId = user.id, contentId = contentId) | ||
val savedBookmark = bookMarkPort.persist(bookmark) | ||
return BookMarkContentResponse(savedBookmark.contentId) | ||
} | ||
|
||
private fun verifyContent(userId: Long, contentId: Long): Content { | ||
return contentPort.loadByUserIdAndId(userId, contentId) | ||
?: throw NotFoundCustomException(ContentErrorCode.NOT_FOUND_CONTENT) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
application/src/test/kotlin/com/pokit/content/port/service/ContentServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.pokit.content.port.service | ||
|
||
import com.pokit.bookmark.BookmarkFixture | ||
import com.pokit.bookmark.port.out.BookmarkPort | ||
import com.pokit.common.exception.NotFoundCustomException | ||
import com.pokit.content.ContentFixture | ||
import com.pokit.content.port.out.ContentPort | ||
import com.pokit.user.UserFixture | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
|
||
class ContentServiceTest : BehaviorSpec({ | ||
val contentPort = mockk<ContentPort>() | ||
val bookmarkPort = mockk<BookmarkPort>() | ||
val contentService = ContentService(contentPort, bookmarkPort) | ||
|
||
Given("컨텐츠에 대해 즐겨찾기 할 때") { | ||
val user = UserFixture.getUser() | ||
val requestContentId = 1L | ||
val invalidContentId = 2L | ||
val content = ContentFixture.getContent() | ||
val bookmark = BookmarkFixture.getBookmark(requestContentId, user.id) | ||
|
||
every { contentPort.loadByUserIdAndId(user.id, requestContentId) } returns content | ||
every { contentPort.loadByUserIdAndId(user.id, invalidContentId) } returns null | ||
every { bookmarkPort.persist(bookmark) } returns bookmark | ||
|
||
When("존재하지 않는 컨텐츠면") { | ||
Then("예외가 발생한다.") { | ||
shouldThrow<NotFoundCustomException> { | ||
contentService.bookmarkContent(user, invalidContentId) | ||
} | ||
} | ||
} | ||
|
||
When("존재하는 컨텐츠면") { | ||
val response = contentService.bookmarkContent(user, requestContentId) | ||
Then("북마크 처리 후 컨텐츠 아이디가 반환된다.") { | ||
response.contentId shouldBe requestContentId | ||
} | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.pokit.bookmark.model | ||
|
||
data class Bookmark( | ||
val contentId: Long, | ||
val userId: Long | ||
) |
5 changes: 5 additions & 0 deletions
5
domain/src/main/kotlin/com/pokit/content/dto/response/BookMarkContentResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.pokit.content.dto.response | ||
|
||
data class BookMarkContentResponse( | ||
val contentId: Long | ||
) |
10 changes: 10 additions & 0 deletions
10
domain/src/main/kotlin/com/pokit/content/exception/ContentErrorCode.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.pokit.content.exception | ||
|
||
import com.pokit.common.exception.ErrorCode | ||
|
||
enum class ContentErrorCode( | ||
override val message: String, | ||
override val code: String | ||
) : ErrorCode { | ||
NOT_FOUND_CONTENT("존재하지 않는 컨텐츠입니다.", "C_001") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.pokit.content.model | ||
|
||
data class Content( | ||
val categoryId: Long, | ||
val type: ContentType, | ||
val data: String, | ||
val title: String, | ||
val memo: String, | ||
val alertYn: String, | ||
) |