From 29f03dc64708698d58dc6607f6f4ed2c5790e919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=ED=95=B8=EB=AA=A8?= Date: Mon, 21 Aug 2023 23:46:47 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20baseEntity=20=EC=83=9D=EC=84=B1=20&=20F?= =?UTF-8?q?eedEntity=20=EA=B3=A8=EA=B2=A9=EB=A7=8C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sns_service/SnsServiceApplication.kt | 4 +- .../joryu/sns_service/feed/entity/Feed.kt | 30 +++++++++++ .../feed/repository/FeedRepository.kt | 6 +++ .../sns_service/profile/entity/BaseEntity.kt | 24 +++++++++ .../sns_service/feed/entity/FeedEntityTest.kt | 51 +++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/feed/entity/Feed.kt create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/feed/repository/FeedRepository.kt create mode 100644 sns_service/src/main/kotlin/joryu/sns_service/profile/entity/BaseEntity.kt create mode 100644 sns_service/src/test/kotlin/joryu/sns_service/feed/entity/FeedEntityTest.kt diff --git a/sns_service/src/main/kotlin/joryu/sns_service/SnsServiceApplication.kt b/sns_service/src/main/kotlin/joryu/sns_service/SnsServiceApplication.kt index 69e17cf..6c4a5a3 100644 --- a/sns_service/src/main/kotlin/joryu/sns_service/SnsServiceApplication.kt +++ b/sns_service/src/main/kotlin/joryu/sns_service/SnsServiceApplication.kt @@ -2,10 +2,12 @@ package joryu.sns_service import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication +import org.springframework.data.jpa.repository.config.EnableJpaAuditing +@EnableJpaAuditing @SpringBootApplication class SnsServiceApplication fun main(args: Array) { - runApplication(*args) + runApplication(*args) } diff --git a/sns_service/src/main/kotlin/joryu/sns_service/feed/entity/Feed.kt b/sns_service/src/main/kotlin/joryu/sns_service/feed/entity/Feed.kt new file mode 100644 index 0000000..4c3b91e --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/feed/entity/Feed.kt @@ -0,0 +1,30 @@ +package joryu.sns_service.feed.entity + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.Table +import joryu.sns_service.profile.entity.BaseEntity + +@Table(name = "feed") +@Entity +class Feed( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + val id: Long, + + content: String, +) : BaseEntity() { + constructor() : this(0, "") + constructor(content: String) : this(0, content) + + @Column(name = "view_count", nullable = false) + var viewCount: Long = 0 + private set + + @Column(name = "content", nullable = false, length = 1000) + var content: String = content + private set +} diff --git a/sns_service/src/main/kotlin/joryu/sns_service/feed/repository/FeedRepository.kt b/sns_service/src/main/kotlin/joryu/sns_service/feed/repository/FeedRepository.kt new file mode 100644 index 0000000..ab68ca5 --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/feed/repository/FeedRepository.kt @@ -0,0 +1,6 @@ +package joryu.sns_service.feed.repository + +import joryu.sns_service.feed.entity.Feed +import org.springframework.data.jpa.repository.JpaRepository + +interface FeedRepository : JpaRepository diff --git a/sns_service/src/main/kotlin/joryu/sns_service/profile/entity/BaseEntity.kt b/sns_service/src/main/kotlin/joryu/sns_service/profile/entity/BaseEntity.kt new file mode 100644 index 0000000..900336e --- /dev/null +++ b/sns_service/src/main/kotlin/joryu/sns_service/profile/entity/BaseEntity.kt @@ -0,0 +1,24 @@ +package joryu.sns_service.profile.entity + +import jakarta.persistence.Column +import jakarta.persistence.EntityListeners +import jakarta.persistence.MappedSuperclass +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.LastModifiedDate +import org.springframework.data.jpa.domain.support.AuditingEntityListener +import java.time.LocalDateTime + +@MappedSuperclass +@EntityListeners(AuditingEntityListener::class) +abstract class BaseEntity { + + @CreatedDate + @Column(name = "create_at", nullable = false, updatable = false) + var createAt: LocalDateTime = LocalDateTime.now() + private set + + @LastModifiedDate + @Column(name = "update_at", nullable = false) + var updateAt: LocalDateTime = LocalDateTime.now() + private set +} diff --git a/sns_service/src/test/kotlin/joryu/sns_service/feed/entity/FeedEntityTest.kt b/sns_service/src/test/kotlin/joryu/sns_service/feed/entity/FeedEntityTest.kt new file mode 100644 index 0000000..05b27ca --- /dev/null +++ b/sns_service/src/test/kotlin/joryu/sns_service/feed/entity/FeedEntityTest.kt @@ -0,0 +1,51 @@ +package joryu.sns_service.feed.entity + +import jakarta.persistence.EntityManager +import joryu.sns_service.feed.repository.FeedRepository +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.transaction.annotation.Transactional +import java.time.LocalDateTime + +@Transactional +@SpringBootTest +@Disabled("프로젝트 초기 kotlin entity 테스트용도. springBootTest라 속도가 느려서 disabled.") +class FeedEntityTest { + + @Autowired + lateinit var em: EntityManager + + @Autowired + lateinit var feedRepository: FeedRepository + + @Test + fun `BaseEntity에 createAt, updateAt이 잘 들어가야 한다`() { + val before = LocalDateTime.now() + val feedEntity = feedRepository.save(Feed("123")) + + em.flush() + em.clear() + + val findFeedEntity = feedRepository.getReferenceById(feedEntity.id) + + assertThat(findFeedEntity.createAt).isNotNull() + assertThat(findFeedEntity.createAt).isAfter(before) + assertThat(findFeedEntity.createAt).isBefore(LocalDateTime.now()) + assertThat(findFeedEntity.updateAt).isNotNull() + assertThat(findFeedEntity.updateAt).isAfter(before) + assertThat(findFeedEntity.updateAt).isBefore(LocalDateTime.now()) + } + + @Test + fun `BaseEntity의 Id는 auto increment 되어야 한다`() { + val feedEntity1 = feedRepository.save(Feed("123")) + val feedEntity2 = feedRepository.save(Feed("123")) + val feedEntity3 = feedRepository.save(Feed("123")) + + assertThat(feedEntity1.id).isLessThan(feedEntity2.id) + assertThat(feedEntity2.id).isLessThan(feedEntity3.id) + } +}