Skip to content

Commit

Permalink
Feat: News plain text field 추가 (#80)
Browse files Browse the repository at this point in the history
* Feat: Add plainTextDescription field for news entity

* Feat: Add to create, and update plainTextDescription.

* Feat: Change to use plainTextDescription when making newssearchdto.

* Test: Test for create/update plainTextDescription.
  • Loading branch information
huGgW authored Sep 8, 2023
1 parent 8d5627c commit d2c0366
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.wafflestudio.csereal.core.news.database

import com.wafflestudio.csereal.common.cleanTextFromHtml
import com.wafflestudio.csereal.common.config.BaseTimeEntity
import com.wafflestudio.csereal.common.controller.AttachmentContentEntityType
import com.wafflestudio.csereal.common.controller.MainImageContentEntityType
Expand All @@ -18,6 +19,9 @@ class NewsEntity(
@Column(columnDefinition = "mediumtext")
var description: String,

@Column(columnDefinition = "mediumtext")
var plainTextDescription: String,

var isPublic: Boolean,

var isSlide: Boolean,
Expand All @@ -42,15 +46,20 @@ class NewsEntity(
return NewsEntity(
title = newsDto.title,
description = newsDto.description,
plainTextDescription = cleanTextFromHtml(newsDto.description),
isPublic = newsDto.isPublic,
isSlide = newsDto.isSlide,
isImportant = newsDto.isImportant,
)
}
}
fun update(updateNewsRequest: NewsDto) {
if (updateNewsRequest.description != this.description) {
this.description = updateNewsRequest.description
this.plainTextDescription = cleanTextFromHtml(updateNewsRequest.description)
}

this.title = updateNewsRequest.title
this.description = updateNewsRequest.description
this.isPublic = updateNewsRequest.isPublic
this.isSlide = updateNewsRequest.isSlide
this.isImportant = updateNewsRequest.isImportant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class NewsRepositoryImpl(
NewsSearchDto(
id = it.id,
title = it.title,
description = cleanTextFromHtml(it.description),
description = it.plainTextDescription,
createdAt = it.createdAt,
tags = it.newsTags.map { newsTagEntity -> newsTagEntity.tag.name },
imageURL = imageURL
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.wafflestudio.csereal.core.notice.news

import com.wafflestudio.csereal.core.news.database.NewsEntity
import com.wafflestudio.csereal.core.news.database.NewsRepository
import com.wafflestudio.csereal.core.news.dto.NewsDto
import com.wafflestudio.csereal.core.news.service.NewsService
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.data.repository.findByIdOrNull

@SpringBootTest
class NewsServiceTest(
private val newsService: NewsService,
private val newsRepository: NewsRepository,
) : BehaviorSpec() {
init {

afterSpec {
newsRepository.deleteAll()
}

Given("뉴스를 생성하려고 할 때 간단한 뉴스가 주어지면") {
val newsDTO = NewsDto(
id = -1,
title = "title",
description = """
<h1>Hello, World!</h1>
<p>This is news description.</p>
<h3>Goodbye, World!</h3>
""".trimIndent(),
tags = emptyList(),
createdAt = null,
modifiedAt = null,
isPublic = false,
isSlide = false,
isImportant = false,
prevId = null,
prevTitle = null,
nextId = null,
nextTitle = null,
imageURL = null,
attachments = null,
)

When("DTO를 이용하여 뉴스를 생성하면") {
val createdNewsDTO = newsService.createNews(newsDTO, null, null)

Then("뉴스가 생성되어야 한다.") {
newsRepository.count() shouldBe 1
newsRepository.findByIdOrNull(createdNewsDTO.id) shouldNotBe null
}

Then("plainTextDescription이 생성되었어야 한다.") {
val createdNewsEntity = newsRepository.findByIdOrNull(createdNewsDTO.id)!!
createdNewsEntity.plainTextDescription shouldBe "Hello, World! This is news description. Goodbye, World!"
}
}
}

Given("간단한 뉴스가 저장되어 있을 때") {
val newsEntity = newsRepository.save(
NewsEntity(
title = "title",
description = """
<h1>Hello, World!</h1>
<p>This is news description.</p>
<h3>Goodbye, World!</h3>
""".trimIndent(),
plainTextDescription = "Hello, World! This is news description. Goodbye, World!",
isPublic = false,
isSlide = false,
isImportant = false,
)
)

When("저장된 뉴스의 Description을 수정하면") {
newsService.updateNews(
newsEntity.id,
NewsDto.of(newsEntity, null, emptyList(), null)
.copy(description = """
<h1>Hello, World!</h1>
<p>This is modified news description.</p>
<h3>Goodbye, World!</h3>
<p>This is additional description.</p>
""".trimIndent()
),
null,
null
)

Then("description, plainTextDescription이 수정되어야 한다.") {
val updatedNewsEntity = newsRepository.findByIdOrNull(newsEntity.id)!!
updatedNewsEntity.description shouldBe """
<h1>Hello, World!</h1>
<p>This is modified news description.</p>
<h3>Goodbye, World!</h3>
<p>This is additional description.</p>
""".trimIndent()
updatedNewsEntity.plainTextDescription shouldBe "Hello, World! This is modified news description. Goodbye, World! This is additional description."
}
}
}
}
}

0 comments on commit d2c0366

Please sign in to comment.