Skip to content

Commit

Permalink
[feat #16] 엔티티 생성 (#17)
Browse files Browse the repository at this point in the history
* chore: local db 설정

* feat: jpa entity 작성

* chore: jpa entity 네이밍 수정
  • Loading branch information
jimin3263 authored Jul 13, 2024
1 parent ef86d36 commit 195880d
Show file tree
Hide file tree
Showing 21 changed files with 254 additions and 16 deletions.
10 changes: 10 additions & 0 deletions adapters/out-persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ plugins {
`java-test-fixtures`
}

allOpen {
annotation("jakarta.persistence.Entity")
annotation("jakarta.persistence.MappedSuperclass")
annotation("jakarta.persistence.Embeddable")
}

noArg {
annotation("jakarta.persistence.Entity")
}

dependencies {
// 모듈
implementation(project(":application"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.pokit.out.persistence

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
import java.time.ZoneId

@MappedSuperclass
@EntityListeners(value = [AuditingEntityListener::class])
abstract class BaseEntity {
@CreatedDate
@Column(updatable = false)
var createdAt: LocalDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"))

@LastModifiedDate
var updatedAt: LocalDateTime = LocalDateTime.now(ZoneId.of("Asia/Seoul"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pokit.out.persistence.alert.persist

import jakarta.persistence.*
import java.time.LocalDateTime

@Table(name = "NOTIFICATION")
@Entity
class NotificationEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "notification_time")
val notificationTime: LocalDateTime,

@Column(name = "content_id")
val contentId: Long,

@Column(name = "data")
val data: String,
) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.pokit.out.persistence.auth.impl

import com.pokit.auth.port.out.RefreshTokenPort
import com.pokit.out.persistence.auth.persist.RefreshTokenJpaEntity
import com.pokit.out.persistence.auth.persist.RefreshTokenEntity
import com.pokit.out.persistence.auth.persist.RefreshTokenRepository
import com.pokit.out.persistence.auth.persist.toDomain
import com.pokit.token.model.RefreshToken
Expand All @@ -12,7 +12,7 @@ class RefreshTokenAdapter(
private val refreshTokenRepository: RefreshTokenRepository,
) : RefreshTokenPort {
override fun persist(refreshToken: RefreshToken): RefreshToken {
val refreshTokenEntity = RefreshTokenJpaEntity.of(refreshToken)
val refreshTokenEntity = RefreshTokenEntity.of(refreshToken)
val savedToken = refreshTokenRepository.save(refreshTokenEntity)
return savedToken.toDomain()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ import jakarta.persistence.*

@Table(name = "REFRESH_TOKEN")
@Entity
class RefreshTokenJpaEntity(
class RefreshTokenEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "userId")
val userId: Long,

@Column(name = "token")
val token: String,
) {
companion object {
fun of(refreshToken: RefreshToken) =
RefreshTokenJpaEntity(
RefreshTokenEntity(
userId = refreshToken.userId,
token = refreshToken.token,
)
}
}

fun RefreshTokenJpaEntity.toDomain() = RefreshToken(userId = this.userId, token = this.token)
fun RefreshTokenEntity.toDomain() = RefreshToken(userId = this.userId, token = this.token)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.pokit.out.persistence.auth.persist

import org.springframework.data.jpa.repository.JpaRepository

interface RefreshTokenRepository : JpaRepository<RefreshTokenJpaEntity, Long> {
fun findByUserId(userId: Long): RefreshTokenJpaEntity?
interface RefreshTokenRepository : JpaRepository<RefreshTokenEntity, Long> {
fun findByUserId(userId: Long): RefreshTokenEntity?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.pokit.out.persistence.bookmark.persist

import jakarta.persistence.*

// 콕 엔티티
@Table(name = "BOOKMARK")
@Entity
class BookmarkEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "content_id")
val contentId: Long,

@Column(name = "user_id")
val userId: Long,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.pokit.out.persistence.category.persist

import com.pokit.category.model.OpenType
import com.pokit.out.persistence.BaseEntity
import jakarta.persistence.*

// 포킷 엔티티
@Table(name = "CATEGORY")
@Entity
class CategoryEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "user_id")
val userId: Long,

@Column(name = "name")
var name: String,

@Column(name = "memo")
var memo: String,

@Enumerated(EnumType.STRING)
@Column(name = "open_type")
val openType: OpenType,

@OneToOne
@JoinColumn(name = "image_id")
val image: CategoryImageEntity,
) : BaseEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.pokit.out.persistence.category.persist

import jakarta.persistence.*

@Table(name = "CATEGORY_IMAGE")
@Entity
class CategoryImageEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "path")
val path: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.pokit.out.persistence.content.persist

import com.pokit.content.model.ContentType
import com.pokit.out.persistence.BaseEntity
import jakarta.persistence.*

@Table(name = "CONTENT")
@Entity
class ContentEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "category_id")
val categoryId: Long,

@Column(name = "type")
@Enumerated(EnumType.STRING)
val type: ContentType,

@Column(name = "data")
val data: String, // ex. LINK - url

@Column(name = "title")
val title: String,

@Column(name = "memo")
val memo: String,

@Column(name = "alert_yn")
val alertYn: String,
) : BaseEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.pokit.out.persistence.log.persist

import com.pokit.log.model.LogType
import com.pokit.out.persistence.BaseEntity
import jakarta.persistence.*

@Table(name = "USER_LOG")
@Entity
class UserLogEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "content_id")
val contentId: Long,

@Column(name = "user_id")
val userId: Long,

@Enumerated(EnumType.STRING)
val type: LogType,
) : BaseEntity() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.pokit.out.persistence.subscribe.persist

import jakarta.persistence.*

@Table(name = "SUBSCRIBE")
@Entity
class SubscribeEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "category_id")
val categoryId: Long,

