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) + } +}