Skip to content

Commit

Permalink
Merge pull request #19 from jojaeng2/feature/#12-기본-Entity-및-Reposito…
Browse files Browse the repository at this point in the history
…ry-생성

#12 [FEED] 기본 entity 및 repository 생성
  • Loading branch information
gusah009 authored Aug 27, 2023
2 parents 788d6bb + 934aa76 commit 684bfe4
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.common.entity.BaseEntity
import joryu.sns_service.post.entity.Post

@Table(name = "comment")
@Entity
class Comment(
@ManyToOne(fetch = FetchType.LAZY)
@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(Post(), null, "")

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0

@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,30 @@
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(
@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(Profile(), Comment())

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0
}
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package joryu.sns_service.feed.entity
package joryu.sns_service.post.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
import joryu.sns_service.common.entity.BaseEntity

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

class Post(
content: String,
) : BaseEntity() {
constructor() : this(0, "")
constructor(content: String) : this(0, content)
constructor() : this("")

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0

@Column(name = "view_count", nullable = false)
var viewCount: Long = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package joryu.sns_service.post.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 = "post_like")
@Entity
class PostLike(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id", nullable = false)
val likeMember: Profile,

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

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0
}
25 changes: 25 additions & 0 deletions sns_service/src/main/kotlin/joryu/sns_service/post/entity/Share.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package joryu.sns_service.post.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(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
val post: Post,
) : BaseEntity() {
constructor() : this(Post())

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0
}
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
@@ -0,0 +1,6 @@
package joryu.sns_service.post.repository

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
@@ -0,0 +1,46 @@
package joryu.sns_service.post.entity

import jakarta.persistence.EntityManager
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
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 PostLikeTest {

@Autowired
lateinit var em: EntityManager

@Autowired
lateinit var profileRepository: ProfileRepository

@Autowired
lateinit var postRepository: PostRepository

@Autowired
lateinit var postLikeRepository: PostLikeRepository

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

em.flush()
em.clear()

val findPostLikeEntity = postLikeRepository.getReferenceById(postLike.id)

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

0 comments on commit 684bfe4

Please sign in to comment.