@Column(name = "user_id")
val userId: Long,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.pokit.out.persistence.user.impl

import com.pokit.out.persistence.user.persist.UserJpaEntity
import com.pokit.out.persistence.user.persist.UserEntity
import com.pokit.out.persistence.user.persist.UserRepository
import com.pokit.out.persistence.user.persist.toDomain
import com.pokit.user.model.User
Expand All @@ -12,8 +12,8 @@ class UserAdapter(
private val userRepository: UserRepository,
) : UserPort {
override fun persist(user: User): User {
val userJpaEntity = UserJpaEntity.of(user)
val savedUser = userRepository.save(userJpaEntity)
val userEntity = UserEntity.of(user)
val savedUser = userRepository.save(userEntity)
return savedUser.toDomain()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,27 @@ import jakarta.persistence.*

@Table(name = "USER")
@Entity
class UserJpaEntity(
class UserEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
val id: Long = 0L,

@Column(name = "email")
val email: String,

@Column(name = "role")
val role: Role,
) {
companion object {
fun of(user: User) =
UserJpaEntity(
UserEntity(
email = user.email,
role = user.role,
)
}
}

fun UserJpaEntity.toDomain() = User(id = this.id, email = this.email, role = this.role)
fun UserEntity.toDomain() = User(id = this.id, email = this.email, role = this.role)

fun UserJpaEntity.toPrincipalUser() = null // Security Context에 담을 user
fun UserEntity.toPrincipalUser() = null // Security Context에 담을 user
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.pokit.out.persistence.user.persist

import org.springframework.data.jpa.repository.JpaRepository

interface UserRepository : JpaRepository<UserJpaEntity, Long> {
fun findByEmail(email: String): UserJpaEntity?
interface UserRepository : JpaRepository<UserEntity, Long> {
fun findByEmail(email: String): UserEntity?
}
6 changes: 6 additions & 0 deletions domain/src/main/kotlin/com/pokit/category/model/OpenType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.pokit.category.model

enum class OpenType {
PUBLIC,
PRIVATE,
}
6 changes: 6 additions & 0 deletions domain/src/main/kotlin/com/pokit/content/model/ContentType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.pokit.content.model

enum class ContentType {
LINK,
IMAGE, // 1.0 에는 사용안함
}
5 changes: 5 additions & 0 deletions domain/src/main/kotlin/com/pokit/log/model/LogType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.pokit.log.model

enum class LogType {
READ, // 읽음 로그
}
7 changes: 7 additions & 0 deletions domain/src/main/kotlin/com/pokit/user/model/InterestType.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.pokit.user.model

enum class InterestType(
val kor: String,
) {
SPORTS("스포츠/레저")
}
13 changes: 13 additions & 0 deletions env/db/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: "3.8"

services:
db:
container_name: pokit-mysql-local
image: mysql:8.0
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: pokit
TZ: Asia/Seoul
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
2 changes: 2 additions & 0 deletions env/db/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE DATABASE IF NOT EXISTS pokit;
USE pokit;

0 comments on commit 195880d

Please sign in to comment.