Skip to content

Commit

Permalink
[feat #50] 도메인 파싱 로직 추가 (#51)
Browse files Browse the repository at this point in the history
* feat : domain 필드 추가

* feat : 도메인 파싱 응용 로직
  • Loading branch information
dlswns2480 authored Aug 1, 2024
1 parent 1354ade commit 479dc52
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ class ContentEntity(
val alertYn: String,

@Column(name = "is_deleted")
var deleted: Boolean = false
var deleted: Boolean = false,

@Column(name = "domain")
val domain: String
) : BaseEntity() {
fun delete() {
this.deleted = true
Expand All @@ -47,7 +50,8 @@ class ContentEntity(
data = content.data,
title = content.title,
memo = content.memo,
alertYn = content.alertYn
alertYn = content.alertYn,
domain = content.domain
)
}
}
Expand All @@ -60,5 +64,6 @@ fun ContentEntity.toDomain() = Content(
title = this.title,
memo = this.memo,
alertYn = this.alertYn,
createdAt = this.createdAt
createdAt = this.createdAt,
domain = this.domain
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class ContentService(
@Transactional
override fun create(user: User, contentCommand: ContentCommand): Content {
verifyCategory(contentCommand.categoryId, user.id)
return contentPort.persist(
contentCommand.toDomain()
)
val content = contentCommand.toDomain()
content.parseDomain()
return contentPort.persist(content)
}

@Transactional
Expand Down
17 changes: 16 additions & 1 deletion domain/src/main/kotlin/com/pokit/content/model/Content.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pokit.content.model

import com.pokit.content.dto.ContentCommand

import java.net.URI
import java.time.LocalDateTime

data class Content(
Expand All @@ -11,7 +13,8 @@ data class Content(
var title: String,
var memo: String,
var alertYn: String,
val createdAt: LocalDateTime = LocalDateTime.now()
val createdAt: LocalDateTime = LocalDateTime.now(),
var domain: String = data
) {
fun modify(contetCommand: ContentCommand) {
this.categoryId = contetCommand.categoryId
Expand All @@ -20,4 +23,16 @@ data class Content(
this.memo = contetCommand.memo
this.alertYn = contetCommand.alertYn
}

fun parseDomain() {
val domain = URI(this.data).host
val parts = domain.split(".")
this.domain = when {
domain.contains("youtube") || domain == "youtu.be" -> "youtube"
parts.size > 2 && parts[parts.size - 2] == "co" -> parts[parts.size - 3] // ex) www.pokit.co.kr
parts.size > 2 -> parts[parts.size - 2] // ex) www.pokit.com
parts.size == 2 -> parts[0]
else -> domain
}
}
}
32 changes: 32 additions & 0 deletions domain/src/test/kotlin/com/pokit/content/model/ContentTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pokit.content.model

import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe

class ContentTest : BehaviorSpec({
Given("URL이 주어졌을 때") {
val github = "https://github.com/YAPP-Github/24th-App-Team-4-BE/issues/50"
val google = "https://www.google.com"
val youtube = "https://youtu.be/bqSemSf0VqI?si=whk8MyNC_kGYB3Zl"
val githubData = Content(
categoryId = 1L,
type = ContentType.LINK,
data = github,
title = "깃허브",
memo = "test",
alertYn = "YES"
)
val googleData = githubData.copy(data = google)
val youtubeData = githubData.copy(data = youtube)
When("도메인을 파싱하면") {
githubData.parseDomain()
googleData.parseDomain()
youtubeData.parseDomain()
Then("파싱된 도메인이 domain 필드에 저장된다.") {
githubData.domain shouldBe "github"
googleData.domain shouldBe "google"
youtubeData.domain shouldBe "youtube"
}
}
}
})

0 comments on commit 479dc52

Please sign in to comment.