Skip to content

Commit

Permalink
refactor: feed -> post
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Aug 27, 2023
1 parent 508d204 commit 934aa76
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import joryu.sns_service.feed.entity.Feed
import joryu.sns_service.common.entity.BaseEntity
import joryu.sns_service.post.entity.Post

@Table(name = "comment")
@Entity
class Comment(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "feed_id", nullable = false)
val feed: Feed,
@JoinColumn(name = "post_id", nullable = false)
val post: Post,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
val parent: Comment?,

content: String,
) : BaseEntity() {
constructor() : this(Feed(), null, "")
constructor() : this(Post(), null, "")

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package joryu.sns_service.feed.entity
package joryu.sns_service.post.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
Expand All @@ -8,9 +8,9 @@ import jakarta.persistence.Id
import jakarta.persistence.Table
import joryu.sns_service.common.entity.BaseEntity

@Table(name = "feed")
@Table(name = "post")
@Entity
class Feed(
class Post(
content: String,
) : BaseEntity() {
constructor() : this("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package joryu.sns_service.feed.entity
package joryu.sns_service.post.entity

import jakarta.persistence.Entity
import jakarta.persistence.FetchType
Expand All @@ -11,18 +11,18 @@ import jakarta.persistence.Table
import joryu.sns_service.common.entity.BaseEntity
import joryu.sns_service.profile.entity.Profile

@Table(name = "feed_like")
@Table(name = "post_like")
@Entity
class FeedLike(
class PostLike(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id", nullable = false)
val likeMember: Profile,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "feed_id", nullable = false)
val feed: Feed,
@JoinColumn(name = "post_id", nullable = false)
val post: Post,
) : BaseEntity() {
constructor() : this(Profile(), Feed())
constructor() : this(Profile(), Post())

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package joryu.sns_service.feed.entity
package joryu.sns_service.post.entity

import jakarta.persistence.Entity
import jakarta.persistence.FetchType
Expand All @@ -14,10 +14,10 @@ import joryu.sns_service.common.entity.BaseEntity
@Entity
class Share(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "feed_id", nullable = false)
val feed: Feed,
@JoinColumn(name = "post_id", nullable = false)
val post: Post,
) : BaseEntity() {
constructor() : this(Feed())
constructor() : this(Post())

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package joryu.sns_service.post.repository

import joryu.sns_service.post.entity.PostLike
import org.springframework.data.jpa.repository.JpaRepository

interface PostLikeRepository : JpaRepository<PostLike, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package joryu.sns_service.post.repository

import joryu.sns_service.post.entity.Post
import org.springframework.data.jpa.repository.JpaRepository

interface PostRepository : JpaRepository<Post, Long>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package joryu.sns_service.feed.repository
package joryu.sns_service.post.repository

import joryu.sns_service.feed.entity.Share
import joryu.sns_service.post.entity.Share
import org.springframework.data.jpa.repository.JpaRepository

interface ShareRepository : JpaRepository<Share, Long>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package joryu.sns_service.post.entity

import jakarta.persistence.EntityManager
import joryu.sns_service.post.repository.PostRepository
import joryu.sns_service.post.entity.Post
import org.assertj.core.api.Assertions.assertThat
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 PostEntityTest {

@Autowired
lateinit var em: EntityManager

@Autowired
lateinit var postRepository: PostRepository

@Test
fun `BaseEntity에 createAt, updateAt이 잘 들어가야 한다`() {
val before = LocalDateTime.now()
val postEntity = postRepository.save(Post("123"))

em.flush()
em.clear()

val findPostEntity = postRepository.getReferenceById(postEntity.id)

assertThat(findPostEntity.createAt).isNotNull()
assertThat(findPostEntity.createAt).isAfter(before)
assertThat(findPostEntity.createAt).isBefore(LocalDateTime.now())
assertThat(findPostEntity.updateAt).isNotNull()
assertThat(findPostEntity.updateAt).isAfter(before)
assertThat(findPostEntity.updateAt).isBefore(LocalDateTime.now())
}

@Test
fun `BaseEntity의 Id는 auto increment 되어야 한다`() {
val postEntity1 = postRepository.save(Post("123"))
val postEntity2 = postRepository.save(Post("123"))
val postEntity3 = postRepository.save(Post("123"))

assertThat(postEntity1.id).isLessThan(postEntity2.id)
assertThat(postEntity2.id).isLessThan(postEntity3.id)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package joryu.sns_service.feed.entity
package joryu.sns_service.post.entity

import jakarta.persistence.EntityManager
import joryu.sns_service.feed.repository.FeedLikeRepository
import joryu.sns_service.feed.repository.FeedRepository
import joryu.sns_service.post.repository.PostLikeRepository
import joryu.sns_service.post.repository.PostRepository
import joryu.sns_service.profile.entity.Profile
import joryu.sns_service.profile.repository.ProfileRepository
import org.assertj.core.api.Assertions.assertThat
Expand All @@ -14,7 +14,7 @@ import org.springframework.transaction.annotation.Transactional
@Transactional
@SpringBootTest
// @Disabled
class FeedLikeTest {
class PostLikeTest {

@Autowired
lateinit var em: EntityManager
Expand All @@ -23,24 +23,24 @@ class FeedLikeTest {
lateinit var profileRepository: ProfileRepository

@Autowired
lateinit var feedRepository: FeedRepository
lateinit var postRepository: PostRepository

@Autowired
lateinit var feedLikeRepository: FeedLikeRepository
lateinit var postLikeRepository: PostLikeRepository

@Test
fun `연관관계가 잘 적용되어야 한다`() {
val profile = profileRepository.save(Profile("현모"))
val feed = feedRepository.save(Feed("피드내용"))
val feedLike = feedLikeRepository.save(FeedLike(profile, feed))
val post = postRepository.save(Post("피드내용"))
val postLike = postLikeRepository.save(PostLike(profile, post))

em.flush()
em.clear()

val findFeedLikeEntity = feedLikeRepository.getReferenceById(feedLike.id)
val findPostLikeEntity = postLikeRepository.getReferenceById(postLike.id)

assertThat(findFeedLikeEntity).isNotNull()
assertThat(findFeedLikeEntity.likeMember.name).isEqualTo(profile.name)
assertThat(findFeedLikeEntity.feed.content).isEqualTo(feed.content)
assertThat(findPostLikeEntity).isNotNull()
assertThat(findPostLikeEntity.likeMember.name).isEqualTo(profile.name)
assertThat(findPostLikeEntity.post.content).isEqualTo(post.content)
}
}

0 comments on commit 934aa76

Please sign in to comment.