Skip to content

Commit

Permalink
feat: baseEntity 생성, 적용 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiwon-cho authored Feb 17, 2024
1 parent d6c0e81 commit b8ef194
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/main/kotlin/mara/server/common/BaseEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package mara.server.common

import com.fasterxml.jackson.annotation.JsonFormat
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
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.ZonedDateTime

@MappedSuperclass
@JsonIgnoreProperties(
value = [
"createdAt, updatedAt",
],
allowGetters = true,
)
@EntityListeners(AuditingEntityListener::class)
abstract class BaseEntity {
@CreatedDate
@Column(name = "created_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", timezone = "Asia/Seoul")
var createdAt: ZonedDateTime = ZonedDateTime.now()

@LastModifiedDate
@Column(name = "updated_at")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", timezone = "Asia/Seoul")
var updatedAt: ZonedDateTime = ZonedDateTime.now()
}
3 changes: 2 additions & 1 deletion src/main/kotlin/mara/server/domain/friend/Friendship.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import mara.server.common.BaseEntity
import mara.server.domain.user.User

@Entity
Expand All @@ -20,7 +21,7 @@ class Friendship(
@JoinColumn(name = "to_user_id")
val toUser: User,

) {
) : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "friendship_id", nullable = false)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/mara/server/domain/ingredient/Ingredient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import mara.server.common.BaseEntity

@Entity
class Ingredient(
var category: String,
var name: String,
var iconImage: String,
var expirationDays: Int = 0
) {
) : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val ingredientId: Long = 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import mara.server.common.BaseEntity
import mara.server.domain.refrigerator.Refrigerator
import java.time.LocalDateTime

Expand All @@ -30,7 +31,7 @@ class IngredientDetail(
var addDate: LocalDateTime,
var expirationDate: LocalDateTime,
var isDeleted: Boolean = false
) {
) : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val ingredientDetailId: Long = 0L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import mara.server.common.BaseEntity
import mara.server.domain.user.User
import java.time.LocalDateTime

Expand All @@ -19,7 +20,7 @@ class Refrigerator(
@JoinColumn(name = "userId")
val user: User,

) {
) : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "refrigerator_id", nullable = false)
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/mara/server/domain/share/ApplyShare.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import mara.server.common.BaseEntity
import mara.server.domain.user.User

@Entity
Expand All @@ -18,7 +19,7 @@ data class ApplyShare(
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shareId")
val share: Share,
) {
) : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "apply_share_id")
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/mara/server/domain/share/Share.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import mara.server.common.BaseEntity
import mara.server.domain.ingredient.IngredientDetail
import mara.server.domain.user.User
import java.time.LocalDate
Expand All @@ -34,7 +35,7 @@ data class Share(
var location: String,
var status: ShareStatus,
var thumbNailImage: String
) {
) : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/mara/server/domain/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.Table
import mara.server.common.BaseEntity

enum class Role { USER }
@Entity
Expand All @@ -20,7 +21,7 @@ class User(
val inviteCode: String,
@Enumerated(EnumType.STRING)
var profileImage: ProfileImage,
) {
) : BaseEntity() {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val userId: Long = 0L
Expand Down

0 comments on commit b8ef194

Please sign in to comment.