Skip to content

Commit

Permalink
feat: 기본 Entity, Repository 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
gusah009 committed Aug 22, 2023
1 parent 6499268 commit 3e62716
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package joryu.sns_service.comment.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
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

@Table(name = "comment")
@Entity
class Comment(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "feed_id", nullable = false)
val feed: Feed,

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

content: String,
) : BaseEntity() {
constructor() : this(0, Feed(), null, "")
constructor(feed: Feed, parent: Comment) : this(0, feed, parent, "")

@Column(name = "content", nullable = false, length = 1000)
var content: String = content
private set
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package joryu.sns_service.comment.entity

import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import joryu.sns_service.common.entity.BaseEntity
import joryu.sns_service.profile.entity.Profile

@Table(name = "comment_like")
@Entity
class CommentLike(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id", nullable = false)
val likeMember: Profile,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "comment_id", nullable = false)
val comment: Comment,
) : BaseEntity() {
constructor() : this(0, Profile(), Comment())
constructor(likeMember: Profile, comment: Comment) : this(0, likeMember, comment)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package joryu.sns_service.comment.repository

import joryu.sns_service.comment.entity.CommentLike
import org.springframework.data.jpa.repository.JpaRepository

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

import joryu.sns_service.comment.entity.Comment
import org.springframework.data.jpa.repository.JpaRepository

interface CommentRepository : JpaRepository<Comment, Long>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package joryu.sns_service.profile.entity
package joryu.sns_service.common.entity

import jakarta.persistence.Column
import jakarta.persistence.EntityListeners
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.Table
import joryu.sns_service.profile.entity.BaseEntity
import joryu.sns_service.common.entity.BaseEntity

@Table(name = "feed")
@Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package joryu.sns_service.feed.entity

import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import joryu.sns_service.common.entity.BaseEntity
import joryu.sns_service.profile.entity.Profile

@Table(name = "feed_like")
@Entity
class FeedLike(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,

@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,
) : BaseEntity() {
constructor() : this(0, Profile(), Feed())
constructor(likeMember: Profile, feed: Feed) : this(0, likeMember, feed)
}
26 changes: 26 additions & 0 deletions sns_service/src/main/kotlin/joryu/sns_service/feed/entity/Share.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package joryu.sns_service.feed.entity

import jakarta.persistence.Entity
import jakarta.persistence.FetchType
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.Table
import joryu.sns_service.common.entity.BaseEntity

@Table(name = "share")
@Entity
class Share(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "feed_id", nullable = false)
val feed: Feed,
) : BaseEntity() {
constructor() : this(0, Feed())
constructor(feed: Feed) : this(0, feed)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package joryu.sns_service.feed.repository

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

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

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

interface ShareRepository : JpaRepository<Share, Long>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package joryu.sns_service.feed.entity

import jakarta.persistence.EntityManager
import joryu.sns_service.feed.repository.FeedLikeRepository
import joryu.sns_service.feed.repository.FeedRepository
import joryu.sns_service.profile.entity.Profile
import joryu.sns_service.profile.repository.ProfileRepository
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

@Transactional
@SpringBootTest
// @Disabled
class FeedLikeTest {

@Autowired
lateinit var em: EntityManager

@Autowired
lateinit var profileRepository: ProfileRepository

@Autowired
lateinit var feedRepository: FeedRepository

@Autowired
lateinit var feedLikeRepository: FeedLikeRepository

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

em.flush()
em.clear()

val findFeedLikeEntity = feedLikeRepository.getReferenceById(feedLike.id)

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

0 comments on commit 3e62716

Please sign in to comment.